Sha256: 8b557a3d188a4d11231cb5ec92b9205e9b041fe7cfbabe5306b949c13d580a03

Contents?: true

Size: 789 Bytes

Versions: 17

Compression:

Stored size: 789 Bytes

Contents

require 'open-uri'

module BookingLocations
  class Api
    def get(id)
      response = open("#{api_uri}/api/v1/booking_locations/#{id}.json", headers_and_options)
      yield JSON.parse(response.read)
    rescue OpenURI::HTTPError, Net::ReadTimeout
      nil
    end

    private

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

    def bearer_token
      ENV['BOOKING_LOCATIONS_API_BEARER_TOKEN']
    end

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

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
booking_locations-0.17.0 lib/booking_locations/api.rb
booking_locations-0.16.0 lib/booking_locations/api.rb
booking_locations-0.15.0 lib/booking_locations/api.rb
booking_locations-0.14.0 lib/booking_locations/api.rb
booking_locations-0.13.0 lib/booking_locations/api.rb
booking_locations-0.12.0 lib/booking_locations/api.rb
booking_locations-0.11.0 lib/booking_locations/api.rb
booking_locations-0.10.0 lib/booking_locations/api.rb
booking_locations-0.9.1 lib/booking_locations/api.rb
booking_locations-0.9.0 lib/booking_locations/api.rb
booking_locations-0.8.0 lib/booking_locations/api.rb
booking_locations-0.7.0 lib/booking_locations/api.rb
booking_locations-0.6.1 lib/booking_locations/api.rb
booking_locations-0.6.0 lib/booking_locations/api.rb
booking_locations-0.5.0 lib/booking_locations/api.rb
booking_locations-0.4.0 lib/booking_locations/api.rb
booking_locations-0.3.0 lib/booking_locations/api.rb