Sha256: c0543a350ec561aecdac6b584e07c504ac4a269f7f4c061cbd289c93ec2540ee

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module PayPal
  module Recurring
    module Response
      class Base
        attr_accessor :response

        def self.mapping(options = {})
          options.each do |to, from|
            class_eval <<-RUBY
              def #{to}
                @#{to} ||= begin
                  value = params[:#{from}]
                  value = send("build_#{to}", params[:#{from}]) if respond_to?("build_#{to}", true)
                  value
                end
              end
            RUBY
          end
        end

        mapping(
          :token           => :TOKEN,
          :ack             => :ACK,
          :version         => :VERSION,
          :build           => :BUILD,
          :correlaction_id => :CORRELATIONID,
          :requested_at    => :TIMESTAMP
        )

        def initialize(response = nil)
          @response = response
        end

        def params
          @params ||= CGI.parse(response.body).inject({}) do |buffer, (name, value)|
            buffer.merge(name.to_sym => value.first)
          end
        end

        def errors
          @errors ||= begin
            index = 0
            [].tap do |errors|
              while params[:"L_ERRORCODE#{index}"]
                errors << {
                  :code => params[:"L_ERRORCODE#{index}"],
                  :messages => [
                    params[:"L_SHORTMESSAGE#{index}"],
                    params[:"L_LONGMESSAGE#{index}"]
                  ]
                }

                index += 1
              end
            end
          end
        end

        def success?
          ack == "Success"
        end

        def valid?
          errors.empty? && success?
        end

        private
        def build_requested_at(stamp) # :nodoc:
          Time.parse(stamp)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
paypal-recurring-0.1.3 lib/paypal/recurring/response/base.rb
paypal-recurring-0.1.2 lib/paypal/recurring/response/base.rb
paypal-recurring-0.1.1 lib/paypal/recurring/response/base.rb
paypal-recurring-0.1.0 lib/paypal/recurring/response/base.rb