Class: Funky::Connection::API Private

Inherits:
Base
  • Object
show all
Defined in:
lib/funky/connections/api.rb

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.

Constant Summary collapse

FB_API_VERSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'v2.10'

Class Method Summary collapse

Methods inherited from Base

get_http_request, instrument, json_for, response_for, sleep_and_retry_response_for

Class Method Details

.app_idObject

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_id
  Funky.configuration.app_id
end

.app_secretObject

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.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.



75
76
77
78
79
80
81
82
83
# 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

.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.



103
104
105
106
107
# 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

.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.



28
29
30
31
32
33
# 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) ⇒ 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.



12
13
14
15
# 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) ⇒ 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.



17
18
19
20
21
22
23
24
25
26
# 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) ⇒ 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.



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
63
64
65
66
# 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

.hostObject

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.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.



99
100
101
# File 'lib/funky/connections/api.rb', line 99

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.



68
69
70
71
72
73
# 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