module GovukPayApiClient::Api

Public Class Methods

included(base) click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 3
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

get() click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 23
def get
  client("#{api_url}#{endpoint}").get.tap { |resp|
    # Only timeouts and network issues raise errors.
    handle_response_errors(resp)
    @body = resp.body
  }
rescue Excon::Error => e
  raise Unavailable, e
end
post() click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 13
def post
  client("#{api_url}#{endpoint}").post(body: request_body.to_json).tap { |resp|
    # Only timeouts and network issues raise errors.
    handle_response_errors(resp)
    @body = resp.body
  }
rescue Excon::Error => e
  raise Unavailable, e
end
response_body() click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 33
def response_body
  @response_body ||= JSON.parse(@body, symbolize_names: true)
rescue JSON::ParserError
  ''
end

Private Instance Methods

api_url() click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 47
def api_url
  ENV.fetch('GOVUK_PAY_API_URL')
end
client(uri) click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 51
def client(uri)
  Excon.new(
    uri,
    headers: {
      'Authorization' => "Bearer #{ENV.fetch('GOVUK_PAY_API_KEY')}",
      'Content-Type' => 'application/json',
      'Accept' => 'application/json'
    },
    persistent: true
  )
end
handle_response_errors(resp) click to toggle source
# File lib/govuk_pay_api_client/api.rb, line 41
def handle_response_errors(resp)
  if (400..599).cover?(resp.status)
    raise Unavailable, resp.status
  end
end