Sha256: 0decba6e799a2dcbe1cfc7820a5df191b1d94cbdac44b5b686f606e8708570f1

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

require "faraday"
require "faraday_middleware"

module PagSeguro
  class API
    delegate :get, :post, :put, :patch, to: :@connection

    attr_reader :options, :logger

    def initialize(options = {})
      @options = options
      @logger = options.delete(:logger)
      @connection ||= Faraday.new api_url do |conn|
        conn.request :json
        conn.response :xml,  content_type: /\bxml$/
        conn.response :json, content_type: /\bjson$/
        conn.response :logger, logger, bodies: true if logger
        conn.adapter :net_http
        conn.params = auth_params
        conn.headers[:accept] = FORMATS[:json]
      end
    end

    def subscriptions
      @subscriptions ||= Subscriptions.new(self)
    end

    def payment_orders
      @payment_orders ||= PaymentOrders.new(self)
    end

    def sessions
      @sessions ||= Sessions.new(self)
    end

    def plans
      @plans ||= Plans.new(self)
    end

    def authorizations
      @authorizations ||= Authorizations.new(self)
    end

    def checkout
      @checkout ||= Checkout.new(self)
    end

    def transactions
      @transactions ||= Transactions.new(self)
    end

    def build_url(source, path, params = {})
      url = URI(send("#{source}_url"))
      url.path = path
      url.query = params.to_query
      url.to_s
    end

    private

    def site_url
      PagSeguro.uris[environment.to_sym][:site]
    end

    def api_url
      PagSeguro.uris[environment.to_sym][:api]
    end

    def environment
      options[:environment] || PagSeguro.environment
    end

    def auth_params
      auth = {}

      if options.key?(:app) || options.key?(:app_id)
        auth[:appId]  = options.fetch :app_id, PagSeguro.app_id
        auth[:appKey] = options.fetch :app_key, PagSeguro.app_key
        auth[:authorizationCode] = options[:authorization_code]
      else
        auth[:token] = options.fetch :token, PagSeguro.token
        auth[:email] = options.fetch :email, PagSeguro.email
      end

      auth.compact
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pagseguro_next-0.4.0 lib/pagseguro/api.rb
pagseguro_next-0.3.0 lib/pagseguro/api.rb
pagseguro_next-0.2.0 lib/pagseguro/api.rb
pagseguro_next-0.1.1 lib/pagseguro/api.rb