Sha256: b0e74d5c461038ce492a56acd94ea0164ceef0018a5484a47a39bffa4fa9c095

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Trusona
  module Api
    #
    ## An a wrapper around HTTParty
    class HTTPClient
      POST  = 'POST'
      GET   = 'GET'
      PATCH = 'PATCH'
      CONTENT_TYPE = 'application/json;charset=utf-8'

      def initialize(host = nil)
        @host = host || Trusona.config.api_host
      end

      def post(path, params = {})
        execute(path, params, POST)
      end

      def patch(path, params = {})
        execute(path, params, PATCH)
      end

      def get(path, params = {})
        execute(path, params, GET)
      end

      private

      def execute(path, params, method)
        request = Trusona::Api::SignedRequest.new(path, params, method, @host)

        # Power of ruby or hard to read?
        unverified = HTTParty.send(
          method.downcase,
          request.uri,
          body: request.body,
          headers: request.headers
        )

        Trusona::Api::VerifiedResponse.new(unverified)
      end
    end

    ##
    # A default nil http client
    class NilHTTPClient
      def initialize(host)
        @host = host
      end

      def post(_uri, _params); end

      def get(_uri, _params); end

      def patch(_uri, _params); end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trusona-0.18.2 lib/trusona/api/client.rb
trusona-0.18.1 lib/trusona/api/client.rb
trusona-0.16.0 lib/trusona/api/client.rb