class FacebookAds::AdObject

Attributes

__all_fields[R]
attributes[R]
deserializer[RW]
last_api_response[R]
last_destroyed[RW]
last_saved[RW]

Public Class Methods

get(id, *args) { |obj| ... } click to toggle source
# File lib/facebook_ads/ad_object.rb, line 52
def self.get(id, *args)
  obj = new({id: id}, *args)
  yield obj if block_given?
  obj
end
new(attributes, *args) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 35
def initialize(attributes, *args)
  if attributes.empty?
    raise InvalidParameterError, 'Invalid attributes. Must include at least one attribute'
  end

  update_attributes(attributes)
  # assume object with only id in the attributes as not loaded

  # is next arg a list of fields?
  fields = (args[0].is_a?(Array) || args[0].is_a?(String)) ? args.shift : []
  fields = fields.split(',') if fields.is_a?(String)
  session = args.shift

  self.__all_fields = fields + attributes.keys
  self.session = session
end

Public Instance Methods

[](attr_name) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 72
def [](attr_name)
  @attributes[attr_name.to_sym]
end
__all_fields=(fields) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 96
def __all_fields=(fields)
  @__all_fields = Set.new((fields.is_a?(String) ? fields.split(',') : fields.map(&:to_s)).map(&:to_sym))
end
as_json(*opts) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 80
def as_json(*opts)
  to_hash
end
changes() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 153
def changes
  @changes ||= {}
end
destroy(params = {}) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 147
def destroy(params = {})
  delete(params) do |attrs|
    self.last_destroyed = Time.now if attrs['success']
  end
end
fields_as_string() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 92
def fields_as_string
  @__all_fields.to_a.join(',')
end
graph_params() click to toggle source

TODO refactor this to somewhere

# File lib/facebook_ads/ad_object.rb, line 116
def graph_params
  {fields: fields_as_string}
end
id() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 58
def id
  self[:id]
end
inspect() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 88
def inspect
  "#<#{self.class.name} #{to_hash.inspect}>"
end
load!() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 104
def load!
  get(graph_params) do |attrs|
    update_attributes(attrs)
  end
end
loaded?() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 100
def loaded?
  (@__all_fields - attributes.keys).empty?
end
reload!() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 110
def reload!
  # delete all attribute except id
  @attributes = @attributes.keep_if { |k,_| k == :id }
end
save(params = {}) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 139
def save(params = {})
  post(graph_params.merge(changes).merge(params)) do |attrs|
    update_attributes(attrs)
    @changes = {}
    self.last_saved = Time.now
  end
end
session() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 157
def session
  @session || Session.default_session
end
session=(session) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 161
def session=(session)
  @session = session.is_a?(Hash) ? Session.new(session) : session
end
to_hash() click to toggle source
# File lib/facebook_ads/ad_object.rb, line 76
def to_hash
  @attributes.merge(changes)
end
to_json(*a) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 84
def to_json(*a)
  as_json.to_json(*a)
end
update_attributes(attrs) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 62
def update_attributes(attrs)
  @attributes ||= {}
  @attributes.merge!(
    self.class.deserializer ?
      self.class.deserializer.deserialize(symbolize_keys_shallow(attrs)) :
      symbolize_keys_shallow(attrs)
  )
  self
end

Private Instance Methods

extract_options(params) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 170
def extract_options(params)
  option_keys = [:batch_opts]
  options = {}
  option_keys.each { |k| options[k] = params.delete(k) if params.has_key?(k) }
  [params, options]
end
symbolize_keys_shallow(hash) click to toggle source
# File lib/facebook_ads/ad_object.rb, line 166
def symbolize_keys_shallow(hash)
  Hash[hash.map { |k,v| [k.to_sym,v] }]
end