Class: Chargify::Customer

Inherits:
Base
  • Object
show all
Defined in:
lib/chargify/customer.rb

Instance Attribute Summary

Attributes inherited from Base

#errors

Class Method Summary collapse

Methods inherited from Base

api_request, #api_request, #attributes=, #initialize

Constructor Details

This class inherits a constructor from Chargify::Base

Class Method Details

.all(options = {}) ⇒ Object

options: page



7
8
9
10
# File 'lib/chargify/customer.rb', line 7

def all(options={})
  customers = api_request(:get, '/customers.json', :query => options)
  customers.map{|c| Hashie::Mash.new c['customer']}
end

.create(info = {}) ⇒ Object



57
58
59
60
61
# File 'lib/chargify/customer.rb', line 57

def create(info={})
  create!(info)
rescue Chargify::Error::Base => e
  return false
end

.create!(info = {}) ⇒ Object

  • first_name (Required)

  • last_name (Required)

  • email (Required)

  • organization (Optional) Company/Organization name

  • reference (Optional, but encouraged) The unique identifier used within your own application for this customer



50
51
52
53
54
55
# File 'lib/chargify/customer.rb', line 50

def create!(info={})
  result = api_request(:post, "/customers.json", :body => {:customer => info})
  created = true if result.code == 201
  response = Hashie::Mash.new(result)
  (response.customer || response).update(:success? => created)
end

.find(id) ⇒ Object



20
21
22
23
24
# File 'lib/chargify/customer.rb', line 20

def find(id)
  find!(id)
rescue Chargify::Error::Base => e
  return nil
end

.find!(id) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/chargify/customer.rb', line 12

def find!(id)
  return all if id == :all

  request = api_request(:get, "/customers/#{id}.json")
  response = Hashie::Mash.new(request)
  response
end

.find_or_create(info = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/chargify/customer.rb', line 63

def find_or_create(info={})
  info.symbolize_keys!
  self.lookup!(info[:reference])
rescue Chargify::Error::NotFound => e
  self.create!(info)
end

.lookup(reference_id) ⇒ Object



37
38
39
40
41
# File 'lib/chargify/customer.rb', line 37

def lookup(reference_id)
  lookup!(reference_id)
rescue Chargify::Error::Base => e
  return nil
end

.lookup!(reference_id) ⇒ Object

end



31
32
33
34
35
# File 'lib/chargify/customer.rb', line 31

def lookup!(reference_id)
  request = api_request(:get, "/customers/lookup.json?reference=#{reference_id}")
  response = Hashie::Mash.new(request)
  response.customer
end

.subscriptions(id) ⇒ Object



93
94
95
96
# File 'lib/chargify/customer.rb', line 93

def subscriptions(id)
  subscriptions = api_request(:get, "/customers/#{id}/subscriptions.json")
  subscriptions.map{|s| Hashie::Mash.new s['subscription']}
end

.update(info = {}) ⇒ Object



87
88
89
90
91
# File 'lib/chargify/customer.rb', line 87

def update(info={})
  update!(info)
rescue Chargify::Error::Base => e
  return false
end

.update!(info = {}) ⇒ Object

  • first_name (Required)

  • last_name (Required)

  • email (Required)

  • organization (Optional) Company/Organization name

  • reference (Optional, but encouraged) The unique identifier used within your own application for this customer



77
78
79
80
81
82
83
84
85
# File 'lib/chargify/customer.rb', line 77

def update!(info={})
  info.stringify_keys!
  chargify_id = info.delete('id')
  result = api_request(:put, "/customers/#{chargify_id}.json", :body => {:customer => info})

  response = Hashie::Mash.new(result)
  return response.customer unless response.customer.to_a.empty?
  response
end