Sha256: e9b201cda8a30ec73b3f3a989397091b7e55f9299e1de8a9f10214a314e7fa42

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module Erede
  module Services
    class Base
      POST = Net::HTTP::Post
      GET  = Net::HTTP::Get
      PUT  = Net::HTTP::Put

      attr_accessor :store

      def initialize(store)
        self.store = store
      end

      def url
        "#{store.environment.endpoint}/transactions"
      end

      def execute
        raise NotImplementedError
      end

      def send_request(method, body = '')
        uri = URI(url)

        query_string = uri && uri.query && !uri.query.nil? && !uri.query.empty? ? "?#{uri.query}" : ''

        response = Net::HTTP.new(uri.host, uri.port)
                            .tap { |net_http| net_http.use_ssl = uri.scheme == 'https' }
                            .request(
                              method.new(uri.path + query_string, { 'Content-Type': 'application/json', 'Transaction-Response': 'brand-return-opened' })
                                            .tap do |request|
                                              request.body = body
                                              request.basic_auth store.filiation, store.token
                                            end
                            )

        if store.logger
          store.logger.info("RedeRequest - #{method.to_s.split('::').last.upcase} #{uri} - Response #{response.code} #{response.body}")
        end

        Erede::Responses::EredeResponse.new.tap do |erede_response|
          erede_response.code     = response.code
          erede_response.response = JSON.parse(response.body)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
erede-ruby-sdk-3.0.1 lib/erede/services/base.rb
erede-ruby-sdk-3.0.0 lib/erede/services/base.rb