Sha256: 7f7d41ac713267bb48d89a4593cfd26753097721cd2be73aadf0e1869a3165d8

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 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
                  from = [#{from.inspect}].flatten
                  name = from.find {|name| params[name]}
                  value = nil
                  value = params[name] if name
                  value = send("build_#{to}", value) 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

1 entries across 1 versions & 1 rubygems

Version Path
paypal-recurring-0.1.4 lib/paypal/recurring/response/base.rb