Module: Adparlor::Facebook::Api
- Included in:
- GraphApi::AdTargetingSearch, GraphApi::GraphObject
- Defined in:
- lib/adparlor/facebook/api.rb
Instance Method Summary collapse
- #base_uri ⇒ Object
- #conn ⇒ Object
- #conn_multi ⇒ Object
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #post(path, options = {}, method = 'CREATE') ⇒ Object
- #proxy_api_key ⇒ Object
Instance Method Details
#base_uri ⇒ Object
17 18 19 |
# File 'lib/adparlor/facebook/api.rb', line 17 def base_uri Adparlor::Facebook.configuration.base_uri end |
#conn ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/adparlor/facebook/api.rb', line 25 def conn Faraday.default_adapter = :httpclient @conn ||= Faraday.new do |faraday| faraday.request :multipart faraday.request :retry, max: 3, exceptions: [Exception] faraday.request :url_encoded faraday.response :json faraday.adapter Faraday.default_adapter faraday.headers['x-api-key'] = proxy_api_key unless proxy_api_key.nil? end end |
#conn_multi ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/adparlor/facebook/api.rb', line 37 def conn_multi @conn_multi ||= Faraday.new( full_url('', {}), request: { params_encoder: Adparlor::Facebook::MultipleRequestParamsEncoder } ) do |faraday| faraday.request :retry, max: 3, exceptions: [Exception] faraday.response :json faraday.adapter Faraday.default_adapter faraday.headers['x-api-key'] = proxy_api_key unless proxy_api_key.nil? end end |
#delete(path, options = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/adparlor/facebook/api.rb', line 79 def delete(path, = {}) raise FbError.new('required parameter access_token missing', 500) unless [:access_token] || path.include?('access_token') conn.delete full_url(path, ) do |request| request.headers['Content-Type'] = 'application/json' request.params = request.body = instance_variables.each_with_object({}) { |var, hash| hash[var.to_s.sub(/@/, '').to_sym] = instance_variable_get(var) }.to_json end rescue Faraday::ParsingError # FB has returned some data that we can't parse into JSON raise FbError.new("Unable to make request.", 500) end |
#get(path, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/adparlor/facebook/api.rb', line 48 def get(path, = {}) [:fields] = self.class.fields(*[:fields]) if [:fields] && [:fields].is_a?(Array) raise FbError.new('required parameter access_token missing', 500) unless [:access_token] || path.include?('access_token') if path.to_s.empty? && .key?(:ids) conn_multi.get '', else conn.get full_url(path, ), end rescue Faraday::ParsingError # FB has returned some data that we can't parse into JSON raise FbError.new("Unable to make request.", 500) end |
#post(path, options = {}, method = 'CREATE') ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/adparlor/facebook/api.rb', line 61 def post(path, = {}, method = 'CREATE') self.class.validate_post_fields(*instance_variables.map { |k| k.to_s.delete('@') }, method) if self.class.respond_to?(:validate_post_fields) raise FbError.new('required parameter access_token missing', 500) unless [:access_token] || path.include?('access_token') body = instance_variables.each_with_object({}) { |var, hash| hash[var.to_s.sub(/@/, '').to_sym] = instance_variable_get(var) } content_type_header = .delete(:content_type_header) content_type_header ||= body[:source].respond_to?(:content_type) || !@files.nil? ? 'multipart/form-data' : 'application/json' body.delete(:files) body[:bytes] = Base64.encode64(open(body.delete(:source).io, &:read)) if body.key?(:source) conn.post full_url(path, ) do |request| request.headers['Content-Type'] = content_type_header request.params = request.body = content_type_header == 'multipart/form-data' ? (@files || {}).merge(body) : body.to_json end rescue Faraday::ParsingError # FB has returned some data that we can't parse into JSON raise FbError.new("Unable to make request.", 500) end |
#proxy_api_key ⇒ Object
21 22 23 |
# File 'lib/adparlor/facebook/api.rb', line 21 def proxy_api_key Adparlor::Facebook.configuration.proxy_api_key end |