chartmogul-ruby 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +10 -10
- data/.rspec +2 -2
- data/.travis.yml +4 -4
- data/Gemfile +4 -4
- data/LICENSE.txt +21 -21
- data/README.md +111 -111
- data/Rakefile +6 -6
- data/bin/console +14 -14
- data/bin/setup +9 -9
- data/changelog.md +27 -27
- data/chartmogul-ruby.gemspec +29 -29
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/adds_custom_attributes.yml +80 -80
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/adds_required_tags.yml +79 -79
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/can_page_through_search_endpoint.yml +43 -43
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/correctly_handles_a_422_response.yml +50 -50
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/correctly_interracts_with_the_API.yml +244 -244
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/merges_customers.yml +185 -185
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/raises_401_if_invalid_credentials.yml +38 -38
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/raises_404_if_no_customers_found.yml +38 -38
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/raises_422_for_update_with_invalid_data.yml +86 -86
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/removes_custom_attributes.yml +80 -80
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/removes_tags.yml +80 -80
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/returns_all_customers_through_list_all_endpoint.yml +47 -47
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/returns_customer_through_retrieve_endpoint.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/returns_right_customers_through_search_endpoint.yml +43 -43
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/updates_custom_attributes.yml +82 -82
- data/fixtures/vcr_cassettes/ChartMogul_Customer/API_Interactions/updates_customer.yml +139 -139
- data/fixtures/vcr_cassettes/ChartMogul_Customer/_find_by_external_id/when_external_id_is_provided/returns_matching_user_if_exists.yml +102 -102
- 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
- data/fixtures/vcr_cassettes/ChartMogul_CustomerInvoices/API_Interactions/correctly_interracts_with_the_API.yml +298 -298
- data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_interracts_with_the_API.yml +189 -189
- data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_raises_errors_on_404.yml +46 -46
- data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/correctly_raises_errors_on_422.yml +48 -48
- data/fixtures/vcr_cassettes/ChartMogul_DataSource/API_Interactions/retrieves_existing_data_source_matching_uuid.yml +101 -101
- data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/deletes_an_invoice.yml +37 -0
- data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/deletes_an_invoice_with_class_method.yml +37 -0
- data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/raises_error_on_deleting_non-existing_invoice.yml +42 -0
- data/fixtures/vcr_cassettes/ChartMogul_Invoice/API_Interactions/returns_all_invoices_through_list_all_endpoint.yml +46 -46
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARPA/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARPA/behaves_like_Metrics_API_resource/should_have_entries.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARR/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_ARR/behaves_like_Metrics_API_resource/should_have_entries.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_ASP/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_ASP/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_Activity/behaves_like_Pageable/should_be_pageable.yml +41 -41
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_Activity/should_have_Activity_entries.yml +41 -41
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_AllKeyMetric/should_have_entries.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerChurnRate/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerChurnRate/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerCount/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_CustomerCount/behaves_like_Metrics_API_resource/should_have_entries.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_LTV/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_LTV/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRR/behaves_like_Summary/should_have_summary.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRR/should_have_entries.yml +42 -42
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRRChurnRate/behaves_like_Metrics_API_resource/behaves_like_Summary/should_have_summary.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_MRRChurnRate/behaves_like_Metrics_API_resource/should_have_entries.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_Subscription/behaves_like_Pageable/should_be_pageable.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Metrics_Subscription/should_have_Subscription_entries.yml +40 -40
- data/fixtures/vcr_cassettes/ChartMogul_Ping/pings/and_fails_on_incorrect_credentials.yml +38 -38
- data/fixtures/vcr_cassettes/ChartMogul_Ping/pings/when_credentials_correct.yml +38 -38
- data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/correctly_handles_a_422_error.yml +51 -51
- data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/correctly_interacts_with_the_API.yml +152 -152
- data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/deletes_existing_plan.yml +198 -198
- data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/retrieves_existing_plan_by_uuid.yml +151 -151
- data/fixtures/vcr_cassettes/ChartMogul_Plan/API_Interactions/updates_existing_plan.yml +198 -198
- data/fixtures/vcr_cassettes/ChartMogul_Subscription/API_Interactions/correctly_interracts_with_the_API.yml +340 -340
- data/fixtures/vcr_cassettes/ChartMogul_Subscription/API_Interactions/has_multiple_aliases.yml +101 -101
- data/fixtures/vcr_cassettes/ChartMogul_Transactions_Payment/API_Interactions/correctly_interracts_with_the_API.yml +243 -243
- data/lib/chartmogul.rb +79 -79
- data/lib/chartmogul/api/actions/all.rb +23 -23
- data/lib/chartmogul/api/actions/create.rb +39 -39
- data/lib/chartmogul/api/actions/custom.rb +37 -37
- data/lib/chartmogul/api/actions/destroy.rb +28 -14
- data/lib/chartmogul/api/actions/update.rb +19 -19
- data/lib/chartmogul/api_resource.rb +68 -68
- data/lib/chartmogul/concerns/entries.rb +39 -39
- data/lib/chartmogul/concerns/pageable.rb +13 -13
- data/lib/chartmogul/concerns/pageable2.rb +12 -12
- data/lib/chartmogul/concerns/summary.rb +20 -20
- data/lib/chartmogul/config_attributes.rb +15 -15
- data/lib/chartmogul/configuration.rb +6 -6
- data/lib/chartmogul/customer.rb +155 -155
- data/lib/chartmogul/customer_invoices.rb +44 -44
- data/lib/chartmogul/data_source.rb +38 -38
- data/lib/chartmogul/enrichment/customer.rb +141 -141
- data/lib/chartmogul/errors/chartmogul_error.rb +19 -19
- data/lib/chartmogul/errors/configuration_error.rb +4 -4
- data/lib/chartmogul/errors/forbidden_error.rb +4 -4
- data/lib/chartmogul/errors/not_found_error.rb +4 -4
- data/lib/chartmogul/errors/resource_invalid_error.rb +4 -4
- data/lib/chartmogul/errors/schema_invalid_error.rb +4 -4
- data/lib/chartmogul/errors/unauthorized_error.rb +4 -4
- data/lib/chartmogul/invoice.rb +83 -78
- data/lib/chartmogul/line_items/one_time.rb +23 -23
- data/lib/chartmogul/line_items/subscription.rb +29 -29
- data/lib/chartmogul/metrics/activity.rb +34 -34
- data/lib/chartmogul/metrics/all_key_metrics.rb +25 -25
- data/lib/chartmogul/metrics/arpa.rb +19 -19
- data/lib/chartmogul/metrics/arr.rb +19 -19
- data/lib/chartmogul/metrics/asp.rb +19 -19
- data/lib/chartmogul/metrics/base.rb +56 -56
- data/lib/chartmogul/metrics/customer_churn_rate.rb +19 -19
- data/lib/chartmogul/metrics/customer_count.rb +19 -19
- data/lib/chartmogul/metrics/ltv.rb +19 -19
- data/lib/chartmogul/metrics/mrr.rb +26 -26
- data/lib/chartmogul/metrics/mrr_churn_rate.rb +19 -19
- data/lib/chartmogul/metrics/subscription.rb +37 -37
- data/lib/chartmogul/object.rb +119 -119
- data/lib/chartmogul/ping.rb +14 -14
- data/lib/chartmogul/plan.rb +41 -41
- data/lib/chartmogul/resource_path.rb +42 -42
- data/lib/chartmogul/subscription.rb +48 -48
- data/lib/chartmogul/summary.rb +7 -7
- data/lib/chartmogul/transactions/payment.rb +24 -24
- data/lib/chartmogul/transactions/refund.rb +24 -24
- data/lib/chartmogul/utils/hash_snake_caser.rb +50 -50
- data/lib/chartmogul/utils/json_parser.rb +11 -11
- data/lib/chartmogul/version.rb +3 -3
- metadata +5 -2
data/lib/chartmogul.rb
CHANGED
@@ -1,79 +1,79 @@
|
|
1
|
-
require 'time'
|
2
|
-
require 'json'
|
3
|
-
require 'faraday'
|
4
|
-
|
5
|
-
require "chartmogul/version"
|
6
|
-
|
7
|
-
require "chartmogul/utils/hash_snake_caser"
|
8
|
-
require "chartmogul/utils/json_parser"
|
9
|
-
|
10
|
-
require "chartmogul/errors/chartmogul_error"
|
11
|
-
require "chartmogul/errors/not_found_error"
|
12
|
-
require "chartmogul/errors/configuration_error"
|
13
|
-
require "chartmogul/errors/resource_invalid_error"
|
14
|
-
require "chartmogul/errors/schema_invalid_error"
|
15
|
-
require "chartmogul/errors/unauthorized_error"
|
16
|
-
|
17
|
-
require "chartmogul/config_attributes"
|
18
|
-
require "chartmogul/configuration"
|
19
|
-
|
20
|
-
require "chartmogul/object"
|
21
|
-
require "chartmogul/resource_path"
|
22
|
-
require "chartmogul/api_resource"
|
23
|
-
require "chartmogul/summary"
|
24
|
-
|
25
|
-
require "chartmogul/api/actions/all"
|
26
|
-
require "chartmogul/api/actions/create"
|
27
|
-
require "chartmogul/api/actions/update"
|
28
|
-
require "chartmogul/api/actions/destroy"
|
29
|
-
require "chartmogul/api/actions/custom"
|
30
|
-
|
31
|
-
require "chartmogul/line_items/one_time"
|
32
|
-
require "chartmogul/line_items/subscription"
|
33
|
-
|
34
|
-
require "chartmogul/transactions/payment"
|
35
|
-
require "chartmogul/transactions/refund"
|
36
|
-
|
37
|
-
require "chartmogul/concerns/entries"
|
38
|
-
require "chartmogul/concerns/summary"
|
39
|
-
require "chartmogul/concerns/pageable"
|
40
|
-
require "chartmogul/concerns/pageable2"
|
41
|
-
|
42
|
-
require "chartmogul/subscription"
|
43
|
-
require "chartmogul/invoice"
|
44
|
-
require "chartmogul/customer_invoices"
|
45
|
-
require "chartmogul/customer"
|
46
|
-
require "chartmogul/data_source"
|
47
|
-
require "chartmogul/ping"
|
48
|
-
require "chartmogul/plan"
|
49
|
-
|
50
|
-
require "chartmogul/metrics/arpa"
|
51
|
-
require "chartmogul/metrics/arr"
|
52
|
-
require "chartmogul/metrics/asp"
|
53
|
-
require "chartmogul/metrics/customer_churn_rate"
|
54
|
-
require "chartmogul/metrics/customer_count"
|
55
|
-
require "chartmogul/metrics/ltv"
|
56
|
-
require "chartmogul/metrics/mrr"
|
57
|
-
require "chartmogul/metrics/mrr_churn_rate"
|
58
|
-
require "chartmogul/metrics/all_key_metrics"
|
59
|
-
require "chartmogul/metrics/base"
|
60
|
-
|
61
|
-
require "chartmogul/metrics/activity"
|
62
|
-
require "chartmogul/metrics/subscription"
|
63
|
-
|
64
|
-
require "chartmogul/enrichment/customer"
|
65
|
-
|
66
|
-
module ChartMogul
|
67
|
-
API_BASE = 'https://api.chartmogul.com'
|
68
|
-
|
69
|
-
class << self
|
70
|
-
extend ConfigAttributes
|
71
|
-
|
72
|
-
def config
|
73
|
-
@config ||= ChartMogul::Configuration.new
|
74
|
-
end
|
75
|
-
|
76
|
-
config_accessor :account_token
|
77
|
-
config_accessor :secret_key
|
78
|
-
end
|
79
|
-
end
|
1
|
+
require 'time'
|
2
|
+
require 'json'
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
require "chartmogul/version"
|
6
|
+
|
7
|
+
require "chartmogul/utils/hash_snake_caser"
|
8
|
+
require "chartmogul/utils/json_parser"
|
9
|
+
|
10
|
+
require "chartmogul/errors/chartmogul_error"
|
11
|
+
require "chartmogul/errors/not_found_error"
|
12
|
+
require "chartmogul/errors/configuration_error"
|
13
|
+
require "chartmogul/errors/resource_invalid_error"
|
14
|
+
require "chartmogul/errors/schema_invalid_error"
|
15
|
+
require "chartmogul/errors/unauthorized_error"
|
16
|
+
|
17
|
+
require "chartmogul/config_attributes"
|
18
|
+
require "chartmogul/configuration"
|
19
|
+
|
20
|
+
require "chartmogul/object"
|
21
|
+
require "chartmogul/resource_path"
|
22
|
+
require "chartmogul/api_resource"
|
23
|
+
require "chartmogul/summary"
|
24
|
+
|
25
|
+
require "chartmogul/api/actions/all"
|
26
|
+
require "chartmogul/api/actions/create"
|
27
|
+
require "chartmogul/api/actions/update"
|
28
|
+
require "chartmogul/api/actions/destroy"
|
29
|
+
require "chartmogul/api/actions/custom"
|
30
|
+
|
31
|
+
require "chartmogul/line_items/one_time"
|
32
|
+
require "chartmogul/line_items/subscription"
|
33
|
+
|
34
|
+
require "chartmogul/transactions/payment"
|
35
|
+
require "chartmogul/transactions/refund"
|
36
|
+
|
37
|
+
require "chartmogul/concerns/entries"
|
38
|
+
require "chartmogul/concerns/summary"
|
39
|
+
require "chartmogul/concerns/pageable"
|
40
|
+
require "chartmogul/concerns/pageable2"
|
41
|
+
|
42
|
+
require "chartmogul/subscription"
|
43
|
+
require "chartmogul/invoice"
|
44
|
+
require "chartmogul/customer_invoices"
|
45
|
+
require "chartmogul/customer"
|
46
|
+
require "chartmogul/data_source"
|
47
|
+
require "chartmogul/ping"
|
48
|
+
require "chartmogul/plan"
|
49
|
+
|
50
|
+
require "chartmogul/metrics/arpa"
|
51
|
+
require "chartmogul/metrics/arr"
|
52
|
+
require "chartmogul/metrics/asp"
|
53
|
+
require "chartmogul/metrics/customer_churn_rate"
|
54
|
+
require "chartmogul/metrics/customer_count"
|
55
|
+
require "chartmogul/metrics/ltv"
|
56
|
+
require "chartmogul/metrics/mrr"
|
57
|
+
require "chartmogul/metrics/mrr_churn_rate"
|
58
|
+
require "chartmogul/metrics/all_key_metrics"
|
59
|
+
require "chartmogul/metrics/base"
|
60
|
+
|
61
|
+
require "chartmogul/metrics/activity"
|
62
|
+
require "chartmogul/metrics/subscription"
|
63
|
+
|
64
|
+
require "chartmogul/enrichment/customer"
|
65
|
+
|
66
|
+
module ChartMogul
|
67
|
+
API_BASE = 'https://api.chartmogul.com'
|
68
|
+
|
69
|
+
class << self
|
70
|
+
extend ConfigAttributes
|
71
|
+
|
72
|
+
def config
|
73
|
+
@config ||= ChartMogul::Configuration.new
|
74
|
+
end
|
75
|
+
|
76
|
+
config_accessor :account_token
|
77
|
+
config_accessor :secret_key
|
78
|
+
end
|
79
|
+
end
|
@@ -1,23 +1,23 @@
|
|
1
|
-
module ChartMogul
|
2
|
-
module API
|
3
|
-
module Actions
|
4
|
-
module All
|
5
|
-
def self.included(base)
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
def all(options = {})
|
11
|
-
resp = handling_errors do
|
12
|
-
connection.get(resource_path.apply_with_get_params(options)) do |req|
|
13
|
-
req.headers['Content-Type'] = 'application/json'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
json = ChartMogul::Utils::JSONParser.parse(resp.body)
|
17
|
-
new_from_json(json)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
1
|
+
module ChartMogul
|
2
|
+
module API
|
3
|
+
module Actions
|
4
|
+
module All
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def all(options = {})
|
11
|
+
resp = handling_errors do
|
12
|
+
connection.get(resource_path.apply_with_get_params(options)) do |req|
|
13
|
+
req.headers['Content-Type'] = 'application/json'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
json = ChartMogul::Utils::JSONParser.parse(resp.body)
|
17
|
+
new_from_json(json)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,39 +1,39 @@
|
|
1
|
-
module ChartMogul
|
2
|
-
module API
|
3
|
-
module Actions
|
4
|
-
module Create
|
5
|
-
def self.included(base)
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
def create!
|
10
|
-
resp = handling_errors do
|
11
|
-
connection.post(resource_path.apply(self.instance_attributes)) do |req|
|
12
|
-
req.headers['Content-Type'] = 'application/json'
|
13
|
-
req.body = JSON.dump(self.serialize_for_write)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
json = ChartMogul::Utils::JSONParser.parse(resp.body)
|
17
|
-
|
18
|
-
assign_all_attributes(json)
|
19
|
-
end
|
20
|
-
|
21
|
-
module ClassMethods
|
22
|
-
def create!(attributes = {})
|
23
|
-
resource = new(attributes)
|
24
|
-
|
25
|
-
resp = handling_errors do
|
26
|
-
connection.post(resource_path.apply(attributes)) do |req|
|
27
|
-
req.headers['Content-Type'] = 'application/json'
|
28
|
-
req.body = JSON.dump(resource.serialize_for_write)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
json = ChartMogul::Utils::JSONParser.parse(resp.body)
|
32
|
-
|
33
|
-
new_from_json(json)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
1
|
+
module ChartMogul
|
2
|
+
module API
|
3
|
+
module Actions
|
4
|
+
module Create
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def create!
|
10
|
+
resp = handling_errors do
|
11
|
+
connection.post(resource_path.apply(self.instance_attributes)) do |req|
|
12
|
+
req.headers['Content-Type'] = 'application/json'
|
13
|
+
req.body = JSON.dump(self.serialize_for_write)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
json = ChartMogul::Utils::JSONParser.parse(resp.body)
|
17
|
+
|
18
|
+
assign_all_attributes(json)
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def create!(attributes = {})
|
23
|
+
resource = new(attributes)
|
24
|
+
|
25
|
+
resp = handling_errors do
|
26
|
+
connection.post(resource_path.apply(attributes)) do |req|
|
27
|
+
req.headers['Content-Type'] = 'application/json'
|
28
|
+
req.body = JSON.dump(resource.serialize_for_write)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
json = ChartMogul::Utils::JSONParser.parse(resp.body)
|
32
|
+
|
33
|
+
new_from_json(json)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,37 +1,37 @@
|
|
1
|
-
module ChartMogul
|
2
|
-
module API
|
3
|
-
module Actions
|
4
|
-
module Custom
|
5
|
-
def self.included(base)
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
def custom_without_assign!(http_method, http_path, body_data = {})
|
10
|
-
self.class.custom_without_assign!(http_method, http_path, body_data)
|
11
|
-
end
|
12
|
-
|
13
|
-
def custom!(http_method, http_path, body_data = {})
|
14
|
-
json = custom_without_assign!(http_method, http_path, body_data)
|
15
|
-
assign_all_attributes(json)
|
16
|
-
end
|
17
|
-
|
18
|
-
module ClassMethods
|
19
|
-
def custom_without_assign!(http_method, http_path, body_data = {})
|
20
|
-
resp = handling_errors do
|
21
|
-
connection.send(http_method, http_path) do |req|
|
22
|
-
req.headers['Content-Type'] = 'application/json'
|
23
|
-
req.body = JSON.dump(body_data)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
ChartMogul::Utils::JSONParser.parse(resp.body)
|
27
|
-
end
|
28
|
-
|
29
|
-
def custom!(http_method, http_path, body_data = {})
|
30
|
-
json = custom_without_assign!(http_method, http_path, body_data)
|
31
|
-
new_from_json(json)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
1
|
+
module ChartMogul
|
2
|
+
module API
|
3
|
+
module Actions
|
4
|
+
module Custom
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def custom_without_assign!(http_method, http_path, body_data = {})
|
10
|
+
self.class.custom_without_assign!(http_method, http_path, body_data)
|
11
|
+
end
|
12
|
+
|
13
|
+
def custom!(http_method, http_path, body_data = {})
|
14
|
+
json = custom_without_assign!(http_method, http_path, body_data)
|
15
|
+
assign_all_attributes(json)
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def custom_without_assign!(http_method, http_path, body_data = {})
|
20
|
+
resp = handling_errors do
|
21
|
+
connection.send(http_method, http_path) do |req|
|
22
|
+
req.headers['Content-Type'] = 'application/json'
|
23
|
+
req.body = JSON.dump(body_data)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
ChartMogul::Utils::JSONParser.parse(resp.body)
|
27
|
+
end
|
28
|
+
|
29
|
+
def custom!(http_method, http_path, body_data = {})
|
30
|
+
json = custom_without_assign!(http_method, http_path, body_data)
|
31
|
+
new_from_json(json)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,14 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
require 'pp'
|
2
|
+
module ChartMogul
|
3
|
+
module API
|
4
|
+
module Actions
|
5
|
+
module Destroy
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def destroy!
|
11
|
+
handling_errors do
|
12
|
+
connection.delete("#{resource_path.apply(self.instance_attributes)}/#{uuid}")
|
13
|
+
end
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def destroy!(options = {})
|
19
|
+
handling_errors do
|
20
|
+
connection.delete("#{resource_path.apply(options)}/#{options[:uuid]}")
|
21
|
+
end
|
22
|
+
true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -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(self.instance_attributes)}/#{uuid}") do |req|
|
8
|
-
req.headers['Content-Type'] = 'application/json'
|
9
|
-
req.body = JSON.dump(self.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(self.instance_attributes)}/#{uuid}") do |req|
|
8
|
+
req.headers['Content-Type'] = 'application/json'
|
9
|
+
req.body = JSON.dump(self.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.new(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.new(exception.message)
|
64
|
+
end
|
65
|
+
|
66
|
+
def_delegators 'self.class', :resource_path, :resource_name, :resource_root_key, :connection, :handling_errors
|
67
|
+
end
|
68
|
+
end
|