Sha256: 6cc1113595ac54cfc8136b1b030033b0dc7039427f39e02878ff072bca34ec73

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

module SynapsePayments
  class Request

    HEADERS = {
      'User-Agent'      => "SynapsePaymentsRubyGem/#{SynapsePayments::VERSION}",
      'X-Ruby-Version'  => RUBY_VERSION,
      'X-Ruby-Platform' => RUBY_PLATFORM
    }

    def initialize(client:, method:, path:, oauth_key: nil, fingerprint: nil, json: nil)
      @client = client
      @method = method
      @path = path
      @oauth_key = oauth_key
      @fingerprint = fingerprint
      @json = json
    end

    def perform
      options_key = @method == :get ? :params : :json
      response = http_client.public_send(@method, "#{@client.api_base}#{@path}", options_key => @json)
      response_body = @client.symbolize_keys!(response.parse)
      fail_or_return_response_body(response.code, response_body)
    end

    private

    def http_client
      headers = HEADERS.merge({
        'X-SP-GATEWAY' => "#{@client.client_id}|#{@client.client_secret}",
        'X-SP-USER' => "#{@oauth_key}|#{@fingerprint}",
        'X-SP-USER-IP' => ''
      })
      HTTP.headers(headers).accept(:json).timeout(@client.timeout_options)
    end

    def fail_or_return_response_body(code, body)
      if code < 200 || code >= 206
        error = SynapsePayments::Error.error_from_response(body, code)
        fail(error)
      end
      body
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
synapse_payments-0.9.0 lib/synapse_payments/request.rb
synapse_payments-0.8.1 lib/synapse_payments/request.rb
synapse_payments-0.8.0 lib/synapse_payments/request.rb
synapse_payments-0.7.0 lib/synapse_payments/request.rb
synapse_payments-0.6.4 lib/synapse_payments/request.rb
synapse_payments-0.6.3 lib/synapse_payments/request.rb