Sha256: 4d706cf1cc8be1275f66d1c92861ea9352cd2df560648ab2b05e9f2fcdc1fdab

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 KB

Contents

require 'poms/api/drivers/net_http'
require 'poms/api/request'
require 'poms/api/auth'
require 'poms/errors'

module Poms
  module Api
    # The Client module isolates all HTTP interactions, regardless of the driver
    # module to implement the actual operations. Use the Client module to build
    # signed requests and execute them.
    #
    # @see Poms::Api::Drivers::NetHttp
    module Client
      extend Drivers::NetHttp

      module_function

      def get(uri, credentials, headers = {})
        handle_response(
          execute(
            Auth.sign(
              prepare_get(uri, headers),
              credentials
            )
          )
        )
      end

      def post(uri, body, credentials, headers = {})
        handle_response(
          execute(
            Auth.sign(
              prepare_post(uri, body, headers),
              credentials
            )
          )
        )
      end

      def handle_response(response)
        case response.code
        when 400..499 then raise Errors::HttpMissingError, response.code
        when 500..599 then raise Errors::HttpServerError, response.code
        else
          response
        end
      end

      def prepare_get(uri, headers = {})
        Request.get(uri, nil, headers)
      end

      def prepare_post(uri, body, headers = {})
        Request.post(uri, body, headers)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
poms-2.1.3 lib/poms/api/client.rb
poms-2.1.2.1 lib/poms/api/client.rb
poms-2.1.2 lib/poms/api/client.rb
poms-2.1.1 lib/poms/api/client.rb
poms-2.1.0 lib/poms/api/client.rb
poms-2.0.1 lib/poms/api/client.rb
poms-2.0.0 lib/poms/api/client.rb
poms-2.0.0.b lib/poms/api/client.rb
poms-2.0.0.a lib/poms/api/client.rb