Class: Paypal::Api::Sequential

Inherits:
Parameter show all
Includes:
Formatters
Defined in:
lib/paypal_api/support/parameter.rb

Instance Attribute Summary collapse

Attributes inherited from Parameter

#value

Instance Method Summary collapse

Methods included from Formatters

#escape_uri_component

Methods inherited from Parameter

#parameter_parse, #parse

Constructor Details

#initialize(hash = {}, limit = nil, key_proc = nil) ⇒ Sequential

allows you to specify an optional key -> paypal_key proc due to nonstandard key formatting



46
47
48
49
50
51
52
# File 'lib/paypal_api/support/parameter.rb', line 46

def initialize(hash = {}, limit = nil, key_proc = nil)
  @list = []
  @schema = hash
  @required = hash.map{|(k,v)| k if v.class != Optional }.compact
  @key_proc = key_proc if key_proc
  @limit = limit
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



43
44
45
# File 'lib/paypal_api/support/parameter.rb', line 43

def list
  @list
end

Instance Method Details

#cloneObject

necessary because sequential stores request state, need a new list created for each request instance



56
57
58
# File 'lib/paypal_api/support/parameter.rb', line 56

def clone
  return self.class.new(@schema, @limit, @key_proc)
end

#lengthObject



60
61
62
# File 'lib/paypal_api/support/parameter.rb', line 60

def length
  return @list.length
end

#push(hash) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/paypal_api/support/parameter.rb', line 64

def push(hash)
  raise Paypal::InvalidParameter, "missing required parameter for sequential field" unless (@required - hash.keys).empty?
  raise Paypal::InvalidParameter, "field cannot have more than #{@limit} items, #{@list.length} provided" if !@limit.nil? && @list.length == @limit

  hash.each do |k,val|
    type = @schema[k]

    if type.nil?
      hash[k] = val
    elsif type.class == Regexp
      if match = type.match(val)
        hash[k] = match[0]
      else
        raise Paypal::InvalidParameter, "'#{val}' did not match #{type}"
      end
    elsif [Optional, Enum, Coerce, Default].include?(type.class)
      hash[k] = type.parse(val)
    elsif type.class == Proc && type.call(val)
      hash[k] = val
    elsif type == val.class
      hash[k] = val
    else
      raise Paypal::InvalidParameter, "#{type.class} is an invalid parameter specification"
    end
  end

  @list.push(hash)
end

#to_key(symbol, i) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/paypal_api/support/parameter.rb', line 93

def to_key(symbol, i)
  if @key_proc
    return @key_proc.call(symbol, i)
  else
    return symbol.to_s.split("_", 2).map{|s| s.gsub("_", "") }.join("_").gsub(/[^a-z0-9_]/i, "").upcase + "#{i}"
  end
end

#to_query_stringObject



101
102
103
104
105
106
107
108
109
# File 'lib/paypal_api/support/parameter.rb', line 101

def to_query_string
  output = ""
  @list.each_index do |i|
    @list[i].each do |(k,v)|
      output = "#{output}&#{to_key(k, i)}=#{escape_uri_component(@list[i][k])}"
    end
  end
  return output
end