Sha256: 8271f595a0c6ddf8269f5412e07370832744a9b27aaeb74e8285bd603ab73107

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'net/http'
require 'uri'

module Kentaa
  module Api
    class Request
      attr_reader :config

      def initialize(config)
        @config = config
      end

      def get(path, params = {})
        uri = URI.parse(File.join(config.api_url, path))
        uri.query = URI.encode_www_form(params) unless params.empty?

        request = Net::HTTP::Get.new(uri)
        request["X-Api-Key"] = config.api_key

        begin
          result = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
            http.request(request)
          end
        # Try to catch some common exceptions Net::HTTP might raise.
        rescue Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH,
               IOError, SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::OpenTimeout,
               Net::ProtocolError, Net::ReadTimeout, OpenSSL::SSL::SSLError => e
          raise Kentaa::Api::Exception, e.message
        end

        response = Kentaa::Api::Response.new(result)

        if response.error?
          raise Kentaa::Api::RequestError, response
        end

        response
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kentaa-api-0.3.2 lib/kentaa/api/request.rb
kentaa-api-0.3.1 lib/kentaa/api/request.rb
kentaa-api-0.3.0 lib/kentaa/api/request.rb