Class: Pin::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pin-payments/base.rb

Direct Known Subclasses

Card, Charge, Customer, Refund

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
# File 'lib/pin-payments/base.rb', line 5

def initialize(attributes = {})
  attributes.each do |name, value|
    if name == 'card' # TODO: this should be generalised (has_one relationship i suppose)
      self.card = Card.new value
    else
      send("#{name}=", value) if respond_to? "#{name}="
    end
  end
end

Class Method Details

.all(options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/pin-payments/base.rb', line 26

def all(options = {})
  options = {path: api_path, page: 1}.merge(options)
  paging = {page: options[:page]} unless options[:page] == 1
  build_collection_from_response(authenticated_get(options[:path], paging))
end

.create(options, path = api_path) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/pin-payments/base.rb', line 17

def create(options, path = api_path)
  response = authenticated_post(path, options)
  if response.code == 201 # object created
    build_instance_from_response(response)
  else
    raise Pin::APIError.new(response)
  end
end

.find(token, options = {}) ⇒ Object



39
40
41
42
# File 'lib/pin-payments/base.rb', line 39

def find(token, options = {})
  options = options.merge(path: api_path)
  build_instance_from_response(authenticated_get("#{options[:path]}/#{token}"))
end

.first(options = {}) ⇒ Object



32
33
34
# File 'lib/pin-payments/base.rb', line 32

def first(options = {})
  all(options).first
end

.last(options = {}) ⇒ Object



35
36
37
# File 'lib/pin-payments/base.rb', line 35

def last(options = {})
  all(options).last
end