Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Created March 7, 2012 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenhwkim/1993750 to your computer and use it in GitHub Desktop.
Save allenhwkim/1993750 to your computer and use it in GitHub Desktop.
fb_graph_overrides
module FbGraph
module Connections
module AdCampaigns
def ad_campaign!(options = {})
ad_campaign = post(options.merge(:connection => :adcampaigns))
fb_data = ad_campaign[:data]? ad_campaign[:data][:campaigns][ad_campaign[:id]] : ad_campaign
AdCampaign.new(ad_campaign[:id], options.merge(fb_data.symbolize_keys).merge(
:access_token => options[:access_token] || self.access_token
))
end
end
module AdImages
def ad_images(options = {})
ad_images = self.connection(:adimages, options)
ad_images.map! do |ad_image|
AdImage.new(ad_image[:hash], ad_image.merge(
:access_token => options[:access_token] || self.access_token
))
end
end
def ad_image!(options = {})
raise "Invalid file option, must have :file as key" if options[:file].empty?
raise "Image file not found" unless File.exist?(options[:file])
options[:file] = File.open(options[:file])
ad_image = post(options.merge(:connection => :adimages))
fb_data = ad_image[:images]? ad_image[:images].values.first.symbolize_keys : ad_image
AdImage.new(fb_data[:hash], options.merge( fb_data ).merge(
:access_token => options[:access_token] || self.access_token
))
end
end
module AdCreatives
def ad_creatives(options = {})
ad_creatives = self.connection(:adcreatives, options)
ad_creatives.map! do |ad_creative|
AdCreative.new(ad_creative[:creative_id], ad_creative.merge(
:access_token => options[:access_token] || self.access_token
))
end
end
def ad_creative!(options = {}) # only id is returned
if options[:body]
options['body'] = options[:body]
options.delete(:body)
end
ad_creative = post(options.merge(:connection => :adcreatives))
AdCreative.new(ad_creative[:id], options.merge(ad_creative).merge(
:access_token => options[:access_token] || self.access_token
))
end
end
end
AdAccount.send(:include, Connections::AdImages)
AdAccount.send(:include, Connections::AdCreatives)
class Collection < Array
def initialize_with_clean_options(options = nil)
options[:data] = options[:data].values if !options.nil? && options[:data].is_a?(Hash)
initialize_without_clean_options(options)
end
alias_method_chain :initialize, :clean_options
end
class AdImage < Node
attr_accessor :hash, :url
def initialize(identifier, attributes = {})
super
%w(hash url).each do |field|
send("#{field}=", attributes[field.to_sym])
end
end
end
class AdCreative < Node
attr_accessor :name, :type, :object_id, :body, :image_hash, :image_url, :creative_id
attr_accessor :count_current_adgroups, :title, :run_status, :link_url, :preview_url
attr_accessor :related_fan_page, :auto_update, :story_id, :action_spec
def initialize(identifier, attributes = {})
super
(%w(name type object_id body image_hash image_url creative_id) +
%w(count_current_adgroups title run_status link_url preview_url) +
%w(related_fan_page auto_update story_id action_spec)).each do |field|
send("#{field}=", attributes[field.to_sym])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment