Class: PaysonAPI::V1::Receiver

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



9
10
11
# File 'lib/payson_api/v1/receiver.rb', line 9

def amount
  @amount
end

#emailObject

Returns the value of attribute email.



9
10
11
# File 'lib/payson_api/v1/receiver.rb', line 9

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



9
10
11
# File 'lib/payson_api/v1/receiver.rb', line 9

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



9
10
11
# File 'lib/payson_api/v1/receiver.rb', line 9

def last_name
  @last_name
end

#primaryObject

Returns the value of attribute primary.



9
10
11
# File 'lib/payson_api/v1/receiver.rb', line 9

def primary
  @primary
end

Class Method Details

.parse(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/payson_api/v1/receiver.rb', line 29

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

.to_hash(receivers) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/payson_api/v1/receiver.rb', line 11

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(FORMAT_STRING, index, 'email') => receiver.email,
                    format(FORMAT_STRING, index, 'amount') => receiver.amount,
                    format(FORMAT_STRING, index, 'firstName') => receiver.first_name,
                    format(FORMAT_STRING, index, 'lastName') => receiver.last_name,
                    format(FORMAT_STRING, index, 'primary') => receiver.primary
                  })
      hash[format(FORMAT_STRING, index, 'firstName')] = receiver.first_name if receiver.first_name
      hash[format(FORMAT_STRING, index, 'lastName')] = receiver.last_name if receiver.last_name
    end
  end
end