Class: IGE_ISB_API::Callback::Withdrawal

Inherits:
Object
  • Object
show all
Defined in:
lib/ige_isb_api/callback.rb

Class Method Summary collapse

Class Method Details

.parse(raw_data) ⇒ Object

Parses incoming Withdrawl data of the form: [

{
  

]



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ige_isb_api/callback.rb', line 97

def self.parse(raw_data)
  raise ValidationException, "No data to parse" if raw_data.nil? || raw_data.empty?
  begin
    data = JSON.parse(raw_data)
    result = data.map do |wd|
      player = wd['player']
      IGE_ISB_API::Validation.test('player', player, 'username')
      withdrawal_reference = wd['withdrawal_reference'].to_i
      IGE_ISB_API::Validation.test('withdrawal_reference', withdrawal_reference, 'int')
      approved = wd['approved']
      unless !!approved == approved
        if approved.is_a?(String) && ( approved == 'true' || approved == 'false')
          approved = (approved == 'true')
        else
          raise ArgumentError, "Expected 'approved' to be true or false"
        end
      end
      checksum = wd['checksum']
      expected = (Digest::SHA256.new << "#{player}#{withdrawal_reference}#{IGE_ISB_API::Constants::SECRET_KEY}").to_s
      raise ValidationException, "Invalid Checksum" unless checksum == expected
      operator = wd['operator']
      IGE_ISB_API::Validation.test('operator', operator, 'username')
    
      {
        player: player,
        withdrawal_reference: withdrawal_reference,
        approved: approved,
        operator: operator
      }
    end
    return result
  rescue ArgumentError => ae
    raise ValidationException, ae.message
  rescue JSON::ParserError => jpe
    raise ValidationException, "Invalid JSON format in supplied data: #{jpe.message}"
  end
end