tribune_recurly_api 1.0.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90bc9314bc9a69973358b2865c2f75c5d4ce83f17393304deec39e8dae4dc2d4
4
- data.tar.gz: 44ba860e9260009fb54fc277fc7418b5da1325d4d304dff30f7fe247b544450c
3
+ metadata.gz: 710191b3144933ba14175b9f79f8b913c1838daf314103e03b2ce3c2a01d6c7b
4
+ data.tar.gz: 1c590a30db886709b49db2628a532de7da49f1bcc2774274e992a486095821e8
5
5
  SHA512:
6
- metadata.gz: 8a526bdd23c232b57019d4f5bd8356781cbfd696efd32fef9baefabda89d3b469261dc8ad1e3a313548f269d517e29f8f81a95e5a30a53fd285bfdae20f3adba
7
- data.tar.gz: 5bfccbbc08e4fdb9edf9a972dade04955d92094af277546eff4698a55e67220e8fa1e2e0dbe8b60e05b4677175f80818c225284da9da8726d532d7c064debcb8
6
+ metadata.gz: b5357d11ad9b95582f81582fb3aec20759c3bc4fc5072e8608908d10a7fcb07f01d96e837235300bbdf6df2de3cf58eb38a6634fc37e3334f59a9374061b8993
7
+ data.tar.gz: cd48953eb36937260352b95ec6c58076204e261a604b33da7812eebc487de3fb9f2788012d6a33cf0690f47974100e2b16591a51a41e9a279d9142dbbb1c164a
@@ -9,9 +9,11 @@ module RecurlyApi
9
9
  :cache_expires_in,
10
10
  :cache_key # key name to read/write cache, # default value is caller method_name
11
11
 
12
- # default settings for caching, every GET request is cached by default with default expiry_time 5.seconds
12
+ # default settings for caching, every GET request is cached by default with default expiry_time 300 seconds(5 minutes)
13
13
  CACHE_SETTINGS = { bypass: false,
14
- expiry_time_in_secs: 60 }.freeze
14
+ default_expiry: 300,
15
+ plan_expiry: 300,
16
+ subscription_expiry: 5 }.freeze
15
17
 
16
18
  # Usage examples to perform caching in RecurlyApi:Client
17
19
  # cache.write('key', 'val', expires_in: 30)
@@ -27,7 +27,7 @@ module RecurlyApi
27
27
 
28
28
  # Initialize method attributes
29
29
  attr_accessor :site_id, :authorization_key, :base_host,
30
- :api_version, :api_endpoint
30
+ :api_version, :api_endpoint, :cache_settings
31
31
  # request attributes for request_api method
32
32
  attr_accessor :path_name, :http_method, :query_params,
33
33
  :payload, :additional_headers, :payload_name,
@@ -45,11 +45,14 @@ module RecurlyApi
45
45
  # @param base_host [String] Optional, default: v3.recurly.com
46
46
  # @param api_version [String] Optional, Recurly api_version ex: 'v2021-02-25' | 'v2019-10-10'
47
47
  # @param ratelimit_retries [Integer] Optional, retry limit for rate limit exceeds, default: 3
48
+ # @param cache_settings [Hash] Optional, default settings for caching
49
+ # rubocop:disable Metrics/ParameterLists
48
50
  def initialize(authorization_key:,
49
51
  site_id:,
50
52
  base_host: nil,
51
53
  api_version: nil,
52
- ratelimit_retries: RATE_LIMIT_MAX_RETRIES)
54
+ ratelimit_retries: RATE_LIMIT_MAX_RETRIES,
55
+ cache_settings: CACHE_SETTINGS)
53
56
  @authorization_key = authorization_key
54
57
  @site_id = site_id
55
58
  raise ArgumentError, "'authorization_key' must be set to a non-nil value" if @authorization_key.nil?
@@ -59,8 +62,9 @@ module RecurlyApi
59
62
  @api_version = api_version || API_DEFAULTS[:api_version]
60
63
  @api_endpoint = "https://#{@base_host}/sites/#{@site_id}"
61
64
  @ratelimit_retries = ratelimit_retries
62
- @cache_expires_in = CACHE_SETTINGS[:expiry_time_in_secs]
63
- @ignore_caching = CACHE_SETTINGS[:bypass]
65
+ @cache_settings = cache_settings
66
+ @cache_expires_in = cache_settings[:default_expiry]
67
+ @ignore_caching = cache_settings[:bypass]
64
68
  end
65
69
 
66
70
  # TODO: remove unnecessary logs once integration testing completed
@@ -78,7 +82,7 @@ module RecurlyApi
78
82
  # @param optional_params [Hash] Optional, or any of below ( ref full usage example)
79
83
  # :payload_name [Symbol] Optional, defaults to ':data'
80
84
  # :bypass_caching [Boolean] Optional, default value is false
81
- # :cache_expiry_in_secs [Integer] Optional, default value is 60 seconds
85
+ # :cache_expiry_in [Integer] Optional, default value is 300 seconds
82
86
 
83
87
  # Ful Usage Example: Below is the example for 'request_api' call that includes all options
84
88
  # res = request_api(path_name: 'plans', # required
@@ -89,12 +93,13 @@ module RecurlyApi
89
93
  # cache_key_name: 'key123', # optional, defaults to method_name
90
94
  # bypass_caching: true, # optionals, default false
91
95
  # payload_name: 'list_all_plans', # optional defaults to caller method_name
92
- # cache_expiry_in_secs: 180 # optional default 60
96
+ # cache_expiry_in: 300 # optional default 300
93
97
  # )
94
98
  # RestClient retry { https://blog.appsignal.com/2018/05/16/ensure-retry-and-reraise-exceptions-in-ruby.html }
95
- # rubocop:disable Metrics/ParameterLists
96
99
  # rubocop:disable Metrics/AbcSize
97
100
  # rubocop:disable Metrics/PerceivedComplexity
101
+ # rubocop:disable Metrics/CyclomaticComplexity
102
+ # rubocop:disable Metrics/MethodLength
98
103
  def request_api(path_name:, http_method: :get, query_params: {},
99
104
  payload: {}, additional_headers: {},
100
105
  cache_key_name: nil, **optional_params)
@@ -110,7 +115,7 @@ module RecurlyApi
110
115
  # optional params ===>
111
116
  self.payload_name = optional_params[:payload_name] || 'payload'
112
117
  self.ignore_caching = optional_params[:bypass_caching] || ignore_caching
113
- self.cache_expires_in = optional_params[:cache_expiry_in_secs] || cache_expires_in
118
+ self.cache_expires_in = optional_params[:cache_expires_in] || cache_expires_in
114
119
  # caller_method_name = caller_locations.first.label # Ruby 2.0 +
115
120
  caller_method_name = caller(1..1).first[/`(.*)'/, 1] # Prior Ruby 2.0
116
121
  self.cache_key = cache_key_name || caller_method_name # fallbacks to caller method if cache_key_name not present
@@ -179,5 +184,12 @@ module RecurlyApi
179
184
  error: json_body['error']['type'],
180
185
  message: json_body['error']['message'] }
181
186
  end
187
+
188
+ def handle_recurly_error_response(resp)
189
+ json_body = JSON.parse(resp.body)
190
+ return unless json_body['error']
191
+
192
+ handle_error_response(resp.code, json_body)
193
+ end
182
194
  end
183
195
  end
@@ -5,18 +5,21 @@ module RecurlyApi
5
5
  class Client
6
6
  # Fetch Account Info--
7
7
  # { https://developers.recurly.com/api/v2021-02-25/index.html#operation/get_account }
8
- # @param ssor_id [String] Required, Account ID(SsorID) or code.
9
- # For ID no prefix is used e.g. +e28zov4fw0v2+. For code use prefix +code-+, e.g. +code-bob+.
8
+ # @param ssor_id [String], Recurly Account Code(i.e SsorID)
9
+ # - The unique identifier of the Recurly account
10
10
  def account_info(ssor_id:, **optional_params)
11
- request_api(path_name: "accounts/#{ssor_id}", **optional_params) # cache_key_name: cache_key_name
11
+ # cache_key should be uniq for each account(here ssor_id is uniq)
12
+ # final cache key with prefix: tribune_recurly_api.account_info.<abcdef>
13
+ self.cache_key_name = "account_info.#{ssor_id}" unless bypass_caching?
14
+ request_api(path_name: "accounts/code-#{ssor_id}", **optional_params) # cache_key_name: cache_key_name
12
15
  end
13
16
 
14
17
  # Deactivate account ---
15
18
  # { https://developers.recurly.com/api/v2021-02-25/index.html#operation/deactivate_account }
16
- # @param ssor_id [String] Required, Account ID(SsorID) or code.
17
- # For ID no prefix is used e.g. +e28zov4fw0v2+. For code use prefix +code-+, e.g. +code-bob+.
19
+ # @param ssor_id [String], Recurly Account Code(i.e SsorID)
20
+ # - The unique identifier of the Recurly account
18
21
  def deactivate_account(ssor_id:)
19
- path = "accounts/#{ssor_id}"
22
+ path = "accounts/code-#{ssor_id}"
20
23
  request_api(path_name: path, http_method: :delete)
21
24
  end
22
25
  end
@@ -8,7 +8,10 @@ module RecurlyApi
8
8
  # @param ssor_id [String] Recurly Account ID(SsorID) or code.
9
9
  # For ID no prefix is used e.g. +e28zov4fw0v2+. For code use prefix +code-+, e.g. +code-bob+.
10
10
  def coupon_redemptions(ssor_id:, **optional_params)
11
- request_api(path_name: "accounts/#{ssor_id}/coupon_redemptions", **optional_params)
11
+ # cache_key should be uniq for each account(here ssor_id is uniq)
12
+ # final cache key with prefix: tribune_recurly_api.coupon_redemptions.<abcdef>
13
+ self.cache_key_name = "coupon_redemptions.#{ssor_id}" unless bypass_caching?
14
+ request_api(path_name: "accounts/code-#{ssor_id}/coupon_redemptions", **optional_params)
12
15
  end
13
16
 
14
17
  # Other request any.....
@@ -16,7 +16,7 @@ module RecurlyApi
16
16
  # @param optional_params [Hash] Optional, or any of below ( ref full usage example)
17
17
  # :payload_name [Symbol] Optional, defaults to caller method_name
18
18
  # :bypass_caching [Boolean] Optional, default value is false
19
- # :cache_expiry_in_secs [Integer] Optional, default value is 60 seconds
19
+ # :cache_expires_in [Integer] Optional, default value is 300 seconds(5 minutes)
20
20
 
21
21
  # Examples:
22
22
  # query_params = { limit: 2, order: :asc, ids: ['abc', 'def'..etc], :sort: <value>}
@@ -26,7 +26,8 @@ module RecurlyApi
26
26
 
27
27
  def list_plans(recurly_query_params: {}, **optional_params)
28
28
  # payload_name = optional_params[:payload_name] # if not present default value is :payload
29
- cache_key_name = 'list_all_plans' # optional by default cache_key is caller method_name in receiver i.e. list_plans here
29
+ # cache_key_name = 'list_all_plans' # optional by default cache_key is caller method_name in receiver i.e. list_plans here
30
+ self.cache_expires_in = optional_params[:cache_expires_in] || cache_settings[:plan_expiry] unless bypass_caching?
30
31
  request_api(path_name: 'plans', query_params: recurly_query_params,
31
32
  cache_key_name: cache_key_name, **optional_params)
32
33
  end
@@ -37,7 +38,11 @@ module RecurlyApi
37
38
  # - For ID no prefix is used e.g. <e28zov4fw0v2>. For code use prefix <code->, ex. <code-gold>.
38
39
  # @param optional_params [Hash] Optional, same as above
39
40
  def plan_info(plan_id:, **optional_params)
40
- request_api(path_name: "plans/#{plan_id}", **optional_params) # cache_key_name: cache_key_name
41
+ self.cache_expires_in = optional_params[:cache_expires_in] || cache_settings[:plan_expiry] unless bypass_caching?
42
+ # cache_key should be uniq for each plan(here plan_id is uniq)
43
+ # final cache key with prefix: tribune_recurly_api.plan_info.<abcdef>
44
+ self.cache_key_name = "plan_info.#{plan_id}" unless bypass_caching?
45
+ request_api(path_name: "plans/#{plan_id}", **optional_params)
41
46
  end
42
47
  end
43
48
  end
@@ -5,30 +5,35 @@ module RecurlyApi
5
5
  class Client
6
6
  # Fetch Account Subscriptions ---
7
7
  # { https://developers.recurly.com/api/v2021-02-25/index.html#operation/list_account_subscriptions }
8
- # @param ssor_id [String] Account ID(SsorID) or code.
9
- # For ID no prefix is used e.g. +e28zov4fw0v2+. For code use prefix +code-+, e.g. +code-gold+.
10
- # For SsorID no prefix is required
11
- # for other params ref documentation
8
+ # @param ssor_id [String], Recurly Account Code(i.e SsorID)
9
+ # - The unique identifier of the Recurly account
10
+ # for other params refer documentation
12
11
  def account_subscriptions(ssor_id:, recurly_query_params: {}, **optional_params)
13
- path = "accounts/#{ssor_id}/subscriptions"
14
- request_api(path_name: path, query_params: recurly_query_params, **optional_params) # cache_key_name: cache_key_name
12
+ path = "accounts/code-#{ssor_id}/subscriptions"
13
+ self.cache_expires_in = optional_params[:cache_expires_in] || cache_settings[:subscription_expiry] unless bypass_caching?
14
+ # cache_key should be uniq for each account(here ssor_id is uniq)
15
+ # final cache key with prefix: tribune_recurly_api.account_subscriptions.<abcdef>
16
+ self.cache_key_name = "account_subscriptions.#{ssor_id}" unless bypass_caching?
17
+ request_api(path_name: path, query_params: recurly_query_params, **optional_params)
15
18
  end
16
19
 
17
20
  # Checking if user already has subscription OR
18
21
  # Checking if user has a subscription for particular plan by its code
19
22
  # ---------------
20
23
  # @param ssor_id [String] required, Recurly Account Code(i.e SsorID)
24
+ # - The unique identifier of the Recurly account
21
25
  # @param plan_code [String] optional,
22
26
  # - if present then checks if user has subscription for that particular plan by its code
23
27
  # Usage ex:
24
28
  # client.check_user_subscription(ssor_id: '4900-0272-6875', plan_code: '000150d03d')
25
- # response => { :success=>true, :status=>200, :has_subscription=>true }
29
+ # response => { :success=>true, :status_code=>200, :has_subscription=>true }
26
30
  def check_user_subscription(ssor_id:, plan_code: nil)
27
- resp = account_subscriptions(ssor_id: "code-#{ssor_id}")
31
+ query_params = { limit: 2 }
32
+ resp = account_subscriptions(ssor_id: ssor_id, recurly_query_params: query_params)
28
33
  if resp[:success]
29
34
  has_subscription = false
30
- subs = resp[:data]
31
- has_subscription = true if subs.any?
35
+ subs = resp[:payload]['data']
36
+ has_subscription = true if subs&.any?
32
37
  if plan_code
33
38
  sub = subs.detect { |s| s['plan']['code'].eql?(plan_code) }
34
39
  has_subscription = sub ? true : false
@@ -51,6 +56,7 @@ module RecurlyApi
51
56
  # recurly_query_params = { limit: RECORDS_LIMIT, order: :asc, .....}
52
57
  # ex: client.site_subscriptions(params)
53
58
  def site_subscriptions(recurly_query_params: {}, **optional_params)
59
+ self.cache_expires_in = optional_params[:cache_expires_in] || cache_settings[:subscription_expiry] unless bypass_caching?
54
60
  request_api(path_name: 'subscriptions', query_params: recurly_query_params, **optional_params)
55
61
  end
56
62
 
@@ -10,21 +10,21 @@ module RecurlyApi
10
10
  rescue RestClient::Unauthorized, RestClient::Forbidden => e
11
11
  # https://www.rubydoc.info/gems/rest-client/1.6.7/frames#label-Result+handling
12
12
  logger.error("#{logger_heading} ERROR: Access denied to Recurly API")
13
- raise e.response
13
+ handle_recurly_error_response(e.response)
14
14
  rescue RestClient::ImATeapot => e
15
15
  logger.error("#{logger_heading} ERROR: Recurly Server is a teapot! # RFC 2324")
16
- raise e.response
16
+ handle_recurly_error_response(e.response)
17
17
  rescue RestClient::MovedPermanently,
18
18
  RestClient::Found,
19
19
  RestClient::TemporaryRedirect => e
20
20
  logger.error("#{logger_heading} ERROR: Follow redirection- #{e.response.follow_redirection}")
21
- raise e.response
21
+ handle_recurly_error_response(e.response)
22
22
  rescue RestClient::ExceptionWithResponse => e
23
23
  logger.error("#{logger_heading} ERROR: RestClient::ExceptionWithResponse")
24
- raise e.response
24
+ handle_recurly_error_response(e.response)
25
25
  rescue RestClient::Exception => e
26
26
  logger.error("#{logger_heading} ERROR: RestClient::Exception")
27
- raise e.response
27
+ handle_recurly_error_response(e.response)
28
28
  # rescue StandardError => e # Note: don't rescue standard error
29
29
  # logger.error("#{logger_heading} ERROR: Internal Server Error")
30
30
  # # e.backtrace.join("\n ")
@@ -14,5 +14,8 @@ module RecurlyApi
14
14
  # def self.to_s
15
15
  # STRING
16
16
  # end
17
- VERSION = '1.0.0'
17
+ # VERSION = '1.0.1' # cache settings from configuration file
18
+ # VERSION = '1.0.2' # prefixing '<code->' to ssor_id is now part of GEM
19
+ # VERSION = '1.0.3' # return recurly error response
20
+ VERSION = '1.0.4' # cache key should be uniq
18
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tribune_recurly_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - udevulapally
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-21 00:00:00.000000000 Z
11
+ date: 2021-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport