class CustomAudience::CustomAudience

Attributes

attributes[R]
token[R]
users[RW]

Public Class Methods

new(name_or_hash = {}) click to toggle source
# File lib/custom_audience.rb, line 12
def initialize(name_or_hash = {})
  if name_or_hash.is_a?(Hash)
    @attributes = name_or_hash.stringify_keys
  else
    @attributes = {"name" => name_or_hash}
  end

  self.token = @attributes.delete('token')
  fetch_attributes!
end

Public Instance Methods

account_id() click to toggle source
# File lib/custom_audience.rb, line 44
def account_id
  attributes['account_id'].to_s.gsub(/^act_/, '')
end
account_id=(account_id) click to toggle source
# File lib/custom_audience.rb, line 34
def account_id=(account_id)
  attributes["account_id"] = account_id
  fetch_attributes!
end
exists?() click to toggle source
# File lib/custom_audience.rb, line 23
def exists?
  id.present?
end
id() click to toggle source
# File lib/custom_audience.rb, line 52
def id
  attributes["id"]
end
name() click to toggle source
# File lib/custom_audience.rb, line 48
def name
  attributes["name"]
end
save() click to toggle source
# File lib/custom_audience.rb, line 27
def save
  # Should try and fetch attributes?
  create if ! exists?

  add_users
end
token=(token) click to toggle source
# File lib/custom_audience.rb, line 39
def token=(token)
  @token = token
  fetch_attributes!
end

Private Instance Methods

add_users() click to toggle source
# File lib/custom_audience.rb, line 67
def add_users
  users.each_slice(1000) do |slice|
    graph.put_connections id, 'users', users: JSON.dump(slice.map {|user| {"id" => user } })
  end
end
all() click to toggle source
# File lib/custom_audience.rb, line 73
def all
  @all ||= graph.get_connections("act_" + account_id, 'customaudiences').map do |audience_data|
    CustomAudience.new audience_data
  end
end
create() click to toggle source
# File lib/custom_audience.rb, line 61
def create
  id = graph.put_connections("act_" + account_id, 'customaudiences', name: name)["id"]

  @attributes = graph.get_object(id)
end
fetch_attributes!() click to toggle source
# File lib/custom_audience.rb, line 79
def fetch_attributes!
  return unless account_id.present? && token.present?

  if match = all.detect {|audience| audience.name == name }
    @attributes = match.attributes
  end
end
graph() click to toggle source
# File lib/custom_audience.rb, line 57
def graph
  @graph ||= Koala::Facebook::API.new(token)
end