Sha256: 276a0b209ab314592a491fc4e570243c074b282ebd4d22059e8cce0d099ad3ea

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 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.to_json)
    end

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

    def put(path, body = {})
      make_request :put, path, options(body: body.to_json)
    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',
        'Content-Type' => 'application/json' }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quiz_api_client-2.0.0 lib/quiz_api_client/http_client.rb