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.

Class Method Summary collapse

Class Method Details

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



73
74
75
76
77
78
79
80
81
# File 'lib/funky/connections/api.rb', line 73

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) ⇒ 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
30
31
# 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)
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.



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.



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
63
64
# File 'lib/funky/connections/api.rb', line 33

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:) ⇒ 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.



66
67
68
69
70
71
# File 'lib/funky/connections/api.rb', line 66

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