Sha256: 1004ee2ce6f482c264416be05bf7cbf5ea84a79d00455cd2a819683888e902a5

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

require 'open-uri'

module TelephoneAppointments
  class Api
    SSL_PORT = 443

    def post(path, form_data = nil)
      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) if 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

3 entries across 3 versions & 1 rubygems

Version Path
telephone_appointments-0.3.0 lib/telephone_appointments/api.rb
telephone_appointments-0.2.1 lib/telephone_appointments/api.rb
telephone_appointments-0.2.0 lib/telephone_appointments/api.rb