Class: Billy::Service

Inherits:
BaseModel show all
Defined in:
lib/billy/service.rb

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#initialize, #method_missing

Constructor Details

This class inherits a constructor from Billy::BaseModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Billy::BaseModel

Class Method Details

.create(atech_identity, service) ⇒ Object



180
181
182
183
# File 'lib/billy/service.rb', line 180

def self.create(atech_identity, service)
  attributes = Billy::Request.request('services/create', :service => service, :atech_identity_key => atech_identity)
  attributes.is_a?(Hash) ? self.new(attributes) : nil
end

.find(field, data) ⇒ Object



171
172
173
174
# File 'lib/billy/service.rb', line 171

def self.find(field, data)
  attributes = Billy::Request.request('services/info', :field => field, :data => data)
  attributes.is_a?(Hash) ? self.new(attributes) : nil
end

.find_for_identity(ati_key) ⇒ Object



176
177
178
# File 'lib/billy/service.rb', line 176

def self.find_for_identity(ati_key)
  Billy::Request.request('services/list_for_identity', :atech_identity_key => ati_key).map { |p| self.new(p) }
end

.listObject

Get a paginated list of all Billy services



5
6
7
# File 'lib/billy/service.rb', line 5

def self.list
  Billy::Request.request('services/list')
end

.validate_label(label) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/billy/service.rb', line 185

def self.validate_label(label)
  result = Billy::Request.request('services/validate_label', :label => label)
  if result.is_a?(Hash)
    result['status'] == 'error' ? result['errors'] : true
  else
    false
  end
end

Instance Method Details

#add_log_entry(message) ⇒ Object



194
195
196
# File 'lib/billy/service.rb', line 194

def add_log_entry(message)
  Billy::Request.request('services/add_log_entry', :id => @attributes['id'], :message => message)
end

#add_newsletter_subscriber(email_address, first_name, consent_type) ⇒ Object



166
167
168
169
# File 'lib/billy/service.rb', line 166

def add_newsletter_subscriber(email_address, first_name, consent_type)
  req = Billy::Request.request('services/add_newsletter_subscriber', :id => @attributes['id'], :email_address => email_address, :first_name => first_name, :consent_type => consent_type)
  req ? true : false
end

#add_stripe_card(*args) ⇒ Object



142
143
144
# File 'lib/billy/service.rb', line 142

def add_stripe_card(*args)
  Billy::Card.add_stripe_card(@attributes['id'], *args)
end

#address?Boolean

Returns:

  • (Boolean)


161
162
163
164
# File 'lib/billy/service.rb', line 161

def address?
  return false if self.address.first.nil? || self.address.last.nil?
  return true
end

#apply_coupon(code) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/billy/service.rb', line 74

def apply_coupon(code)
  if Billy::Request.request('services/apply_coupon', :id => @attributes['id'], :code => code)
    return true
  else
    return false
  end
end

#auto_cancel(reason) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/billy/service.rb', line 98

def auto_cancel(reason)
  request = Billy::Request.request('services/auto_cancel', :id => @attributes['id'], :cancellation_reason => reason)
  if request
    @attributes = request['service']
    true
  else
    false
  end
end

#begin_subscriptionObject



146
147
148
149
# File 'lib/billy/service.rb', line 146

def begin_subscription
  req = Billy::Request.request('services/begin_subscription', :id => @attributes['id'])
  req.is_a?(Hash) ? Billy::Invoice.new(req) : nil
end

#cancel(cancellation_properties = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/billy/service.rb', line 82

def cancel(cancellation_properties = {})
  if Billy::Request.request('services/cancel', cancellation_properties.merge(:id => @attributes['id']))
    true
  else
    false
  end
end

#cardsObject



134
135
136
# File 'lib/billy/service.rb', line 134

def cards
  @cards ||= Billy::Request.request('cards/list', :service => @attributes['id'])
end

#change_addon(addon_permalink, quantity) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/billy/service.rb', line 63

def change_addon(addon_permalink, quantity)
  request = Billy::Request.request('services/change_addon', :id => @attributes['id'], :addon => addon_permalink, :quantity => quantity)

  if request
    @attributes = request['service']
    true
  else
    false
  end
end

#change_label(new_label) ⇒ Object

Change the label associated with a service



41
42
43
44
45
46
47
48
49
# File 'lib/billy/service.rb', line 41

def change_label(new_label)
  request = Billy::Request.request('services/change_label', :id => @attributes['id'], :label => new_label)
  if request
    @attributes = request
    return true
  else
    return false
  end
end

#change_package(new_package) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/billy/service.rb', line 51

def change_package(new_package)
  request = Billy::Request.request('services/change_package', :id => @attributes['id'], :package => new_package)
  if request
    @attributes = request['service']

    return Billy::Invoice.new(request['invoice']) if request['invoice']
    true
  else
    false
  end
end

#change_user(atech_identity_key) ⇒ Object

Change the user associated with this service to the given aTech identity key



30
31
32
33
34
35
36
37
38
# File 'lib/billy/service.rb', line 30

def change_user(atech_identity_key)
  request = Billy::Request.request('services/change_user', :id => @attributes['id'], :atech_identity_key => atech_identity_key)
  if request
    @attributes = request
    return true
  else
    return false
  end
end

#create_invoice(options = {}) ⇒ Object

Create a new invoice for the service and return the new invoice object



109
110
111
112
# File 'lib/billy/service.rb', line 109

def create_invoice(options = {})
  attributes = Billy::Request.request('invoices/create', :service => @attributes['id'], :invoice => options)
  attributes.is_a?(Hash) ? Billy::Invoice.new(attributes) : nil
end

#create_session(start_path = nil, return_url = nil, auto_return_after_payment = false) ⇒ Object

Create a new login session and return a URL which can be used to log the user into the billing application



17
18
19
20
# File 'lib/billy/service.rb', line 17

def create_session(start_path = nil, return_url = nil, auto_return_after_payment = false)
  session = Billy::Request.request('services/create_session', :id => @attributes['id'], :start_path => start_path, :return_url => return_url, :auto_return_after_payment => auto_return_after_payment)
  session ? [Billy.host, 'start', session['token']].join('/') : false
end

#create_signup_token(product, package) ⇒ Object

Create a new signup token for another application



23
24
25
26
27
# File 'lib/billy/service.rb', line 23

def (product, package)
  return false if product.nil? || package.nil?
  token = Billy::Request.request('services/create_signup_token', :id => @attributes['id'], :product => product, :package => package)
  token ? token['url'] : false
end

#default_cardObject



138
139
140
# File 'lib/billy/service.rb', line 138

def default_card
  cards.select { |c| c['default'] == true }.first
end

#immediate_payment(quantity) ⇒ Object



156
157
158
159
# File 'lib/billy/service.rb', line 156

def immediate_payment(quantity)
  req = Billy::Request.request('services/immediate_payment', :id => @attributes['id'], :quantity => quantity)
  req.is_a?(Hash) ? Billy::Invoice.new(req) : nil
end

#invoice(number) ⇒ Object

Return the invoice for this service



121
122
123
124
# File 'lib/billy/service.rb', line 121

def invoice(number)
  attributes = Billy::Request.request('invoices/info', :invoice => number, :service => @attributes['id'])
  attributes.is_a?(Hash) ? Billy::Invoice.new(attributes) : nil
end

#invoicesObject

Return all the invoices for this service



115
116
117
118
# File 'lib/billy/service.rb', line 115

def invoices
  list = Billy::Request.request('invoices/list', :service => @attributes['id'])
  list.is_a?(Array) ? list.map { |i| Billy::Invoice.new(i) } : []
end

#pay_invoice(invoice, card) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/billy/service.rb', line 126

def pay_invoice(invoice, card)
  if Billy::Request.request('cards/pay_invoice', :service => @attributes['id'], :invoice => invoice, :card => card)
    return true
  else
    return false
  end
end

#reactivate_subscriptionObject



151
152
153
154
# File 'lib/billy/service.rb', line 151

def reactivate_subscription
  req = Billy::Request.request('services/reactivate_subscription', :id => @attributes['id'])
  req.is_a?(Hash) ? Billy::Invoice.new(req) : nil
end

#uncancelObject



90
91
92
93
94
95
96
# File 'lib/billy/service.rb', line 90

def uncancel
  if Billy::Request.request('services/uncancel', :id => @attributes['id'])
    true
  else
    false
  end
end

#update(properties) ⇒ Object

Update an existing service by passing new properties



10
11
12
13
# File 'lib/billy/service.rb', line 10

def update(properties)
  @attributes = Billy::Request.request('services/update', :service => @attributes['id'],:properties => properties)
  true
end