Sha256: 973547055baf36e454af8962ba205b6b5f72707f08abdfca8735c5af8bd9bfb0

Contents?: true

Size: 1.1 KB

Versions: 7

Compression:

Stored size: 1.1 KB

Contents

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

7 entries across 7 versions & 1 rubygems

Version Path
quiz_api_client-1.0.0 lib/quiz_api_client/http_client.rb
quiz_api_client-0.2.1 lib/quiz_api_client/http_client.rb
quiz_api_client-0.2.0 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.10 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.9 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.8 lib/quiz_api_client/http_client.rb
quiz_api_client-0.1.7 lib/quiz_api_client/http_client.rb