Sha256: f17018d9160022ce95847bc2db51217cce413e1c1b4c62dd5c9cc32fa9c378c2

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

require 'httparty'

module QuizApiClient
  class HttpClient
    include HTTParty

    class RequestFailed < StandardError; end

    attr_reader :jwt, :uri

    def initialize(uri:, jwt:)
      @uri = uri
      @jwt = jwt
    end

    def get(path, query = {})
      make_request :get, path, options(query: query)
    end

    def post(path, body = {})
      make_request :post, path, options(body: body)
    end

    def patch(path, body = {})
      make_request :patch, path, options(body: body)
    end

    def put(path, body = {})
      make_request :put, path, options(body: body)
    end

    def delete(path)
      make_request :delete, path, options
    end

    private

    def make_request(method, path, options)
      self.class.send(
        method,
        "#{uri}#{path}",
        options
      )
    rescue HTTParty::Error, Errno::ECONNREFUSED, Net::ReadTimeout => e
      raise RequestFailed, e.message
    end

    def options(opts = {})
      { headers: headers }.merge(opts)
    end

    def headers
      { 'Authorization' => jwt,
        'AuthType' => 'Signature',
        'Accept' => 'application/json' }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
quiz_api_client-0.1.5 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.4 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.3 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.1 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.0 lib/quiz_api_client/http_client.rb