Sha256: e46b1c11ee099012a3b117546b7ad1c51bf80d258752a2a1520a00054d5040b7

Contents?: true

Size: 841 Bytes

Versions: 4

Compression:

Stored size: 841 Bytes

Contents

require "dialers"

module Github
  class ApiCaller < Dialers::Caller
    TIMEOUT_IN_SECONDS = 5
    GITHUB_API_URL = "https://api.github.com"

    setup_api(url: GITHUB_API_URL) do |faraday|
      faraday.request :json
      faraday.request :request_headers, accept: "application/vnd.github.v3+json"
      faraday.response :json
      faraday.adapter :net_http
      faraday.options.timeout = TIMEOUT_IN_SECONDS
      faraday.options.open_timeout = TIMEOUT_IN_SECONDS
    end

    short_circuits.add(
      if: -> (response) { Dialers::Status.new(response.status).server_error? },
      do: -> (response) { fail Dialers::ServerError.new(response) }
    )

    short_circuits.add(
      if: -> (response) { Dialers::Status.new(response.status).is?(404) },
      do: -> (response) { fail Dialers::NotFoundError.new(response) }
    )
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dialers-0.2.2 examples/github/api_caller.rb
dialers-0.1.2 examples/github/api_caller.rb
dialers-0.1.1 examples/github/api_caller.rb
dialers-0.1.0 examples/github/api_caller.rb