Sha256: 56a45630bad18b9a1740991ac017f3b6365b9ab92e73dcef57d9a97e7d1382d7

Contents?: true

Size: 943 Bytes

Versions: 1

Compression:

Stored size: 943 Bytes

Contents

require 'open-uri'

module TelephoneAppointments
  class Api
    SSL_PORT = 443

    def post(path, form_data)
      uri = URI.parse("#{api_uri}#{path}")
      http = Net::HTTP.new(uri.host, uri.port)
      http.read_timeout = read_timeout
      http.use_ssl = true if uri.port == SSL_PORT
      request = Net::HTTP::Post.new(uri.request_uri, headers)
      request.set_form_data(form_data)

      TelephoneAppointments::Response.new(http.request(request))
    end

    private

    def headers
      {}.tap do |hash|
        hash['Authorization'] = "Bearer #{bearer_token}" if bearer_token
        hash['Accept'] = 'application/json'
      end
    end

    def bearer_token
      ENV['TELEPHONE_APPOINTMENTS_API_BEARER_TOKEN']
    end

    def api_uri
      ENV.fetch('TELEPHONE_APPOINTMENTS_API_URI', 'http://localhost:3001')
    end

    def read_timeout
      ENV.fetch('TELEPHONE_APPOINTMENTS_API_READ_TIMEOUT', 5).to_i
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
telephone_appointments-0.1.1 lib/telephone_appointments/api.rb