chartmogul-ruby 1.1.5 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +10 -10
  3. data/.rspec +2 -2
  4. data/.travis.yml +9 -4
  5. data/Gemfile +4 -4
  6. data/LICENSE.txt +21 -21
  7. data/README.md +111 -111
  8. data/Rakefile +6 -6
  9. data/bin/console +14 -14
  10. data/bin/setup +9 -9
  11. data/changelog.md +27 -27
  12. data/chartmogul-ruby.gemspec +30 -30
  13. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/adds_custom_attributes.yml +80 -80
  14. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/adds_required_tags.yml +79 -79
  15. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/can_page_through_search_endpoint.yml +43 -43
  16. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/correctly_handles_a_422_response.yml +50 -50
  17. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/correctly_interracts_with_the_API.yml +244 -244
  18. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/merges_customers.yml +185 -185
  19. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/raises_401_if_invalid_credentials.yml +38 -38
  20. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/raises_404_if_no_customers_found.yml +38 -38
  21. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/raises_422_for_update_with_invalid_data.yml +86 -86
  22. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/removes_custom_attributes.yml +80 -80
  23. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/removes_tags.yml +80 -80
  24. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/returns_all_customers_through_list_all_endpoint.yml +47 -47
  25. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/returns_customer_through_retrieve_endpoint.yml +42 -42
  26. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/returns_right_customers_through_search_endpoint.yml +43 -43
  27. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/updates_custom_attributes.yml +82 -82
  28. data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/updates_customer.yml +139 -139
  29. data/fixtures/vcr_cassettes/ChartMogul_Customer/_find_by_external_id/when_external_id_is_provided/returns_matching_user_if_exists.yml +102 -102
  30. data/fixtures/vcr_cassettes/ChartMogul_Customer/_find_by_external_id/when_external_id_is_provided/returns_nil_if_customer_does_not_exist.yml +101 -101
  31. data/fixtures/vcr_cassettes/ChartMogul_CustomerInvoices/API_Interactions/correctly_interracts_with_the_API.yml +298 -298
  32. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_interracts_with_the_API.yml +189 -189
  33. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_raises_errors_on_404.yml +46 -46
  34. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_raises_errors_on_422.yml +48 -48
  35. data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/retrieves_existing_data_source_matching_uuid.yml +101 -101
  36. data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/deletes_an_invoice.yml +37 -37
  37. data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/deletes_an_invoice_with_class_method.yml +37 -37
  38. data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/raises_error_on_deleting_non-existing_invoice.yml +42 -42
  39. data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/retrieves_existing_invoice_by_uuid.yml +50 -50
  40. data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/returns_all_invoices_through_list_all_endpoint.yml +46 -46
  41. data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARPA/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +42 -42
  42. data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARPA/behaves_like_Metrics_API_resource/should_have_entries.yml +42 -42
  43. data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARR/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +42 -42
  44. data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARR/behaves_like_Metrics_API_resource/should_have_entries.yml +42 -42
  45. data/fixtures/vcr_cassettes/ChartMogul_Metrics_ASP/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
  46. data/fixtures/vcr_cassettes/ChartMogul_Metrics_ASP/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
  47. data/fixtures/vcr_cassettes/ChartMogul_Metrics_Activity/behaves_like_Pageable/should_be_pageable.yml +41 -41
  48. data/fixtures/vcr_cassettes/ChartMogul_Metrics_Activity/should_have_Activity_entries.yml +41 -41
  49. data/fixtures/vcr_cassettes/ChartMogul_Metrics_AllKeyMetric/should_have_entries.yml +42 -42
  50. data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerChurnRate/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
  51. data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerChurnRate/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
  52. data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerCount/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +42 -42
  53. data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerCount/behaves_like_Metrics_API_resource/should_have_entries.yml +42 -42
  54. data/fixtures/vcr_cassettes/ChartMogul_Metrics_LTV/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
  55. data/fixtures/vcr_cassettes/ChartMogul_Metrics_LTV/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
  56. data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRR/behaves_like_Summary/should_have_summary.yml +42 -42
  57. data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRR/should_have_entries.yml +42 -42
  58. data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRRChurnRate/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
  59. data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRRChurnRate/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
  60. data/fixtures/vcr_cassettes/ChartMogul_Metrics_Subscription/behaves_like_Pageable/should_be_pageable.yml +40 -40
  61. data/fixtures/vcr_cassettes/ChartMogul_Metrics_Subscription/should_have_Subscription_entries.yml +40 -40
  62. data/fixtures/vcr_cassettes/ChartMogul_Ping/pings/and_fails_on_incorrect_credentials.yml +38 -38
  63. data/fixtures/vcr_cassettes/ChartMogul_Ping/pings/when_credentials_correct.yml +38 -38
  64. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/correctly_handles_a_422_error.yml +51 -51
  65. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/correctly_interacts_with_the_API.yml +152 -152
  66. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/deletes_existing_plan.yml +198 -198
  67. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/retrieves_existing_plan_by_uuid.yml +151 -151
  68. data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/updates_existing_plan.yml +198 -198
  69. data/fixtures/vcr_cassettes/ChartMogul_Subscription/API_Interactions/correctly_interracts_with_the_API.yml +340 -340
  70. data/fixtures/vcr_cassettes/ChartMogul_Subscription/API_Interactions/has_multiple_aliases.yml +101 -101
  71. data/fixtures/vcr_cassettes/ChartMogul_Transactions_Payment/API_Interactions/correctly_interracts_with_the_API.yml +243 -243
  72. data/lib/chartmogul.rb +81 -81
  73. data/lib/chartmogul/api/actions/all.rb +23 -23
  74. data/lib/chartmogul/api/actions/create.rb +39 -39
  75. data/lib/chartmogul/api/actions/custom.rb +37 -37
  76. data/lib/chartmogul/api/actions/destroy.rb +27 -27
  77. data/lib/chartmogul/api/actions/retrieve.rb +23 -23
  78. data/lib/chartmogul/api/actions/update.rb +19 -19
  79. data/lib/chartmogul/api_resource.rb +68 -68
  80. data/lib/chartmogul/concerns/entries.rb +37 -37
  81. data/lib/chartmogul/concerns/pageable.rb +13 -13
  82. data/lib/chartmogul/concerns/pageable2.rb +12 -12
  83. data/lib/chartmogul/concerns/summary.rb +19 -19
  84. data/lib/chartmogul/config_attributes.rb +15 -15
  85. data/lib/chartmogul/configuration.rb +6 -6
  86. data/lib/chartmogul/customer.rb +156 -156
  87. data/lib/chartmogul/customer_invoices.rb +44 -44
  88. data/lib/chartmogul/data_source.rb +34 -34
  89. data/lib/chartmogul/enrichment/customer.rb +140 -140
  90. data/lib/chartmogul/errors/chartmogul_error.rb +19 -19
  91. data/lib/chartmogul/errors/configuration_error.rb +4 -4
  92. data/lib/chartmogul/errors/forbidden_error.rb +4 -4
  93. data/lib/chartmogul/errors/not_found_error.rb +4 -4
  94. data/lib/chartmogul/errors/resource_invalid_error.rb +4 -4
  95. data/lib/chartmogul/errors/schema_invalid_error.rb +4 -4
  96. data/lib/chartmogul/errors/unauthorized_error.rb +4 -4
  97. data/lib/chartmogul/invoice.rb +86 -86
  98. data/lib/chartmogul/line_items/one_time.rb +24 -23
  99. data/lib/chartmogul/line_items/subscription.rb +30 -29
  100. data/lib/chartmogul/metrics/activity.rb +33 -33
  101. data/lib/chartmogul/metrics/all_key_metrics.rb +24 -24
  102. data/lib/chartmogul/metrics/arpa.rb +18 -18
  103. data/lib/chartmogul/metrics/arr.rb +18 -18
  104. data/lib/chartmogul/metrics/asp.rb +18 -18
  105. data/lib/chartmogul/metrics/base.rb +54 -54
  106. data/lib/chartmogul/metrics/customer_churn_rate.rb +18 -18
  107. data/lib/chartmogul/metrics/customer_count.rb +18 -18
  108. data/lib/chartmogul/metrics/ltv.rb +18 -18
  109. data/lib/chartmogul/metrics/mrr.rb +26 -26
  110. data/lib/chartmogul/metrics/mrr_churn_rate.rb +18 -18
  111. data/lib/chartmogul/metrics/subscription.rb +37 -37
  112. data/lib/chartmogul/object.rb +119 -119
  113. data/lib/chartmogul/ping.rb +14 -14
  114. data/lib/chartmogul/plan.rb +37 -37
  115. data/lib/chartmogul/resource_path.rb +42 -42
  116. data/lib/chartmogul/subscription.rb +47 -47
  117. data/lib/chartmogul/summary.rb +7 -7
  118. data/lib/chartmogul/transactions/payment.rb +24 -24
  119. data/lib/chartmogul/transactions/refund.rb +24 -24
  120. data/lib/chartmogul/utils/hash_snake_caser.rb +49 -49
  121. data/lib/chartmogul/utils/json_parser.rb +10 -10
  122. data/lib/chartmogul/version.rb +3 -3
  123. metadata +3 -4
@@ -1,19 +1,19 @@
1
- module ChartMogul
2
- module API
3
- module Actions
4
- module Update
5
- def update!
6
- resp = handling_errors do
7
- connection.patch("#{resource_path.apply(instance_attributes)}/#{uuid}") do |req|
8
- req.headers['Content-Type'] = 'application/json'
9
- req.body = JSON.dump(serialize_for_write)
10
- end
11
- end
12
- json = ChartMogul::Utils::JSONParser.parse(resp.body)
13
-
14
- assign_all_attributes(json)
15
- end
16
- end
17
- end
18
- end
19
- end
1
+ module ChartMogul
2
+ module API
3
+ module Actions
4
+ module Update
5
+ def update!
6
+ resp = handling_errors do
7
+ connection.patch("#{resource_path.apply(instance_attributes)}/#{uuid}") do |req|
8
+ req.headers['Content-Type'] = 'application/json'
9
+ req.body = JSON.dump(serialize_for_write)
10
+ end
11
+ end
12
+ json = ChartMogul::Utils::JSONParser.parse(resp.body)
13
+
14
+ assign_all_attributes(json)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,68 +1,68 @@
1
- require 'forwardable'
2
-
3
- module ChartMogul
4
- class APIResource < ChartMogul::Object
5
- extend Forwardable
6
-
7
- class << self; attr_reader :resource_path, :resource_name, :resource_root_key end
8
-
9
- def self.set_resource_path(path)
10
- @resource_path = ChartMogul::ResourcePath.new(path)
11
- end
12
-
13
- def self.set_resource_name(name)
14
- @resource_name = name
15
- end
16
-
17
- def self.set_resource_root_key(root_key)
18
- @resource_root_key = root_key
19
- end
20
-
21
- def self.connection
22
- @connection ||= Faraday.new(url: ChartMogul::API_BASE) do |faraday|
23
- faraday.use Faraday::Request::BasicAuthentication, ChartMogul.account_token, ChartMogul.secret_key
24
- faraday.use Faraday::Response::RaiseError
25
- faraday.use Faraday::Adapter::NetHttp
26
- end
27
- end
28
-
29
- def self.handling_errors
30
- yield
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)
38
- response = exception.response[:body]
39
- http_status = exception.response[:status]
40
- case http_status
41
- when 400
42
- message = "JSON schema validation hasn't passed."
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)
47
- when 403
48
- message = 'The requested action is forbidden.'
49
- raise ChartMogul::ForbiddenError.new(message, http_status: 403, response: response)
50
- when 404
51
- message = "The requested #{resource_name} could not be found."
52
- raise ChartMogul::NotFoundError.new(message, http_status: 404, response: response)
53
- when 422
54
- message = "The #{resource_name} could not be created or updated."
55
- raise ChartMogul::ResourceInvalidError.new(message, http_status: 422, response: response)
56
- else
57
- message = "#{resource_name} request error has occurred."
58
- raise ChartMogul::ChartMogulError.new(message, http_status: http_status, response: response)
59
- end
60
- end
61
-
62
- def self.handle_other_error(exception)
63
- raise ChartMogul::ChartMogulError, exception.message
64
- end
65
-
66
- def_delegators 'self.class', :resource_path, :resource_name, :resource_root_key, :connection, :handling_errors
67
- end
68
- end
1
+ require 'forwardable'
2
+
3
+ module ChartMogul
4
+ class APIResource < ChartMogul::Object
5
+ extend Forwardable
6
+
7
+ class << self; attr_reader :resource_path, :resource_name, :resource_root_key end
8
+
9
+ def self.set_resource_path(path)
10
+ @resource_path = ChartMogul::ResourcePath.new(path)
11
+ end
12
+
13
+ def self.set_resource_name(name)
14
+ @resource_name = name
15
+ end
16
+
17
+ def self.set_resource_root_key(root_key)
18
+ @resource_root_key = root_key
19
+ end
20
+
21
+ def self.connection
22
+ @connection ||= Faraday.new(url: ChartMogul::API_BASE) do |faraday|
23
+ faraday.use Faraday::Request::BasicAuthentication, ChartMogul.account_token, ChartMogul.secret_key
24
+ faraday.use Faraday::Response::RaiseError
25
+ faraday.use Faraday::Adapter::NetHttp
26
+ end
27
+ end
28
+
29
+ def self.handling_errors
30
+ yield
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)
38
+ response = exception.response[:body]
39
+ http_status = exception.response[:status]
40
+ case http_status
41
+ when 400
42
+ message = "JSON schema validation hasn't passed."
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)
47
+ when 403
48
+ message = 'The requested action is forbidden.'
49
+ raise ChartMogul::ForbiddenError.new(message, http_status: 403, response: response)
50
+ when 404
51
+ message = "The requested #{resource_name} could not be found."
52
+ raise ChartMogul::NotFoundError.new(message, http_status: 404, response: response)
53
+ when 422
54
+ message = "The #{resource_name} could not be created or updated."
55
+ raise ChartMogul::ResourceInvalidError.new(message, http_status: 422, response: response)
56
+ else
57
+ message = "#{resource_name} request error has occurred."
58
+ raise ChartMogul::ChartMogulError.new(message, http_status: http_status, response: response)
59
+ end
60
+ end
61
+
62
+ def self.handle_other_error(exception)
63
+ raise ChartMogul::ChartMogulError, exception.message
64
+ end
65
+
66
+ def_delegators 'self.class', :resource_path, :resource_name, :resource_root_key, :connection, :handling_errors
67
+ end
68
+ end
@@ -1,37 +1,37 @@
1
- module ChartMogul
2
- module Concerns
3
- module Entries
4
- def self.included(base)
5
- base.extend ClassMethods
6
-
7
- base.instance_eval do
8
- @resource_root_key = :entries if @resource_root_key.nil?
9
- readonly_attr @resource_root_key, default: []
10
-
11
- include API::Actions::All
12
-
13
- include Enumerable
14
- def_delegators @resource_root_key, :each, :[], :<<, :size, :length, :empty?, :first
15
-
16
- resource_root_key = @resource_root_key.to_s
17
- base.send :define_method, 'set_' + resource_root_key do |entries|
18
- objects = entries.map do |entity|
19
- self.class.get_entry_class.new_from_json(entity)
20
- end
21
- instance_variable_set "@#{resource_root_key}", objects
22
- end
23
- end
24
- end
25
-
26
- module ClassMethods
27
- def set_entry_class(klass)
28
- instance_variable_set('@entry_class', klass)
29
- end
30
-
31
- def get_entry_class
32
- instance_variable_get('@entry_class')
33
- end
34
- end
35
- end
36
- end
37
- end
1
+ module ChartMogul
2
+ module Concerns
3
+ module Entries
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+
7
+ base.instance_eval do
8
+ @resource_root_key = :entries if @resource_root_key.nil?
9
+ readonly_attr @resource_root_key, default: []
10
+
11
+ include API::Actions::All
12
+
13
+ include Enumerable
14
+ def_delegators @resource_root_key, :each, :[], :<<, :size, :length, :empty?, :first
15
+
16
+ resource_root_key = @resource_root_key.to_s
17
+ base.send :define_method, 'set_' + resource_root_key do |entries|
18
+ objects = entries.map do |entity|
19
+ self.class.get_entry_class.new_from_json(entity)
20
+ end
21
+ instance_variable_set "@#{resource_root_key}", objects
22
+ end
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ def set_entry_class(klass)
28
+ instance_variable_set('@entry_class', klass)
29
+ end
30
+
31
+ def get_entry_class
32
+ instance_variable_get('@entry_class')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,13 +1,13 @@
1
- module ChartMogul
2
- module Concerns
3
- module Pageable
4
- def self.included(base)
5
- base.instance_eval do
6
- readonly_attr :has_more
7
- readonly_attr :per_page
8
- readonly_attr :page
9
- end
10
- end
11
- end
12
- end
13
- end
1
+ module ChartMogul
2
+ module Concerns
3
+ module Pageable
4
+ def self.included(base)
5
+ base.instance_eval do
6
+ readonly_attr :has_more
7
+ readonly_attr :per_page
8
+ readonly_attr :page
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,12 +1,12 @@
1
- module ChartMogul
2
- module Concerns
3
- module Pageable2
4
- def self.included(base)
5
- base.instance_eval do
6
- readonly_attr :current_page
7
- readonly_attr :total_pages
8
- end
9
- end
10
- end
11
- end
12
- end
1
+ module ChartMogul
2
+ module Concerns
3
+ module Pageable2
4
+ def self.included(base)
5
+ base.instance_eval do
6
+ readonly_attr :current_page
7
+ readonly_attr :total_pages
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,19 +1,19 @@
1
- module ChartMogul
2
- module Concerns
3
- module Summary
4
- def self.included(base)
5
- base.send :prepend, InstanceMethods
6
-
7
- base.instance_eval do
8
- readonly_attr :summary
9
- end
10
- end
11
-
12
- module InstanceMethods
13
- def set_summary(summary_attributes)
14
- @summary = ChartMogul::Summary.new_from_json(summary_attributes)
15
- end
16
- end
17
- end
18
- end
19
- end
1
+ module ChartMogul
2
+ module Concerns
3
+ module Summary
4
+ def self.included(base)
5
+ base.send :prepend, InstanceMethods
6
+
7
+ base.instance_eval do
8
+ readonly_attr :summary
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ def set_summary(summary_attributes)
14
+ @summary = ChartMogul::Summary.new_from_json(summary_attributes)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,15 +1,15 @@
1
- module ChartMogul
2
- module ConfigAttributes
3
- def config_accessor(attribute)
4
- define_method(attribute) do
5
- attr = config.send(attribute)
6
- raise ConfigurationError, "Configuration for #{attribute} not set" if attr.nil?
7
- attr
8
- end
9
-
10
- define_method("#{attribute}=") do |val|
11
- config.send("#{attribute}=", val)
12
- end
13
- end
14
- end
15
- end
1
+ module ChartMogul
2
+ module ConfigAttributes
3
+ def config_accessor(attribute)
4
+ define_method(attribute) do
5
+ attr = config.send(attribute)
6
+ raise ConfigurationError, "Configuration for #{attribute} not set" if attr.nil?
7
+ attr
8
+ end
9
+
10
+ define_method("#{attribute}=") do |val|
11
+ config.send("#{attribute}=", val)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,6 @@
1
- module ChartMogul
2
- class Configuration
3
- attr_accessor :account_token
4
- attr_accessor :secret_key
5
- end
6
- end
1
+ module ChartMogul
2
+ class Configuration
3
+ attr_accessor :account_token
4
+ attr_accessor :secret_key
5
+ end
6
+ end
@@ -1,156 +1,156 @@
1
- module ChartMogul
2
- class Customer < APIResource
3
- set_resource_name 'Customer'
4
- set_resource_path '/v1/customers'
5
-
6
- readonly_attr :uuid
7
- readonly_attr :id
8
-
9
- readonly_attr :status
10
- readonly_attr :customer_since, type: :time
11
- readonly_attr :address
12
- readonly_attr :mrr
13
- readonly_attr :arr
14
- readonly_attr :billing_system_url
15
- readonly_attr :chartmogul_url
16
- readonly_attr :billing_system_type
17
- readonly_attr :currency
18
- readonly_attr :currency_sign
19
- readonly_attr :data_source_uuids
20
- readonly_attr :external_ids
21
-
22
- writeable_attr :attributes
23
- writeable_attr :external_id
24
- writeable_attr :name
25
- writeable_attr :email
26
- writeable_attr :company
27
- writeable_attr :country
28
- writeable_attr :state
29
- writeable_attr :city
30
- writeable_attr :zip
31
- writeable_attr :data_source_uuid
32
- writeable_attr :lead_created_at, type: :time
33
- writeable_attr :free_trial_started_at, type: :time
34
-
35
- include API::Actions::Create
36
- include API::Actions::Custom
37
- include API::Actions::Destroy
38
- include API::Actions::Retrieve
39
- include API::Actions::Update
40
-
41
- def self.all(options = {})
42
- Customers.all(options)
43
- end
44
-
45
- def self.search(email, options = {})
46
- Customers.search(email, options)
47
- end
48
-
49
- def self.find_by_external_id(external_id)
50
- all(external_id: external_id).first
51
- end
52
-
53
- def subscriptions(options = {})
54
- @subscriptions ||= ChartMogul::Subscription.all(uuid, options)
55
- end
56
-
57
- def invoices(options = {})
58
- @invoices ||= ChartMogul::CustomerInvoices.all(uuid, options)
59
- end
60
-
61
- def invoices=(invoices_array)
62
- @invoices = ChartMogul::CustomerInvoices.new(customer_uuid: uuid, invoices: invoices_array)
63
- end
64
-
65
- # Enrichment
66
- def tags
67
- @attributes[:tags]
68
- end
69
-
70
- def custom_attributes
71
- @attributes[:custom]
72
- end
73
-
74
- def add_tags!(*tags)
75
- self.tags = custom_without_assign!(:post,
76
- "/v1/customers/#{uuid}/attributes/tags",
77
- tags: tags)[:tags]
78
- end
79
-
80
- def remove_tags!(*tags)
81
- self.tags = custom_without_assign!(:delete,
82
- "/v1/customers/#{uuid}/attributes/tags",
83
- tags: tags)[:tags]
84
- end
85
-
86
- def add_custom_attributes!(*custom_attrs)
87
- self.custom_attributes = custom_without_assign!(:post,
88
- "/v1/customers/#{uuid}/attributes/custom",
89
- custom: custom_attrs)[:custom]
90
- end
91
-
92
- def update_custom_attributes!(custom_attrs = {})
93
- self.custom_attributes = custom_without_assign!(:put,
94
- "/v1/customers/#{uuid}/attributes/custom",
95
- custom: custom_attrs)[:custom]
96
- end
97
-
98
- def remove_custom_attributes!(*custom_attrs)
99
- self.custom_attributes = custom_without_assign!(:delete,
100
- "/v1/customers/#{uuid}/attributes/custom",
101
- custom: custom_attrs)[:custom]
102
- end
103
-
104
- def merge_into!(other_customer)
105
- options = {
106
- from: { customer_uuid: uuid },
107
- into: { customer_uuid: other_customer.uuid }
108
- }
109
- custom!(:post, '/v1/customers/merges', options)
110
- true
111
- end
112
-
113
- private
114
-
115
- def tags=(tags)
116
- @attributes[:tags] = tags
117
- end
118
-
119
- def custom_attributes=(custom_attributes = {})
120
- @attributes[:custom] = typecast_custom_attributes(custom_attributes)
121
- end
122
-
123
- def set_attributes(attributes_attributes)
124
- @attributes = attributes_attributes
125
- @attributes[:custom] = typecast_custom_attributes(attributes_attributes[:custom])
126
- end
127
-
128
- def typecast_custom_attributes(custom_attributes)
129
- return {} unless custom_attributes
130
- custom_attributes.each_with_object({}) do |(key, value), hash|
131
- hash[key] = value.instance_of?(String) ? (begin
132
- Time.parse(value)
133
- rescue
134
- value
135
- end) : value
136
- end
137
- end
138
- end
139
-
140
- class Customers < APIResource
141
- set_resource_name 'Customers'
142
- set_resource_path '/v1/customers'
143
-
144
- include Concerns::Entries
145
- include API::Actions::Custom
146
- include Concerns::Pageable
147
- include Concerns::Pageable2
148
-
149
- set_entry_class Customer
150
-
151
- def self.search(email, options = {})
152
- path = ChartMogul::ResourcePath.new('/v1/customers/search')
153
- custom!(:get, path.apply_with_get_params(options.merge(email: email)))
154
- end
155
- end
156
- end
1
+ module ChartMogul
2
+ class Customer < APIResource
3
+ set_resource_name 'Customer'
4
+ set_resource_path '/v1/customers'
5
+
6
+ readonly_attr :uuid
7
+ readonly_attr :id
8
+
9
+ readonly_attr :status
10
+ readonly_attr :customer_since, type: :time
11
+ readonly_attr :address
12
+ readonly_attr :mrr
13
+ readonly_attr :arr
14
+ readonly_attr :billing_system_url
15
+ readonly_attr :chartmogul_url
16
+ readonly_attr :billing_system_type
17
+ readonly_attr :currency
18
+ readonly_attr :currency_sign
19
+ readonly_attr :data_source_uuids
20
+ readonly_attr :external_ids
21
+
22
+ writeable_attr :attributes
23
+ writeable_attr :external_id
24
+ writeable_attr :name
25
+ writeable_attr :email
26
+ writeable_attr :company
27
+ writeable_attr :country
28
+ writeable_attr :state
29
+ writeable_attr :city
30
+ writeable_attr :zip
31
+ writeable_attr :data_source_uuid
32
+ writeable_attr :lead_created_at, type: :time
33
+ writeable_attr :free_trial_started_at, type: :time
34
+
35
+ include API::Actions::Create
36
+ include API::Actions::Custom
37
+ include API::Actions::Destroy
38
+ include API::Actions::Retrieve
39
+ include API::Actions::Update
40
+
41
+ def self.all(options = {})
42
+ Customers.all(options)
43
+ end
44
+
45
+ def self.search(email, options = {})
46
+ Customers.search(email, options)
47
+ end
48
+
49
+ def self.find_by_external_id(external_id)
50
+ all(external_id: external_id).first
51
+ end
52
+
53
+ def subscriptions(options = {})
54
+ @subscriptions ||= ChartMogul::Subscription.all(uuid, options)
55
+ end
56
+
57
+ def invoices(options = {})
58
+ @invoices ||= ChartMogul::CustomerInvoices.all(uuid, options)
59
+ end
60
+
61
+ def invoices=(invoices_array)
62
+ @invoices = ChartMogul::CustomerInvoices.new(customer_uuid: uuid, invoices: invoices_array)
63
+ end
64
+
65
+ # Enrichment
66
+ def tags
67
+ @attributes[:tags]
68
+ end
69
+
70
+ def custom_attributes
71
+ @attributes[:custom]
72
+ end
73
+
74
+ def add_tags!(*tags)
75
+ self.tags = custom_without_assign!(:post,
76
+ "/v1/customers/#{uuid}/attributes/tags",
77
+ tags: tags)[:tags]
78
+ end
79
+
80
+ def remove_tags!(*tags)
81
+ self.tags = custom_without_assign!(:delete,
82
+ "/v1/customers/#{uuid}/attributes/tags",
83
+ tags: tags)[:tags]
84
+ end
85
+
86
+ def add_custom_attributes!(*custom_attrs)
87
+ self.custom_attributes = custom_without_assign!(:post,
88
+ "/v1/customers/#{uuid}/attributes/custom",
89
+ custom: custom_attrs)[:custom]
90
+ end
91
+
92
+ def update_custom_attributes!(custom_attrs = {})
93
+ self.custom_attributes = custom_without_assign!(:put,
94
+ "/v1/customers/#{uuid}/attributes/custom",
95
+ custom: custom_attrs)[:custom]
96
+ end
97
+
98
+ def remove_custom_attributes!(*custom_attrs)
99
+ self.custom_attributes = custom_without_assign!(:delete,
100
+ "/v1/customers/#{uuid}/attributes/custom",
101
+ custom: custom_attrs)[:custom]
102
+ end
103
+
104
+ def merge_into!(other_customer)
105
+ options = {
106
+ from: { customer_uuid: uuid },
107
+ into: { customer_uuid: other_customer.uuid }
108
+ }
109
+ custom!(:post, '/v1/customers/merges', options)
110
+ true
111
+ end
112
+
113
+ private
114
+
115
+ def tags=(tags)
116
+ @attributes[:tags] = tags
117
+ end
118
+
119
+ def custom_attributes=(custom_attributes = {})
120
+ @attributes[:custom] = typecast_custom_attributes(custom_attributes)
121
+ end
122
+
123
+ def set_attributes(attributes_attributes)
124
+ @attributes = attributes_attributes
125
+ @attributes[:custom] = typecast_custom_attributes(attributes_attributes[:custom])
126
+ end
127
+
128
+ def typecast_custom_attributes(custom_attributes)
129
+ return {} unless custom_attributes
130
+ custom_attributes.each_with_object({}) do |(key, value), hash|
131
+ hash[key] = value.instance_of?(String) ? (begin
132
+ Time.parse(value)
133
+ rescue
134
+ value
135
+ end) : value
136
+ end
137
+ end
138
+ end
139
+
140
+ class Customers < APIResource
141
+ set_resource_name 'Customers'
142
+ set_resource_path '/v1/customers'
143
+
144
+ include Concerns::Entries
145
+ include API::Actions::Custom
146
+ include Concerns::Pageable
147
+ include Concerns::Pageable2
148
+
149
+ set_entry_class Customer
150
+
151
+ def self.search(email, options = {})
152
+ path = ChartMogul::ResourcePath.new('/v1/customers/search')
153
+ custom!(:get, path.apply_with_get_params(options.merge(email: email)))
154
+ end
155
+ end
156
+ end