Module: AdaptivePay::Parser

Defined in:
lib/adaptive_pay/parser.rb

Class Method Summary collapse

Class Method Details

.parse(body) ⇒ Object

parse PayPal NVP message into nested Hash/Array structure

  • treats ‘.’ in key as nesting level

  • treats d+ key suffix as list index

e.g. “namespace.listitem(0).key=value0&namespace.listitem(1).key=value1”

parses to

=> {“listitem” => [{“key” => “value0”, => “value1”]}



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adaptive_pay/parser.rb', line 10

def self.parse(body)
  params = {}
  body.split('&').each do |kvp|
    key, value = kvp.split(/=/, 2)

    # keys may be nested e.g. responseEnvelope.ack
    keys = key.split('.')
    value = URI.unescape(value)

    # simple (non-nested, non-indexed) key
    if keys.size == 1
      params[keys.first] = value
    else
      keys, last_key = keys[0..-2], keys.last
      keys.inject(params) do |a,e|
        # indexed key e.g. refundInfo(0)
        if m = e.match(/^(.*)\((\d+)\)$/)
          key = m[1]
          index = m[2].to_i
          a[key] ||= []
          a[key][index] ||= {}
          a[key][index]
        # non-indexed key
        else
          a[e] ||= {}
          a[e]
        end
      end[last_key] = value
    end
  end
  params
end