Method: Funky::Connection::API.fetch_multiple_pages

Defined in:
lib/funky/connections/api.rb

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

API:

  • private



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