Class: PaysonAPI::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/payson_api/receiver.rb

Constant Summary collapse

FORMAT_STRING =
"receiverList.receiver(%d).%s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, amount, first_name = nil, last_name = nil, primary = true) ⇒ Receiver

Returns a new instance of Receiver.



8
9
10
11
12
13
14
# File 'lib/payson_api/receiver.rb', line 8

def initialize(email, amount, first_name = nil, last_name = nil, primary = true)
  @email = email
  @amount = amount
  @first_name = first_name
  @last_name = last_name
  @primary = primary
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



6
7
8
# File 'lib/payson_api/receiver.rb', line 6

def amount
  @amount
end

#emailObject

Returns the value of attribute email.



6
7
8
# File 'lib/payson_api/receiver.rb', line 6

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



6
7
8
# File 'lib/payson_api/receiver.rb', line 6

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



6
7
8
# File 'lib/payson_api/receiver.rb', line 6

def last_name
  @last_name
end

#primaryObject

Returns the value of attribute primary.



6
7
8
# File 'lib/payson_api/receiver.rb', line 6

def primary
  @primary
end

Class Method Details

.parse(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/payson_api/receiver.rb', line 37

def self.parse(data)
  [].tap do |receivers|
    i = 0
    while data[FORMAT_STRING % [i, 'email']]
      email = CGI.unescape(data[FORMAT_STRING % [i, 'email']].to_s)
      amount = data[FORMAT_STRING % [i, 'amount']]
      first_name = CGI.unescape(data[FORMAT_STRING % [i, 'firstName']].to_s)
      last_name = CGI.unescape(data[FORMAT_STRING % [i, 'lastName']].to_s)
      primary = data[FORMAT_STRING % [i, 'primary']]
      receivers << self.new(email, amount, first_name, last_name, primary)
      i += 1
    end
  end
end

.to_hash(receivers) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/payson_api/receiver.rb', line 16

def self.to_hash(receivers)
  {}.tap do |hash|
    receivers.each_with_index do |receiver, index|
      raise "Invalid receiver" unless receiver.instance_of?(self)
      hash.merge!({
        FORMAT_STRING % [index, 'email'] => receiver.email,
        FORMAT_STRING % [index, 'amount'] => receiver.amount,
        FORMAT_STRING % [index, 'firstName'] => receiver.first_name,
        FORMAT_STRING % [index, 'lastName'] => receiver.last_name,
        FORMAT_STRING % [index, 'primary'] => receiver.primary
      })
      if receiver.first_name
        hash[FORMAT_STRING % [index, 'firstName']] = receiver.first_name
      end
      if receiver.last_name
        hash[FORMAT_STRING % [index, 'lastName']] = receiver.last_name
      end
    end
  end
end