class Funky::Connection::API
Constants
- FB_API_VERSION
Public Class Methods
batch_request(ids:, fields:)
click to toggle source
# File lib/funky/connections/api.rb, line 75 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
fetch(path_query, is_array: false)
click to toggle source
# File lib/funky/connections/api.rb, line 28 def self.fetch(path_query, is_array: false) uri = URI "https://#{host}/#{FB_API_VERSION}/#{path_query}&limit=100&access_token=#{app_id}%7C#{app_secret}" is_array ? fetch_multiple_pages(uri).uniq : json_for(uri) rescue URI::InvalidURIError raise Funky::ContentNotFound, "Invalid URL" end
fetch_all(path_query)
click to toggle source
# File lib/funky/connections/api.rb, line 12 def self.fetch_all(path_query) uri = URI "https://#{host}/#{FB_API_VERSION}/#{path_query}&limit=100&access_token=#{app_id}%7C#{app_secret}" fetch_data_with_paging_token(uri) end
fetch_data_with_paging_token(uri)
click to toggle source
# File lib/funky/connections/api.rb, line 17 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)
click to toggle source
# File lib/funky/connections/api.rb, line 35 def self.fetch_multiple_pages(uri) puts "Fetching '#{uri.path}' until #{ URI.decode_www_form(uri.query).to_h['until'] || 'now'}" json = json_for(uri) if json[:data].empty? @try_count ||= 0 if @previous_timestamp && @try_count < 10 && (Date.parse @previous_timestamp rescue nil) timestamp = (Date.parse(@previous_timestamp) - 1).strftime('%F') @try_count += 1 @previous_timestamp = timestamp new_query = URI.decode_www_form(uri.query).to_h.merge('until' => timestamp) uri.query = URI.encode_www_form(new_query) json[:data] + fetch_multiple_pages(uri) else [] end else timestamp = 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 == timestamp [] else @try_count = 0 @previous_timestamp = timestamp new_query = URI.decode_www_form(uri.query).to_h.merge('until' => timestamp) uri.query = URI.encode_www_form(new_query) json[:data] + fetch_multiple_pages(uri) end end end
request(id:, fields:)
click to toggle source
# File lib/funky/connections/api.rb, line 68 def self.request(id:, fields:) uri = URI::HTTPS.build host: host, path: "/#{FB_API_VERSION}/#{id}", query: "access_token=#{app_id}%7C#{app_secret}&fields=#{fields}" response_for(get_http_request(uri), uri) end
Private Class Methods
app_id()
click to toggle source
# File lib/funky/connections/api.rb, line 91 def self.app_id Funky.configuration.app_id end
app_secret()
click to toggle source
# File lib/funky/connections/api.rb, line 95 def self.app_secret Funky.configuration.app_secret end
create_batch_for(ids, fields)
click to toggle source
# File lib/funky/connections/api.rb, line 103 def self.create_batch_for(ids, fields) ids.map do |id| {"method":"GET", "relative_url": "/#{FB_API_VERSION}/#{id}?fields=#{fields}"} end end
host()
click to toggle source
# File lib/funky/connections/api.rb, line 87 def self.host 'graph.facebook.com' end
post_http_request(uri)
click to toggle source
# File lib/funky/connections/api.rb, line 99 def self.post_http_request(uri) Net::HTTP::Post.new uri end