Sha256: e98db8254d406e9f3847b76591be1cd615262d73a843f25f10ac5a9d7678c30b

Contents?: true

Size: 1.83 KB

Versions: 20

Compression:

Stored size: 1.83 KB

Contents

module Adparlor
  module Facebook
    module GraphApi
      class AdTargetingSearch
        include Adparlor::Facebook::Api

        def initialize(access_token)
          @access_token = access_token
        end

        def path
          '/search'
        end

        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

        private

        def parse_value(key, value)
          case key
          when 'countries'
            obj = AdTargetingTypes::AdGeolocations::AdCountry.new
          when 'cities'
            obj = AdTargetingTypes::AdGeolocations::AdCity.new
          when 'regions'
            obj = AdTargetingTypes::AdGeolocations::AdRegion.new
          when 'zips'
            obj = AdTargetingTypes::AdGeolocations::AdZipCode.new
          else
            return value.to_s
          end
          value.each { |k, v| obj.send("#{k}=".to_sym, v) }
          obj
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
adparlor-facebook-0.8.2 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.8.0 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.9 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.8 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.7 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.6 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.5 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.4 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.3 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.2 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.1 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.7.0 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.6 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.5 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.4 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.3 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.2 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.1 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.6.0 lib/adparlor/facebook/graph_api/ad_targeting_search.rb
adparlor-facebook-0.5.9 lib/adparlor/facebook/graph_api/ad_targeting_search.rb