Class: Funky::Connection::API Private
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class Method Summary collapse
- .app_id ⇒ Object private
- .app_secret ⇒ Object private
- .batch_request(ids:, fields:) ⇒ Object private
- .create_batch_for(ids, fields) ⇒ Object private
- .fetch(path_query, is_array: false) ⇒ Object private
- .fetch_all(path_query) ⇒ Object private
- .fetch_data_with_paging_token(uri) ⇒ Object private
- .fetch_multiple_pages(uri) ⇒ Object private
- .host ⇒ Object private
- .post_http_request(uri) ⇒ Object private
- .request(id:, fields:) ⇒ Object private
Methods inherited from Base
get_http_request, json_for, response_for, sleep_and_retry_response_for
Class Method Details
.app_id ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
87 88 89 |
# File 'lib/funky/connections/api.rb', line 87 def self.app_id Funky.configuration.app_id end |
.app_secret ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
91 92 93 |
# File 'lib/funky/connections/api.rb', line 91 def self.app_secret Funky.configuration.app_secret end |
.batch_request(ids:, fields:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 74 75 76 77 78 79 |
# File 'lib/funky/connections/api.rb', line 71 def self.batch_request(ids:, fields:) uri = URI::HTTPS.build host: host, path: "/", query: "include_headers=false&access_token=#{app_id}%7C#{app_secret}" batch = create_batch_for ids, fields http_request = post_http_request uri http_request.set_form_data batch: batch.to_json response_for(http_request, uri) end |
.create_batch_for(ids, fields) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
99 100 101 102 103 |
# File 'lib/funky/connections/api.rb', line 99 def self.create_batch_for(ids, fields) ids.map do |id| {"method":"GET", "relative_url": "/v2.8/#{id}?fields=#{fields}"} end end |
.fetch(path_query, is_array: false) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
26 27 28 29 |
# File 'lib/funky/connections/api.rb', line 26 def self.fetch(path_query, is_array: false) uri = URI "https://#{host}/v2.8/#{path_query}&limit=100&access_token=#{app_id}%7C#{app_secret}" is_array ? fetch_multiple_pages(uri).uniq : json_for(uri) end |
.fetch_all(path_query) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 13 |
# File 'lib/funky/connections/api.rb', line 10 def self.fetch_all(path_query) uri = URI "https://#{host}/v2.9/#{path_query}&limit=100&access_token=#{app_id}%7C#{app_secret}" fetch_data_with_paging_token(uri) end |
.fetch_data_with_paging_token(uri) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/funky/connections/api.rb', line 15 def self.fetch_data_with_paging_token(uri) json = json_for(uri) if !json[:data].empty? && json[:paging][:next] next_paging_uri = URI json[:paging][:next] puts "Fetching '#{uri.path}' with #{ URI.decode_www_form(next_paging_uri.query).to_h['after'] }" json[:data] + fetch_data_with_paging_token(next_paging_uri) else json[:data] end end |
.fetch_multiple_pages(uri) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/funky/connections/api.rb', line 31 def self.fetch_multiple_pages(uri) json = json_for(uri) puts "Fetching '#{uri.path}' until #{ URI.decode_www_form(uri.query).to_h['until'] || 'now'}" if json[:data].empty? @try_count ||= 0 if @previous_timestamp && @try_count < 1 && (Date.parse @previous_timestamp rescue nil) = (Date.parse(@previous_timestamp) - 1).strftime('%F') @try_count += 1 @previous_timestamp = new_query = URI.decode_www_form(uri.query).to_h.merge('until' => ) uri.query = URI.encode_www_form(new_query) json[:data] + fetch_multiple_pages(uri) else [] end else = if json[:data].count == 1 Date.parse(json[:data][-1][:created_time]).strftime('%F') else Time.parse(json[:data][-1][:created_time]).to_i end if @previous_timestamp == [] else @try_count = 0 @previous_timestamp = new_query = URI.decode_www_form(uri.query).to_h.merge('until' => ) uri.query = URI.encode_www_form(new_query) json[:data] + fetch_multiple_pages(uri) end end end |
.host ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
83 84 85 |
# File 'lib/funky/connections/api.rb', line 83 def self.host 'graph.facebook.com' end |
.post_http_request(uri) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
95 96 97 |
# File 'lib/funky/connections/api.rb', line 95 def self.post_http_request(uri) Net::HTTP::Post.new uri end |
.request(id:, fields:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
64 65 66 67 68 69 |
# File 'lib/funky/connections/api.rb', line 64 def self.request(id:, fields:) uri = URI::HTTPS.build host: host, path: "/v2.8/#{id}", query: "access_token=#{app_id}%7C#{app_secret}&fields=#{fields}" response_for(get_http_request(uri), uri) end |