class HotsApi::Fetcher

Constants

ApiLimitReachedError

Attributes

last_response[R]

Public Instance Methods

get(path, params: {}) click to toggle source
# File lib/hots_api/fetcher.rb, line 11
def get(path, params: {})
  with_retrying do
    HTTP.get("#{base_path}/#{path}", params: params)
  end
end
post(path, body: nil, file: nil) click to toggle source
# File lib/hots_api/fetcher.rb, line 17
def post(path, body: nil, file: nil)
  with_retrying do
    HTTP.post("#{base_path}/#{path}", post_options_for(body: body, file: file))
  end
end

Private Instance Methods

base_path() click to toggle source
# File lib/hots_api/fetcher.rb, line 32
def base_path
  'https://hotsapi.net/api/v1'
end
post_options_for(body:, file:) click to toggle source
# File lib/hots_api/fetcher.rb, line 25
def post_options_for(body:, file:)
  {}.tap do |options|
    options[:body] = body if body
    options[:form] = {file: HTTP::FormData::File.new(file)} if file
  end
end
with_retrying(tries: 10) { || ... } click to toggle source
# File lib/hots_api/fetcher.rb, line 36
def with_retrying(tries: 10)
  tries.times do |try|
    response = yield

    if response.status.too_many_requests? && try < tries - 1
      sleep 0.5
    else
      return @last_response = response
    end
  end

  raise ApiLimitReachedError, "API limit reached, tried #{tries} times"
end