Class: Adparlor::Facebook::GraphApi::AdTargetingSearch

Inherits:
Object
  • Object
show all
Includes:
Api
Defined in:
lib/adparlor/facebook/graph_api/ad_targeting_search.rb

Instance Method Summary collapse

Methods included from Api

#base_uri, #conn, #conn_multi, #delete, #get, #post, #proxy_api_key

Constructor Details

#initialize(access_token) ⇒ AdTargetingSearch

Returns a new instance of AdTargetingSearch.



7
8
9
# File 'lib/adparlor/facebook/graph_api/ad_targeting_search.rb', line 7

def initialize(access_token)
  @access_token = access_token
end

Instance Method Details

#pathObject



11
12
13
# File 'lib/adparlor/facebook/graph_api/ad_targeting_search.rb', line 11

def path
  '/search'
end

#search(query, type, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adparlor/facebook/graph_api/ad_targeting_search.rb', line 15

def search(query, type, options = {})
  type_param = type.respond_to?(:type_param) ? type.type_param : type.name.split('::').last.downcase
  params = { q: query, type: type_param, access_token: @access_token }
  params.merge!(type::OPTIONS) if type.const_defined?('OPTIONS')
  params.merge!(options)
  response = get(path, params)
  if response.body['data'].is_a?(Hash)
    obj = type.new
    keys = response.body['data'].keys
    keys.each do |key|
      vals = []
      response.body['data'][key].each do |val|
        vals << parse_value(key, val.last)
      end
      obj.send("#{key}=".to_sym, vals)
    end
    obj
  else
    response.body['data'].map do |data|
      obj = type.new
      data.each do |k, v|
        obj.send("#{k}=".to_sym, v)
      end
      obj
    end
  end
end