module TwitterAds::Utils

Public Class Methods

deprecated(name, opts = {}) click to toggle source

Creates a deprecation message.

@param name [String] The name of the object or method being deprecated. @param replacement [String] The name of the new object or method (optional). @param refer [String] HTTP address with supporting information (optional).

@api private @since 0.3.2

# File lib/twitter-ads/utils.rb, line 66
def deprecated(name, opts = {})
  message = +"[DEPRECATED] #{name} has been deprecated"
  message += opts[:replacement] ? " (please use #{opts[:replacement]})." : '.'
  message += " Please see #{opts[:refer]} for more info." if opts[:refer]
  warn message
end
extract_response_headers(headers) click to toggle source
# File lib/twitter-ads/utils.rb, line 73
def extract_response_headers(headers)
  values = {}
  # only get "X-${name}" custom response headers
  headers.each { |key, value|
    if key =~ /^x-/
      values[key.gsub(/^x-/, '').tr('-', '_')] = \
        value.first =~ /^[0-9]*$/ ? value.first.to_i : value.first
    end
  }
  values
end
flatten_params(args) click to toggle source
# File lib/twitter-ads/utils.rb, line 85
def flatten_params(args)
  params = args
  params.each { |key, value|
    if value.is_a?(Array)
      next if value.empty?
      params[key] = value.join(',')
    end
  }
  params
end
symbolize!(object) click to toggle source

Converts key names to symbols on a given object.

@param object [Object] The object to be converted.

@return [Object] The symbolized, converted object.

@api private @since 0.1.0

# File lib/twitter-ads/utils.rb, line 49
def symbolize!(object)
  if object.is_a?(Array)
    object.each_with_index { |value, index| object[index] = symbolize!(value) }
  elsif object.is_a?(Hash)
    object.keys.each { |key| object[key.to_sym] = symbolize!(object.delete(key)) }
  end
  object
end
to_bool(object) click to toggle source

Helper to convert objects into boolean values.

@param object [Object] The object to be converted.

@return [Boolean] The boolean result.

@api private @since 0.1.0

# File lib/twitter-ads/utils.rb, line 17
def to_bool(object)
  (object.to_s.downcase == 'false') ? false : !!object
end
to_time(time, granularity = nil, utc_offset = nil) click to toggle source

Helper to convert a time object according to a given granularity level.

@param time [Time] A valid Time instance. @param granularity [Symbol] A symbol representing the desired granuarlity (eg. :hour).

@return [Time] The formatted Time instance.

@api private @since 0.1.0

# File lib/twitter-ads/utils.rb, line 30
def to_time(time, granularity = nil, utc_offset = nil)
  return time.iso8601 unless granularity
  if granularity == :hour
    Time.new(time.year, time.month, time.day, time.hour, 0, 0, utc_offset).iso8601
  elsif granularity == :day
    Time.new(time.year, time.month, time.day, 0, 0, 0, utc_offset).iso8601
  else
    time.iso8601
  end
end