Sha256: 94f13193253207ceb7d9abb99e617756e6019149109aded905d36eca0727ba9a

Contents?: true

Size: 754 Bytes

Versions: 1

Compression:

Stored size: 754 Bytes

Contents

require 'net/http'

module OxfordDictionary
  # A lightweight request class for use by the endpoints
  # All endpoints implement only the GET action
  class Request
    BASE_URL = 'https://od-api.oxforddictionaries.com/api/v2'.freeze

    def initialize(app_id:, app_key:)
      @app_id = app_id
      @app_key = app_key
    end

    def get(uri:)
      uri = URI("#{BASE_URL}/#{uri}")

      Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
        https.request(request_object(uri))
      end
    end

    private

    def request_object(uri)
      Net::HTTP::Get.new(uri).tap do |request|
        request['Accept'] = 'application/json'
        request['app_id'] = @app_id
        request['app_key'] = @app_key
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oxford_dictionary-3.0.0 lib/oxford_dictionary/request.rb