class Nokotime::Connection

Public Instance Methods

get(path, params: {}, request_options: {}) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/nokotime/connection.rb, line 6
def get(path, params: {}, request_options: {})
  response = connection.get do |request|
    authorize_request(request)
    set_request_options(request, request_options)
    request.url path, params
  end

  response
rescue Faraday::ConnectionFailed => e
  raise Errors::ConnectionFailed.new(e), e.message
rescue Faraday::ResourceNotFound => e
  raise Errors::ResourceNotFound.new(e), e.message
rescue Faraday::ClientError => e
  raise Errors::ClientError.new(e), e.message
end
get_in_parallel( path, from_page_number, to_page_number, params: {}, request_options: {} ) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/nokotime/connection.rb, line 25
def get_in_parallel(
  path,
  from_page_number,
  to_page_number,
  params: {},
  request_options: {}
)
  responses = []

  connection.in_parallel(manager) do
    (from_page_number..to_page_number).each do |page|
      merged_params = {page: page}.merge(params)

      responses << get(
        path, params: merged_params, request_options: request_options
      )
    end
  end

  responses
rescue Faraday::ConnectionFailed => e
  raise Errors::ConnectionFailed.new(e), e.message
rescue Faraday::ResourceNotFound => e
  raise Errors::ResourceNotFound.new(e), e.message
rescue Faraday::ClientError => e
  raise Errors::ClientError.new(e), e.message
end

Private Instance Methods

connection() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/nokotime/connection.rb, line 57
def connection
  @connection ||= Faraday.new(default_options) do |connection|
    connection.request  :json
    connection.response :json, content_type: /\bjson$/
    connection.response :raise_error
    connection.adapter  :typhoeus
  end
end
default_options() click to toggle source
# File lib/nokotime/connection.rb, line 66
def default_options
  {
    url: Nokotime.configuration.url,
    headers: {
      user_agent: "MyNokoBot/1.0",
      accept: "application/json"
    }
  }
end
manager() click to toggle source
# File lib/nokotime/connection.rb, line 81
def manager
  @manager ||= Typhoeus::Hydra.new(
    max_concurrency: Nokotime.configuration.max_concurrency
  )
end
set_request_options(request, options) click to toggle source
# File lib/nokotime/connection.rb, line 76
def set_request_options(request, options)
  request.options.timeout = options[:timeout]
  request.options.open_timeout = options[:open_timeout]
end