Sha256: ceb542cc34fafbb9540fa8d3b7f8a0e130f14295479dee2cc40c5a7cf3262a2c

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Bs2Api
  module Payment
    class Base
      def call
        response = post_request

        raise Bs2Api::Errors::BadRequest, parse_error(response) if !response.created?
        Bs2Api::Entities::Payment.from_response(response)
      end

      private
        def post_request
          HTTParty.post(url, headers: headers, body: payload.to_json)
        end

        def headers
          {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": "Bearer #{bearer_token}"
          }
        end

        def bearer_token
          Bs2Api::Request::Auth.token
        end

        def parse_error(response)
          hash    = JSON.parse(response.body)
          message = "#{response.code}: "

          if hash.is_a?(Array)
            message << hash[0]["descricao"]
          elsif hash.key?("error_description")
            message << hash["error_description"]
          else 
            message << hash.to_s
          end

          message
        end

        def payload
          raise NoMethodError, "Missing #{__method__} to #{self.class}"
        end
        
        def url
          raise NoMethodError, "Missing #{__method__} to #{self.class}"
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bs2_api-0.2.1 lib/bs2_api/payment/base.rb