chartmogul-ruby 0.1.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +11 -6
  4. data/bin/setup +1 -0
  5. data/changelog.md +4 -0
  6. data/fixtures/vcr_cassettes/{ChartMogul_Import_Customer → ChartMogul_Customer}/API_Interactions/correctly_handles_a_422_response.yml +3 -3
  7. data/fixtures/vcr_cassettes/{ChartMogul_Import_Customer → ChartMogul_Customer}/API_Interactions/correctly_interracts_with_the_API.yml +15 -15
  8. data/fixtures/vcr_cassettes/{ChartMogul_Import_Customer → ChartMogul_Customer}/_find_by_external_id/when_external_id_is_provided/returns_matching_user_if_exists.yml +2 -2
  9. data/fixtures/vcr_cassettes/{ChartMogul_Import_Customer → ChartMogul_Customer}/_find_by_external_id/when_external_id_is_provided/returns_nil_if_customer_does_not_exist.yml +2 -2
  10. data/fixtures/vcr_cassettes/{ChartMogul_Import_CustomerInvoices → ChartMogul_CustomerInvoices}/API_Interactions/correctly_interracts_with_the_API.yml +4 -4
  11. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_interracts_with_the_API.yml +12 -12
  12. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_raises_errors_on_404.yml +3 -3
  13. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_raises_errors_on_422.yml +3 -3
  14. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/retrieves_existing_data_source_matching_uuid.yml +101 -0
  15. data/fixtures/vcr_cassettes/ChartMogul_Enrichment_Customer/API_Interactions/raises_401_if_invalid_credentials.yml +38 -0
  16. data/fixtures/vcr_cassettes/ChartMogul_Ping/pings/and_fails_on_incorrect_credentials.yml +38 -0
  17. data/fixtures/vcr_cassettes/ChartMogul_Ping/pings/when_credentials_correct.yml +38 -0
  18. data/fixtures/vcr_cassettes/{ChartMogul_Import_Plan/API_Interractions → ChartMogul_Plan/API_Interactions}/correctly_handles_a_422_error.yml +3 -3
  19. data/fixtures/vcr_cassettes/{ChartMogul_Import_Plan/API_Interractions/correctly_interracts_with_the_API.yml → ChartMogul_Plan/API_Interactions/correctly_interacts_with_the_API.yml} +9 -9
  20. data/fixtures/vcr_cassettes/{ChartMogul_Import_DataSource/API_Interactions/correctly_interracts_with_the_API.yml → ChartMogul_Plan/API_Interactions/deletes_existing_plan.yml} +61 -52
  21. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/retrieves_existing_plan_by_uuid.yml +151 -0
  22. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/updates_existing_plan.yml +198 -0
  23. data/fixtures/vcr_cassettes/{ChartMogul_Import_Subscription → ChartMogul_Subscription}/API_Interactions/correctly_interracts_with_the_API.yml +4 -4
  24. data/fixtures/vcr_cassettes/{ChartMogul_Import_Transactions_Payment → ChartMogul_Transactions_Payment}/API_Interactions/correctly_interracts_with_the_API.yml +3 -3
  25. data/lib/chartmogul.rb +12 -11
  26. data/lib/chartmogul/api_resource.rb +12 -1
  27. data/lib/chartmogul/customer.rb +41 -0
  28. data/lib/chartmogul/customer_invoices.rb +42 -0
  29. data/lib/chartmogul/data_source.rb +22 -0
  30. data/lib/chartmogul/errors/unauthorized_error.rb +4 -0
  31. data/lib/chartmogul/invoice.rb +62 -0
  32. data/lib/chartmogul/line_items/one_time.rb +23 -0
  33. data/lib/chartmogul/line_items/subscription.rb +29 -0
  34. data/lib/chartmogul/ping.rb +14 -0
  35. data/lib/chartmogul/plan.rb +26 -0
  36. data/lib/chartmogul/subscription.rb +31 -0
  37. data/lib/chartmogul/transactions/payment.rb +24 -0
  38. data/lib/chartmogul/transactions/refund.rb +24 -0
  39. data/lib/chartmogul/version.rb +1 -1
  40. metadata +30 -24
  41. data/fixtures/vcr_cassettes/ChartMogul_Import_DataSource/API_Interactions/correctly_raises_errors_on_404.yml +0 -46
  42. data/fixtures/vcr_cassettes/ChartMogul_Import_DataSource/API_Interactions/correctly_raises_errors_on_422.yml +0 -48
  43. data/lib/chartmogul/import/customer.rb +0 -43
  44. data/lib/chartmogul/import/customer_invoices.rb +0 -44
  45. data/lib/chartmogul/import/data_source.rb +0 -19
  46. data/lib/chartmogul/import/invoice.rb +0 -64
  47. data/lib/chartmogul/import/line_items/one_time.rb +0 -25
  48. data/lib/chartmogul/import/line_items/subscription.rb +0 -31
  49. data/lib/chartmogul/import/plan.rb +0 -21
  50. data/lib/chartmogul/import/subscription.rb +0 -33
  51. data/lib/chartmogul/import/transactions/payment.rb +0 -26
  52. data/lib/chartmogul/import/transactions/refund.rb +0 -26
@@ -0,0 +1,198 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.chartmogul.com/v1/data_sources
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"name":"Another Data Source"}'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.10.0
12
+ Content-Type:
13
+ - application/json
14
+ Authorization:
15
+ - Basic ZHVtbXktdG9rZW46ZHVtbXktdG9rZW4=
16
+ response:
17
+ status:
18
+ code: 201
19
+ message:
20
+ headers:
21
+ server:
22
+ - nginx/1.9.10
23
+ date:
24
+ - Sun, 05 Jun 2016 19:47:08 GMT
25
+ content-type:
26
+ - application/json; charset=utf-8
27
+ transfer-encoding:
28
+ - chunked
29
+ connection:
30
+ - close
31
+ x-frame-options:
32
+ - SAMEORIGIN
33
+ x-xss-protection:
34
+ - 1; mode=block
35
+ x-content-type-options:
36
+ - nosniff
37
+ etag:
38
+ - W/"05335923b74e9c38a5a5fe8e83ac0f79"
39
+ cache-control:
40
+ - max-age=0, private, must-revalidate
41
+ x-request-id:
42
+ - e163c0c6-e937-4427-a0c8-78bd5e3f1711
43
+ x-runtime:
44
+ - '0.509776'
45
+ strict-transport-security:
46
+ - max-age=15768000
47
+ body:
48
+ encoding: UTF-8
49
+ string: '{"uuid":"ds_77eb401d-fff6-43be-94dc-2cbc2e96e31e","name":"Another Data
50
+ Source","created_at":"2016-06-05T19:47:08.020Z","status":"never_imported"}'
51
+ http_version:
52
+ recorded_at: Wed, 04 Jan 2017 02:32:48 GMT
53
+ - request:
54
+ method: post
55
+ uri: https://api.chartmogul.com/v1/plans
56
+ body:
57
+ encoding: UTF-8
58
+ string: '{"data_source_uuid":"ds_77eb401d-fff6-43be-94dc-2cbc2e96e31e","name":"A
59
+ Test Plan","interval_count":1,"interval_unit":"month","external_id":null}'
60
+ headers:
61
+ User-Agent:
62
+ - Faraday v0.9.1
63
+ Content-Type:
64
+ - application/json
65
+ Authorization:
66
+ - Basic ODE2MjZkMWFiMzU5NTIwMzY5MjhjNjJjYmJiNDMyN2M6ODY3MDRmMDdkYzQwZmYzYjYyYzRjY2E5OTYxMjk2MzY=
67
+ response:
68
+ status:
69
+ code: 201
70
+ message:
71
+ headers:
72
+ server:
73
+ - nginx/1.9.10
74
+ date:
75
+ - Sun, 05 Jun 2016 19:47:08 GMT
76
+ content-type:
77
+ - application/json; charset=utf-8
78
+ transfer-encoding:
79
+ - chunked
80
+ connection:
81
+ - close
82
+ x-frame-options:
83
+ - SAMEORIGIN
84
+ x-xss-protection:
85
+ - 1; mode=block
86
+ x-content-type-options:
87
+ - nosniff
88
+ etag:
89
+ - W/"7086a2e8b932cc1ce3b47f8de5fdfdb8"
90
+ cache-control:
91
+ - max-age=0, private, must-revalidate
92
+ x-request-id:
93
+ - 6e97fa02-8bc1-4d2f-8718-8473a58e4254
94
+ x-runtime:
95
+ - '0.120821'
96
+ strict-transport-security:
97
+ - max-age=15768000
98
+ body:
99
+ encoding: UTF-8
100
+ string: '{"uuid":"pl_ba808f0b-4c1c-4824-8705-a6736efa96dc","external_id":null,"name":"A
101
+ Test Plan","interval_count":1,"interval_unit":"month","data_source_uuid":"ds_77eb401d-fff6-43be-94dc-2cbc2e96e31e"}'
102
+ http_version:
103
+ recorded_at: Sun, 05 Jun 2016 19:47:08 GMT
104
+ - request:
105
+ method: get
106
+ uri: https://api.chartmogul.com/v1/plans/pl_5ee8bf93-b0b4-4722-8a17-6b624a3af072
107
+ body:
108
+ encoding: US-ASCII
109
+ string: ''
110
+ headers:
111
+ User-Agent:
112
+ - Faraday v0.9.1
113
+ Authorization:
114
+ - Basic ODE2MjZkMWFiMzU5NTIwMzY5MjhjNjJjYmJiNDMyN2M6ODY3MDRmMDdkYzQwZmYzYjYyYzRjY2E5OTYxMjk2MzY=
115
+ response:
116
+ status:
117
+ code: 200
118
+ message:
119
+ headers:
120
+ server:
121
+ - nginx/1.9.10
122
+ date:
123
+ - Sun, 05 Jun 2016 19:47:08 GMT
124
+ content-type:
125
+ - application/json; charset=utf-8
126
+ transfer-encoding:
127
+ - chunked
128
+ connection:
129
+ - close
130
+ x-frame-options:
131
+ - SAMEORIGIN
132
+ x-xss-protection:
133
+ - 1; mode=block
134
+ x-content-type-options:
135
+ - nosniff
136
+ etag:
137
+ - W/"2122e94312ef92cc077db62910743655"
138
+ cache-control:
139
+ - max-age=0, private, must-revalidate
140
+ x-request-id:
141
+ - 1286c5a2-596a-4bc2-9e24-cfccc393e589
142
+ x-runtime:
143
+ - '0.018019'
144
+ strict-transport-security:
145
+ - max-age=15768000
146
+ body:
147
+ encoding: UTF-8
148
+ string: '{"uuid":"pl_5ee8bf93-b0b4-4722-8a17-6b624a3af072","external_id":null,"name":"A Test Plan","interval_count":2,"interval_unit":"month","data_source_uuid":"ds_77eb401d-fff6-43be-94dc-2cbc2e96e31e"}'
149
+ http_version:
150
+ recorded_at: Wed, 04 Jan 2017 02:32:48 GMT
151
+ - request:
152
+ method: patch
153
+ uri: https://api.chartmogul.com/v1/plans/pl_5ee8bf93-b0b4-4722-8a17-6b624a3af072
154
+ body:
155
+ encoding: US-ASCII
156
+ string: ''
157
+ headers:
158
+ User-Agent:
159
+ - Faraday v0.9.1
160
+ Authorization:
161
+ - Basic ODE2MjZkMWFiMzU5NTIwMzY5MjhjNjJjYmJiNDMyN2M6ODY3MDRmMDdkYzQwZmYzYjYyYzRjY2E5OTYxMjk2MzY=
162
+ response:
163
+ status:
164
+ code: 200
165
+ message:
166
+ headers:
167
+ server:
168
+ - nginx/1.9.10
169
+ date:
170
+ - Sun, 05 Jun 2016 19:47:08 GMT
171
+ content-type:
172
+ - application/json; charset=utf-8
173
+ transfer-encoding:
174
+ - chunked
175
+ connection:
176
+ - close
177
+ x-frame-options:
178
+ - SAMEORIGIN
179
+ x-xss-protection:
180
+ - 1; mode=block
181
+ x-content-type-options:
182
+ - nosniff
183
+ etag:
184
+ - W/"2122e94312ef92cc077db62910743655"
185
+ cache-control:
186
+ - max-age=0, private, must-revalidate
187
+ x-request-id:
188
+ - 1286c5a2-596a-4bc2-9e24-cfccc393e589
189
+ x-runtime:
190
+ - '0.018019'
191
+ strict-transport-security:
192
+ - max-age=15768000
193
+ body:
194
+ encoding: UTF-8
195
+ string: '{"uuid":"pl_5ee8bf93-b0b4-4722-8a17-6b624a3af072","external_id":null,"name":"A Test Plan","interval_count":2,"interval_unit":"month","data_source_uuid":"ds_77eb401d-fff6-43be-94dc-2cbc2e96e31e"}'
196
+ http_version:
197
+ recorded_at: Wed, 04 Jan 2017 02:32:48 GMT
198
+ recorded_with: VCR 3.0.3
@@ -2,7 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: post
5
- uri: https://api.chartmogul.com/v1/import/data_sources
5
+ uri: https://api.chartmogul.com/v1/data_sources
6
6
  body:
7
7
  encoding: UTF-8
8
8
  string: '{"name":"Subscription Test Data Source"}'
@@ -52,7 +52,7 @@ http_interactions:
52
52
  recorded_at: Thu, 23 Jun 2016 20:46:05 GMT
53
53
  - request:
54
54
  method: post
55
- uri: https://api.chartmogul.com/v1/import/customers
55
+ uri: https://api.chartmogul.com/v1/customers
56
56
  body:
57
57
  encoding: UTF-8
58
58
  string: '{"data_source_uuid":"ds_55ab11fb-53e6-4468-aa95-bd582f14cac6","external_id":"test_cus_ext_id","name":"Test
@@ -103,7 +103,7 @@ http_interactions:
103
103
  recorded_at: Thu, 23 Jun 2016 20:46:06 GMT
104
104
  - request:
105
105
  method: post
106
- uri: https://api.chartmogul.com/v1/import/plans
106
+ uri: https://api.chartmogul.com/v1/plans
107
107
  body:
108
108
  encoding: UTF-8
109
109
  string: '{"data_source_uuid":"ds_55ab11fb-53e6-4468-aa95-bd582f14cac6","name":"Test
@@ -298,7 +298,7 @@ http_interactions:
298
298
  recorded_at: Thu, 23 Jun 2016 20:46:07 GMT
299
299
  - request:
300
300
  method: delete
301
- uri: https://api.chartmogul.com/v1/import/data_sources/ds_55ab11fb-53e6-4468-aa95-bd582f14cac6
301
+ uri: https://api.chartmogul.com/v1/data_sources/ds_55ab11fb-53e6-4468-aa95-bd582f14cac6
302
302
  body:
303
303
  encoding: US-ASCII
304
304
  string: ''
@@ -2,7 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: post
5
- uri: https://api.chartmogul.com/v1/import/data_sources
5
+ uri: https://api.chartmogul.com/v1/data_sources
6
6
  body:
7
7
  encoding: UTF-8
8
8
  string: '{"name":"Invoice Payment Test Data Source"}'
@@ -52,7 +52,7 @@ http_interactions:
52
52
  recorded_at: Thu, 23 Jun 2016 14:47:22 GMT
53
53
  - request:
54
54
  method: post
55
- uri: https://api.chartmogul.com/v1/import/customers
55
+ uri: https://api.chartmogul.com/v1/customers
56
56
  body:
57
57
  encoding: UTF-8
58
58
  string: '{"data_source_uuid":"ds_e25656b4-035e-433c-80e7-46c783271e9f","external_id":"test_cus_ext_id","name":"Test
@@ -201,7 +201,7 @@ http_interactions:
201
201
  recorded_at: Thu, 23 Jun 2016 14:47:23 GMT
202
202
  - request:
203
203
  method: delete
204
- uri: https://api.chartmogul.com/v1/import/data_sources/ds_e25656b4-035e-433c-80e7-46c783271e9f
204
+ uri: https://api.chartmogul.com/v1/data_sources/ds_e25656b4-035e-433c-80e7-46c783271e9f
205
205
  body:
206
206
  encoding: US-ASCII
207
207
  string: ''
data/lib/chartmogul.rb CHANGED
@@ -12,6 +12,7 @@ require "chartmogul/errors/not_found_error"
12
12
  require "chartmogul/errors/configuration_error"
13
13
  require "chartmogul/errors/resource_invalid_error"
14
14
  require "chartmogul/errors/schema_invalid_error"
15
+ require "chartmogul/errors/unauthorized_error"
15
16
 
16
17
  require "chartmogul/config_attributes"
17
18
  require "chartmogul/configuration"
@@ -27,19 +28,19 @@ require "chartmogul/api/actions/update"
27
28
  require "chartmogul/api/actions/destroy"
28
29
  require "chartmogul/api/actions/custom"
29
30
 
30
- require "chartmogul/import/line_items/one_time"
31
- require "chartmogul/import/line_items/subscription"
31
+ require "chartmogul/line_items/one_time"
32
+ require "chartmogul/line_items/subscription"
32
33
 
33
- require "chartmogul/import/transactions/payment"
34
- require "chartmogul/import/transactions/refund"
34
+ require "chartmogul/transactions/payment"
35
+ require "chartmogul/transactions/refund"
35
36
 
36
- require "chartmogul/import/invoice"
37
- require "chartmogul/import/customer_invoices"
38
-
39
- require "chartmogul/import/subscription"
40
- require "chartmogul/import/customer"
41
- require "chartmogul/import/data_source"
42
- require "chartmogul/import/plan"
37
+ require "chartmogul/subscription"
38
+ require "chartmogul/invoice"
39
+ require "chartmogul/customer_invoices"
40
+ require "chartmogul/customer"
41
+ require "chartmogul/data_source"
42
+ require "chartmogul/ping"
43
+ require "chartmogul/plan"
43
44
 
44
45
  require "chartmogul/concerns/entries"
45
46
  require "chartmogul/concerns/summary"
@@ -29,12 +29,21 @@ module ChartMogul
29
29
  def self.handling_errors
30
30
  yield
31
31
  rescue Faraday::ClientError => exception
32
+ exception.response ? handle_request_error(exception) : handle_other_error(exception)
33
+ rescue => exception
34
+ handle_other_error(exception)
35
+ end
36
+
37
+ def self.handle_request_error(exception)
32
38
  response = exception.response[:body]
33
39
  http_status = exception.response[:status]
34
40
  case http_status
35
41
  when 400
36
42
  message = "JSON schema validation hasn't passed."
37
43
  raise ChartMogul::SchemaInvalidError.new(message, http_status: 400, response: response)
44
+ when 401
45
+ message = 'No valid API key provided'
46
+ raise ChartMogul::UnauthorizedError.new(message, http_status: 401, response: response)
38
47
  when 403
39
48
  message = "The requested action is forbidden."
40
49
  raise ChartMogul::ForbiddenError.new(message, http_status: 403, response: response)
@@ -48,7 +57,9 @@ module ChartMogul
48
57
  message = "#{resource_name} request error has occurred."
49
58
  raise ChartMogul::ChartMogulError.new(message, http_status: http_status, response: response)
50
59
  end
51
- rescue => exception
60
+ end
61
+
62
+ def self.handle_other_error(exception)
52
63
  raise ChartMogul::ChartMogulError.new(exception.message)
53
64
  end
54
65
 
@@ -0,0 +1,41 @@
1
+ module ChartMogul
2
+ class Customer < APIResource
3
+ set_resource_name 'Customer'
4
+ set_resource_path '/v1/customers'
5
+ set_resource_root_key :customers
6
+
7
+ readonly_attr :uuid
8
+
9
+ writeable_attr :external_id
10
+ writeable_attr :name
11
+ writeable_attr :email
12
+ writeable_attr :company
13
+ writeable_attr :country
14
+ writeable_attr :state
15
+ writeable_attr :city
16
+ writeable_attr :zip
17
+ writeable_attr :data_source_uuid
18
+ writeable_attr :lead_created_at, type: :time
19
+ writeable_attr :free_trial_started_at, type: :time
20
+
21
+ include API::Actions::All
22
+ include API::Actions::Create
23
+ include API::Actions::Destroy
24
+
25
+ def self.find_by_external_id(external_id)
26
+ all(external_id: external_id).first
27
+ end
28
+
29
+ def subscriptions(options = {})
30
+ @subscriptions ||= ChartMogul::Subscription.all(uuid, options)
31
+ end
32
+
33
+ def invoices(options = {})
34
+ @invoices ||= ChartMogul::CustomerInvoices.all(uuid, options)
35
+ end
36
+
37
+ def invoices=(invoices_array)
38
+ @invoices = ChartMogul::CustomerInvoices.new(customer_uuid: uuid, invoices: invoices_array)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ require 'forwardable'
2
+
3
+ module ChartMogul
4
+ class CustomerInvoices < APIResource
5
+ extend Forwardable
6
+ include Enumerable
7
+
8
+ set_resource_name 'Invoices'
9
+ set_resource_path '/v1/import/customers/:customer_uuid/invoices'
10
+
11
+ writeable_attr :invoices, default: []
12
+
13
+ writeable_attr :customer_uuid
14
+
15
+ include API::Actions::All
16
+ include API::Actions::Create
17
+
18
+ def serialize_invoices
19
+ map(&:serialize_for_write)
20
+ end
21
+
22
+ def self.all(customer_uuid, options = {})
23
+ super(options.merge(customer_uuid: customer_uuid))
24
+ end
25
+
26
+ def_delegators :invoices, :each, :[], :<<, :size, :length
27
+
28
+ private
29
+
30
+ def set_invoices(invoices_attributes)
31
+ @invoices = invoices_attributes.map.with_index do |invoice_attributes, index|
32
+ existing_invoice = invoices[index]
33
+
34
+ if existing_invoice
35
+ existing_invoice.assign_all_attributes(invoice_attributes)
36
+ else
37
+ Invoice.new_from_json(invoice_attributes)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ module ChartMogul
2
+ class DataSource < APIResource
3
+ set_resource_name 'Data Source'
4
+ set_resource_path '/v1/data_sources'
5
+ set_resource_root_key :data_sources
6
+
7
+ readonly_attr :uuid
8
+ readonly_attr :status
9
+ readonly_attr :created_at, type: :time
10
+
11
+ writeable_attr :name
12
+
13
+ include API::Actions::All
14
+ include API::Actions::Create
15
+ include API::Actions::Destroy
16
+ include API::Actions::Custom
17
+
18
+ def self.retrieve(uuid)
19
+ custom!(:get, "/v1/data_sources/#{uuid}")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module ChartMogul
2
+ class UnauthorizedError < ChartMogulError
3
+ end
4
+ end