chargebee 2.6.4 → 2.6.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1501 -1488
  3. data/LICENSE +24 -24
  4. data/README.rdoc +41 -41
  5. data/chargebee.gemspec +2 -3
  6. data/lib/chargebee/environment.rb +28 -28
  7. data/lib/chargebee/errors.rb +43 -43
  8. data/lib/chargebee/models/addon.rb +45 -45
  9. data/lib/chargebee/models/address.rb +18 -18
  10. data/lib/chargebee/models/card.rb +32 -32
  11. data/lib/chargebee/models/comment.rb +25 -25
  12. data/lib/chargebee/models/contact.rb +10 -10
  13. data/lib/chargebee/models/coupon.rb +40 -40
  14. data/lib/chargebee/models/coupon_code.rb +25 -25
  15. data/lib/chargebee/models/coupon_set.rb +37 -37
  16. data/lib/chargebee/models/credit_note.rb +77 -77
  17. data/lib/chargebee/models/credit_note_estimate.rb +35 -35
  18. data/lib/chargebee/models/customer.rb +120 -120
  19. data/lib/chargebee/models/download.rb +9 -9
  20. data/lib/chargebee/models/estimate.rb +46 -46
  21. data/lib/chargebee/models/event.rb +45 -45
  22. data/lib/chargebee/models/export.rb +86 -86
  23. data/lib/chargebee/models/gift.rb +42 -42
  24. data/lib/chargebee/models/hosted_page.rb +77 -77
  25. data/lib/chargebee/models/invoice.rb +172 -172
  26. data/lib/chargebee/models/invoice_estimate.rb +35 -35
  27. data/lib/chargebee/models/model.rb +101 -101
  28. data/lib/chargebee/models/order.rb +76 -76
  29. data/lib/chargebee/models/payment_source.rb +75 -71
  30. data/lib/chargebee/models/plan.rb +60 -59
  31. data/lib/chargebee/models/portal_session.rb +30 -30
  32. data/lib/chargebee/models/promotional_credit.rb +30 -30
  33. data/lib/chargebee/models/quote.rb +72 -72
  34. data/lib/chargebee/models/resource_migration.rb +13 -13
  35. data/lib/chargebee/models/site_migration_detail.rb +14 -14
  36. data/lib/chargebee/models/subscription.rb +137 -137
  37. data/lib/chargebee/models/subscription_estimate.rb +13 -13
  38. data/lib/chargebee/models/third_party_payment_method.rb +9 -9
  39. data/lib/chargebee/models/time_machine.rb +46 -46
  40. data/lib/chargebee/models/transaction.rb +62 -62
  41. data/lib/chargebee/models/unbilled_charge.rb +31 -31
  42. data/lib/chargebee/models/virtual_bank_account.rb +30 -26
  43. data/lib/chargebee/request.rb +27 -27
  44. data/lib/chargebee/rest.rb +92 -92
  45. data/lib/chargebee/result.rb +240 -240
  46. data/lib/chargebee.rb +85 -85
  47. data/spec/errors_spec.rb +23 -23
  48. data/spec/sample_response.rb +73 -73
  49. metadata +3 -4
  50. data/lib/chargebee/.DS_Store +0 -0
@@ -1,173 +1,173 @@
1
- module ChargeBee
2
- class Invoice < Model
3
-
4
- class LineItem < Model
5
- attr_accessor :id, :subscription_id, :date_from, :date_to, :unit_amount, :quantity, :amount, :pricing_model, :is_taxed, :tax_amount, :tax_rate, :discount_amount, :item_level_discount_amount, :description, :entity_type, :tax_exempt_reason, :entity_id
6
- end
7
-
8
- class Discount < Model
9
- attr_accessor :amount, :description, :entity_type, :entity_id
10
- end
11
-
12
- class LineItemDiscount < Model
13
- attr_accessor :line_item_id, :discount_type, :coupon_id, :discount_amount
14
- end
15
-
16
- class Tax < Model
17
- attr_accessor :name, :amount, :description
18
- end
19
-
20
- class LineItemTax < Model
21
- attr_accessor :line_item_id, :tax_name, :tax_rate, :tax_amount, :tax_juris_type, :tax_juris_name, :tax_juris_code
22
- end
23
-
24
- class LineItemTier < Model
25
- attr_accessor :line_item_id, :starting_unit, :ending_unit, :quantity_used, :unit_amount
26
- end
27
-
28
- class LinkedPayment < Model
29
- attr_accessor :txn_id, :applied_amount, :applied_at, :txn_status, :txn_date, :txn_amount
30
- end
31
-
32
- class AppliedCredit < Model
33
- attr_accessor :cn_id, :applied_amount, :applied_at, :cn_reason_code, :cn_date, :cn_status
34
- end
35
-
36
- class AdjustmentCreditNote < Model
37
- attr_accessor :cn_id, :cn_reason_code, :cn_date, :cn_total, :cn_status
38
- end
39
-
40
- class IssuedCreditNote < Model
41
- attr_accessor :cn_id, :cn_reason_code, :cn_date, :cn_total, :cn_status
42
- end
43
-
44
- class LinkedOrder < Model
45
- attr_accessor :id, :document_number, :status, :order_type, :reference_id, :fulfillment_status, :batch_id, :created_at
46
- end
47
-
48
- class Note < Model
49
- attr_accessor :entity_type, :note, :entity_id
50
- end
51
-
52
- class ShippingAddress < Model
53
- attr_accessor :first_name, :last_name, :email, :company, :phone, :line1, :line2, :line3, :city, :state_code, :state, :country, :zip, :validation_status
54
- end
55
-
56
- class BillingAddress < Model
57
- attr_accessor :first_name, :last_name, :email, :company, :phone, :line1, :line2, :line3, :city, :state_code, :state, :country, :zip, :validation_status
58
- end
59
-
60
- attr_accessor :id, :po_number, :customer_id, :subscription_id, :recurring, :status, :vat_number,
61
- :price_type, :date, :due_date, :net_term_days, :currency_code, :total, :amount_paid, :amount_adjusted,
62
- :write_off_amount, :credits_applied, :amount_due, :paid_at, :dunning_status, :next_retry_at,
63
- :voided_at, :resource_version, :updated_at, :sub_total, :tax, :first_invoice, :has_advance_charges,
64
- :term_finalized, :is_gifted, :expected_payment_date, :amount_to_collect, :round_off_amount,
65
- :line_items, :discounts, :line_item_discounts, :taxes, :line_item_taxes, :line_item_tiers, :linked_payments,
66
- :applied_credits, :adjustment_credit_notes, :issued_credit_notes, :linked_orders, :notes, :shipping_address,
67
- :billing_address, :deleted
68
-
69
- # OPERATIONS
70
- #-----------
71
-
72
- def self.create(params, env=nil, headers={})
73
- Request.send('post', uri_path("invoices"), params, env, headers)
74
- end
75
-
76
- def self.charge(params, env=nil, headers={})
77
- Request.send('post', uri_path("invoices","charge"), params, env, headers)
78
- end
79
-
80
- def self.charge_addon(params, env=nil, headers={})
81
- Request.send('post', uri_path("invoices","charge_addon"), params, env, headers)
82
- end
83
-
84
- def self.stop_dunning(id, env=nil, headers={})
85
- Request.send('post', uri_path("invoices",id.to_s,"stop_dunning"), {}, env, headers)
86
- end
87
-
88
- def self.import_invoice(params, env=nil, headers={})
89
- Request.send('post', uri_path("invoices","import_invoice"), params, env, headers)
90
- end
91
-
92
- def self.apply_payments(id, params={}, env=nil, headers={})
93
- Request.send('post', uri_path("invoices",id.to_s,"apply_payments"), params, env, headers)
94
- end
95
-
96
- def self.apply_credits(id, params={}, env=nil, headers={})
97
- Request.send('post', uri_path("invoices",id.to_s,"apply_credits"), params, env, headers)
98
- end
99
-
100
- def self.list(params={}, env=nil, headers={})
101
- Request.send_list_request('get', uri_path("invoices"), params, env, headers)
102
- end
103
-
104
- def self.invoices_for_customer(id, params={}, env=nil, headers={})
105
- Request.send('get', uri_path("customers",id.to_s,"invoices"), params, env, headers)
106
- end
107
-
108
- def self.invoices_for_subscription(id, params={}, env=nil, headers={})
109
- Request.send('get', uri_path("subscriptions",id.to_s,"invoices"), params, env, headers)
110
- end
111
-
112
- def self.retrieve(id, env=nil, headers={})
113
- Request.send('get', uri_path("invoices",id.to_s), {}, env, headers)
114
- end
115
-
116
- def self.pdf(id, params={}, env=nil, headers={})
117
- Request.send('post', uri_path("invoices",id.to_s,"pdf"), params, env, headers)
118
- end
119
-
120
- def self.add_charge(id, params, env=nil, headers={})
121
- Request.send('post', uri_path("invoices",id.to_s,"add_charge"), params, env, headers)
122
- end
123
-
124
- def self.add_addon_charge(id, params, env=nil, headers={})
125
- Request.send('post', uri_path("invoices",id.to_s,"add_addon_charge"), params, env, headers)
126
- end
127
-
128
- def self.close(id, env=nil, headers={})
129
- Request.send('post', uri_path("invoices",id.to_s,"close"), {}, env, headers)
130
- end
131
-
132
- def self.collect_payment(id, params={}, env=nil, headers={})
133
- Request.send('post', uri_path("invoices",id.to_s,"collect_payment"), params, env, headers)
134
- end
135
-
136
- def self.record_payment(id, params, env=nil, headers={})
137
- Request.send('post', uri_path("invoices",id.to_s,"record_payment"), params, env, headers)
138
- end
139
-
140
- def self.refund(id, params={}, env=nil, headers={})
141
- Request.send('post', uri_path("invoices",id.to_s,"refund"), params, env, headers)
142
- end
143
-
144
- def self.record_refund(id, params, env=nil, headers={})
145
- Request.send('post', uri_path("invoices",id.to_s,"record_refund"), params, env, headers)
146
- end
147
-
148
- def self.remove_payment(id, params, env=nil, headers={})
149
- Request.send('post', uri_path("invoices",id.to_s,"remove_payment"), params, env, headers)
150
- end
151
-
152
- def self.remove_credit_note(id, params, env=nil, headers={})
153
- Request.send('post', uri_path("invoices",id.to_s,"remove_credit_note"), params, env, headers)
154
- end
155
-
156
- def self.void_invoice(id, params={}, env=nil, headers={})
157
- Request.send('post', uri_path("invoices",id.to_s,"void"), params, env, headers)
158
- end
159
-
160
- def self.write_off(id, params={}, env=nil, headers={})
161
- Request.send('post', uri_path("invoices",id.to_s,"write_off"), params, env, headers)
162
- end
163
-
164
- def self.delete(id, params={}, env=nil, headers={})
165
- Request.send('post', uri_path("invoices",id.to_s,"delete"), params, env, headers)
166
- end
167
-
168
- def self.update_details(id, params={}, env=nil, headers={})
169
- Request.send('post', uri_path("invoices",id.to_s,"update_details"), params, env, headers)
170
- end
171
-
172
- end # ~Invoice
1
+ module ChargeBee
2
+ class Invoice < Model
3
+
4
+ class LineItem < Model
5
+ attr_accessor :id, :subscription_id, :date_from, :date_to, :unit_amount, :quantity, :amount, :pricing_model, :is_taxed, :tax_amount, :tax_rate, :discount_amount, :item_level_discount_amount, :description, :entity_type, :tax_exempt_reason, :entity_id
6
+ end
7
+
8
+ class Discount < Model
9
+ attr_accessor :amount, :description, :entity_type, :entity_id
10
+ end
11
+
12
+ class LineItemDiscount < Model
13
+ attr_accessor :line_item_id, :discount_type, :coupon_id, :discount_amount
14
+ end
15
+
16
+ class Tax < Model
17
+ attr_accessor :name, :amount, :description
18
+ end
19
+
20
+ class LineItemTax < Model
21
+ attr_accessor :line_item_id, :tax_name, :tax_rate, :is_partial_tax_applied, :is_non_compliance_tax, :taxable_amount, :tax_amount, :tax_juris_type, :tax_juris_name, :tax_juris_code
22
+ end
23
+
24
+ class LineItemTier < Model
25
+ attr_accessor :line_item_id, :starting_unit, :ending_unit, :quantity_used, :unit_amount
26
+ end
27
+
28
+ class LinkedPayment < Model
29
+ attr_accessor :txn_id, :applied_amount, :applied_at, :txn_status, :txn_date, :txn_amount
30
+ end
31
+
32
+ class AppliedCredit < Model
33
+ attr_accessor :cn_id, :applied_amount, :applied_at, :cn_reason_code, :cn_date, :cn_status
34
+ end
35
+
36
+ class AdjustmentCreditNote < Model
37
+ attr_accessor :cn_id, :cn_reason_code, :cn_date, :cn_total, :cn_status
38
+ end
39
+
40
+ class IssuedCreditNote < Model
41
+ attr_accessor :cn_id, :cn_reason_code, :cn_date, :cn_total, :cn_status
42
+ end
43
+
44
+ class LinkedOrder < Model
45
+ attr_accessor :id, :document_number, :status, :order_type, :reference_id, :fulfillment_status, :batch_id, :created_at
46
+ end
47
+
48
+ class Note < Model
49
+ attr_accessor :entity_type, :note, :entity_id
50
+ end
51
+
52
+ class ShippingAddress < Model
53
+ attr_accessor :first_name, :last_name, :email, :company, :phone, :line1, :line2, :line3, :city, :state_code, :state, :country, :zip, :validation_status
54
+ end
55
+
56
+ class BillingAddress < Model
57
+ attr_accessor :first_name, :last_name, :email, :company, :phone, :line1, :line2, :line3, :city, :state_code, :state, :country, :zip, :validation_status
58
+ end
59
+
60
+ attr_accessor :id, :po_number, :customer_id, :subscription_id, :recurring, :status, :vat_number,
61
+ :price_type, :date, :due_date, :net_term_days, :currency_code, :total, :amount_paid, :amount_adjusted,
62
+ :write_off_amount, :credits_applied, :amount_due, :paid_at, :dunning_status, :next_retry_at,
63
+ :voided_at, :resource_version, :updated_at, :sub_total, :tax, :first_invoice, :has_advance_charges,
64
+ :term_finalized, :is_gifted, :expected_payment_date, :amount_to_collect, :round_off_amount,
65
+ :line_items, :discounts, :line_item_discounts, :taxes, :line_item_taxes, :line_item_tiers, :linked_payments,
66
+ :applied_credits, :adjustment_credit_notes, :issued_credit_notes, :linked_orders, :notes, :shipping_address,
67
+ :billing_address, :deleted
68
+
69
+ # OPERATIONS
70
+ #-----------
71
+
72
+ def self.create(params, env=nil, headers={})
73
+ Request.send('post', uri_path("invoices"), params, env, headers)
74
+ end
75
+
76
+ def self.charge(params, env=nil, headers={})
77
+ Request.send('post', uri_path("invoices","charge"), params, env, headers)
78
+ end
79
+
80
+ def self.charge_addon(params, env=nil, headers={})
81
+ Request.send('post', uri_path("invoices","charge_addon"), params, env, headers)
82
+ end
83
+
84
+ def self.stop_dunning(id, env=nil, headers={})
85
+ Request.send('post', uri_path("invoices",id.to_s,"stop_dunning"), {}, env, headers)
86
+ end
87
+
88
+ def self.import_invoice(params, env=nil, headers={})
89
+ Request.send('post', uri_path("invoices","import_invoice"), params, env, headers)
90
+ end
91
+
92
+ def self.apply_payments(id, params={}, env=nil, headers={})
93
+ Request.send('post', uri_path("invoices",id.to_s,"apply_payments"), params, env, headers)
94
+ end
95
+
96
+ def self.apply_credits(id, params={}, env=nil, headers={})
97
+ Request.send('post', uri_path("invoices",id.to_s,"apply_credits"), params, env, headers)
98
+ end
99
+
100
+ def self.list(params={}, env=nil, headers={})
101
+ Request.send_list_request('get', uri_path("invoices"), params, env, headers)
102
+ end
103
+
104
+ def self.invoices_for_customer(id, params={}, env=nil, headers={})
105
+ Request.send('get', uri_path("customers",id.to_s,"invoices"), params, env, headers)
106
+ end
107
+
108
+ def self.invoices_for_subscription(id, params={}, env=nil, headers={})
109
+ Request.send('get', uri_path("subscriptions",id.to_s,"invoices"), params, env, headers)
110
+ end
111
+
112
+ def self.retrieve(id, env=nil, headers={})
113
+ Request.send('get', uri_path("invoices",id.to_s), {}, env, headers)
114
+ end
115
+
116
+ def self.pdf(id, params={}, env=nil, headers={})
117
+ Request.send('post', uri_path("invoices",id.to_s,"pdf"), params, env, headers)
118
+ end
119
+
120
+ def self.add_charge(id, params, env=nil, headers={})
121
+ Request.send('post', uri_path("invoices",id.to_s,"add_charge"), params, env, headers)
122
+ end
123
+
124
+ def self.add_addon_charge(id, params, env=nil, headers={})
125
+ Request.send('post', uri_path("invoices",id.to_s,"add_addon_charge"), params, env, headers)
126
+ end
127
+
128
+ def self.close(id, env=nil, headers={})
129
+ Request.send('post', uri_path("invoices",id.to_s,"close"), {}, env, headers)
130
+ end
131
+
132
+ def self.collect_payment(id, params={}, env=nil, headers={})
133
+ Request.send('post', uri_path("invoices",id.to_s,"collect_payment"), params, env, headers)
134
+ end
135
+
136
+ def self.record_payment(id, params, env=nil, headers={})
137
+ Request.send('post', uri_path("invoices",id.to_s,"record_payment"), params, env, headers)
138
+ end
139
+
140
+ def self.refund(id, params={}, env=nil, headers={})
141
+ Request.send('post', uri_path("invoices",id.to_s,"refund"), params, env, headers)
142
+ end
143
+
144
+ def self.record_refund(id, params, env=nil, headers={})
145
+ Request.send('post', uri_path("invoices",id.to_s,"record_refund"), params, env, headers)
146
+ end
147
+
148
+ def self.remove_payment(id, params, env=nil, headers={})
149
+ Request.send('post', uri_path("invoices",id.to_s,"remove_payment"), params, env, headers)
150
+ end
151
+
152
+ def self.remove_credit_note(id, params, env=nil, headers={})
153
+ Request.send('post', uri_path("invoices",id.to_s,"remove_credit_note"), params, env, headers)
154
+ end
155
+
156
+ def self.void_invoice(id, params={}, env=nil, headers={})
157
+ Request.send('post', uri_path("invoices",id.to_s,"void"), params, env, headers)
158
+ end
159
+
160
+ def self.write_off(id, params={}, env=nil, headers={})
161
+ Request.send('post', uri_path("invoices",id.to_s,"write_off"), params, env, headers)
162
+ end
163
+
164
+ def self.delete(id, params={}, env=nil, headers={})
165
+ Request.send('post', uri_path("invoices",id.to_s,"delete"), params, env, headers)
166
+ end
167
+
168
+ def self.update_details(id, params={}, env=nil, headers={})
169
+ Request.send('post', uri_path("invoices",id.to_s,"update_details"), params, env, headers)
170
+ end
171
+
172
+ end # ~Invoice
173
173
  end # ~ChargeBee
@@ -1,36 +1,36 @@
1
- module ChargeBee
2
- class InvoiceEstimate < Model
3
-
4
- class LineItem < Model
5
- attr_accessor :id, :subscription_id, :date_from, :date_to, :unit_amount, :quantity, :amount, :pricing_model, :is_taxed, :tax_amount, :tax_rate, :discount_amount, :item_level_discount_amount, :description, :entity_type, :tax_exempt_reason, :entity_id
6
- end
7
-
8
- class Discount < Model
9
- attr_accessor :amount, :description, :entity_type, :entity_id
10
- end
11
-
12
- class Tax < Model
13
- attr_accessor :name, :amount, :description
14
- end
15
-
16
- class LineItemTax < Model
17
- attr_accessor :line_item_id, :tax_name, :tax_rate, :tax_amount, :tax_juris_type, :tax_juris_name, :tax_juris_code
18
- end
19
-
20
- class LineItemTier < Model
21
- attr_accessor :line_item_id, :starting_unit, :ending_unit, :quantity_used, :unit_amount
22
- end
23
-
24
- class LineItemDiscount < Model
25
- attr_accessor :line_item_id, :discount_type, :coupon_id, :discount_amount
26
- end
27
-
28
- attr_accessor :recurring, :price_type, :currency_code, :sub_total, :total, :credits_applied,
29
- :amount_paid, :amount_due, :line_items, :discounts, :taxes, :line_item_taxes, :line_item_tiers,
30
- :line_item_discounts, :round_off_amount
31
-
32
- # OPERATIONS
33
- #-----------
34
-
35
- end # ~InvoiceEstimate
1
+ module ChargeBee
2
+ class InvoiceEstimate < Model
3
+
4
+ class LineItem < Model
5
+ attr_accessor :id, :subscription_id, :date_from, :date_to, :unit_amount, :quantity, :amount, :pricing_model, :is_taxed, :tax_amount, :tax_rate, :discount_amount, :item_level_discount_amount, :description, :entity_type, :tax_exempt_reason, :entity_id
6
+ end
7
+
8
+ class Discount < Model
9
+ attr_accessor :amount, :description, :entity_type, :entity_id
10
+ end
11
+
12
+ class Tax < Model
13
+ attr_accessor :name, :amount, :description
14
+ end
15
+
16
+ class LineItemTax < Model
17
+ attr_accessor :line_item_id, :tax_name, :tax_rate, :is_partial_tax_applied, :is_non_compliance_tax, :taxable_amount, :tax_amount, :tax_juris_type, :tax_juris_name, :tax_juris_code
18
+ end
19
+
20
+ class LineItemTier < Model
21
+ attr_accessor :line_item_id, :starting_unit, :ending_unit, :quantity_used, :unit_amount
22
+ end
23
+
24
+ class LineItemDiscount < Model
25
+ attr_accessor :line_item_id, :discount_type, :coupon_id, :discount_amount
26
+ end
27
+
28
+ attr_accessor :recurring, :price_type, :currency_code, :sub_total, :total, :credits_applied,
29
+ :amount_paid, :amount_due, :line_items, :discounts, :taxes, :line_item_taxes, :line_item_tiers,
30
+ :line_item_discounts, :round_off_amount
31
+
32
+ # OPERATIONS
33
+ #-----------
34
+
35
+ end # ~InvoiceEstimate
36
36
  end # ~ChargeBee
@@ -1,101 +1,101 @@
1
- require 'uri'
2
-
3
- module ChargeBee
4
- class Model
5
-
6
- def initialize(values, sub_types={}, dependant_types={})
7
- @values = values
8
- @sub_types = sub_types
9
- @dependant_types = dependant_types
10
- end
11
-
12
- def to_s(*args)
13
- JSON.pretty_generate(@values)
14
- end
15
-
16
- def inspect()
17
- "#<#{self.class}:0x#{self.object_id.to_s(16)} > JSON: " + JSON.pretty_generate(@values)
18
- end
19
-
20
- def load(values)
21
- instance_eval do
22
- values.each do |k, v|
23
- set_val = nil
24
- case v
25
- when Hash && (@dependant_types[k] != nil)
26
- next
27
- when Hash
28
- set_val = (@sub_types[k] != nil) ? @sub_types[k].construct(v) : v
29
- when Array
30
- if(@sub_types[k] != nil)
31
- set_val = v.map { |item| @sub_types[k].construct(item)}
32
- else
33
- set_val = v
34
- end
35
- else
36
- set_val = v
37
- end
38
- instance_variable_set("@#{k}", set_val)
39
- end
40
- end
41
- end
42
-
43
- def method_missing(m, *args, &block)
44
- if(@values.has_key?(m))
45
- return @values[m]
46
- elsif(m[0,3] == "cf_") # All the custom fields start with prefix cf_.
47
- return nil
48
- end
49
- puts "There's no method called #{m} #{args} here -- please try again."
50
- puts @values
51
- end
52
-
53
- def self.uri_path(*paths)
54
- url = ""
55
- for path in paths
56
- if(path.nil? || path.strip.length < 1)
57
- raise "Id is empty or nil"
58
- end
59
- url = "#{url}/#{URI.encode(path.strip)}"
60
- end
61
- return url
62
- end
63
-
64
- def self.construct(values, sub_types = {}, dependant_types = {})
65
- if(values != nil)
66
- obj = self.new(values, sub_types, dependant_types)
67
- obj.load(values)
68
- obj
69
- end
70
- end
71
-
72
- def init_dependant(obj, type, sub_types = {})
73
- instance_eval do
74
- if(obj[type] != nil)
75
- case obj
76
- when Hash
77
- if(@dependant_types[type] != nil)
78
- dependant_obj = @dependant_types[type].construct(obj[type], sub_types)
79
- instance_variable_set("@#{type}", dependant_obj)
80
- end
81
- end
82
- end
83
- end
84
- end
85
-
86
- def init_dependant_list(obj, type, sub_types = {})
87
- instance_eval do
88
- if(obj[type] != nil)
89
- case obj[type]
90
- when Array
91
- if(@dependant_types[type] != nil)
92
- set_val = obj[type].map {|dt| @dependant_types[type].construct(dt, sub_types)}
93
- instance_variable_set("@#{type}", set_val)
94
- end
95
- end
96
- end
97
- end
98
- end
99
-
100
- end
101
- end
1
+ require 'uri'
2
+
3
+ module ChargeBee
4
+ class Model
5
+
6
+ def initialize(values, sub_types={}, dependant_types={})
7
+ @values = values
8
+ @sub_types = sub_types
9
+ @dependant_types = dependant_types
10
+ end
11
+
12
+ def to_s(*args)
13
+ JSON.pretty_generate(@values)
14
+ end
15
+
16
+ def inspect()
17
+ "#<#{self.class}:0x#{self.object_id.to_s(16)} > JSON: " + JSON.pretty_generate(@values)
18
+ end
19
+
20
+ def load(values)
21
+ instance_eval do
22
+ values.each do |k, v|
23
+ set_val = nil
24
+ case v
25
+ when Hash && (@dependant_types[k] != nil)
26
+ next
27
+ when Hash
28
+ set_val = (@sub_types[k] != nil) ? @sub_types[k].construct(v) : v
29
+ when Array
30
+ if(@sub_types[k] != nil)
31
+ set_val = v.map { |item| @sub_types[k].construct(item)}
32
+ else
33
+ set_val = v
34
+ end
35
+ else
36
+ set_val = v
37
+ end
38
+ instance_variable_set("@#{k}", set_val)
39
+ end
40
+ end
41
+ end
42
+
43
+ def method_missing(m, *args, &block)
44
+ if(@values.has_key?(m))
45
+ return @values[m]
46
+ elsif(m[0,3] == "cf_") # All the custom fields start with prefix cf_.
47
+ return nil
48
+ end
49
+ puts "There's no method called #{m} #{args} here -- please try again."
50
+ puts @values
51
+ end
52
+
53
+ def self.uri_path(*paths)
54
+ url = ""
55
+ for path in paths
56
+ if(path.nil? || path.strip.length < 1)
57
+ raise "Id is empty or nil"
58
+ end
59
+ url = "#{url}/#{URI.encode(path.strip)}"
60
+ end
61
+ return url
62
+ end
63
+
64
+ def self.construct(values, sub_types = {}, dependant_types = {})
65
+ if(values != nil)
66
+ obj = self.new(values, sub_types, dependant_types)
67
+ obj.load(values)
68
+ obj
69
+ end
70
+ end
71
+
72
+ def init_dependant(obj, type, sub_types = {})
73
+ instance_eval do
74
+ if(obj[type] != nil)
75
+ case obj
76
+ when Hash
77
+ if(@dependant_types[type] != nil)
78
+ dependant_obj = @dependant_types[type].construct(obj[type], sub_types)
79
+ instance_variable_set("@#{type}", dependant_obj)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ def init_dependant_list(obj, type, sub_types = {})
87
+ instance_eval do
88
+ if(obj[type] != nil)
89
+ case obj[type]
90
+ when Array
91
+ if(@dependant_types[type] != nil)
92
+ set_val = obj[type].map {|dt| @dependant_types[type].construct(dt, sub_types)}
93
+ instance_variable_set("@#{type}", set_val)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ end
101
+ end