Class: Micropayment::Base

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/micropayment-rails/classes/base.rb

Direct Known Subclasses

Address, BankAccount, Customer, Session

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create!(id, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/micropayment-rails/classes/base.rb', line 26

def self.create!(id, params={})
  params.symbolize_keys!
  params.merge!(self::IDENTIFIER => id) if id
  result = Micropayment::Debit.send self::CREATE_METHOD, params
  if result["error"] == "0"
    self.new( valid_attributes( params.merge(result.symbolize_keys) ) )
  else
    raise "#{result["error"]}: #{result["errorMessage"]}"
  end
end

.find(id) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/micropayment-rails/classes/base.rb', line 16

def self.find(id)
  result = Micropayment::Debit.send self::FIND_METHOD, self::IDENTIFIER => id
  case result["error"]
  when "0"
    self.new( valid_attributes(result) )
  else
    raise "#{result["error"]}: #{result["errorMessage"]}"
  end
end

.find_or_create_by_id(id, params = {}) ⇒ Object



37
38
39
40
41
# File 'lib/micropayment-rails/classes/base.rb', line 37

def self.find_or_create_by_id(id, params={})
  params.symbolize_keys!
  obj = (find(id) rescue nil)
  obj ? obj : create( id, params )
end

.valid_attributes(attrs) ⇒ Object



4
5
6
# File 'lib/micropayment-rails/classes/base.rb', line 4

def self.valid_attributes(attrs)
  attrs.reject {|k,v| !self::VALID_ATTRIBUTES.include?(k.to_sym) }
end

Instance Method Details

#idObject



10
11
12
# File 'lib/micropayment-rails/classes/base.rb', line 10

def id
  send self.class::IDENTIFIER
end