shopify_api_bruv 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c0d6c9a757b1c4164d65adcd704201932875e5451b7fc452206fce010847f16
4
- data.tar.gz: 5139a1b215e2d4985857577edc2727e398edae28b068bc20744ec31b57052d81
3
+ metadata.gz: 0f6e0d6869a9334e21fa6c70cb741b1834050483e056b7a7de33b4c855696ade
4
+ data.tar.gz: 7bb74655a3b31e03951b2a6e78c1fec74cb02dd8463377b3f8af615e3a05cb7e
5
5
  SHA512:
6
- metadata.gz: 82222f2e7665f99ea63e8c17a7c0445a01e61504ed37ac4c69aaac93704a576e55a813be22e8f4b89e45d85e66e87eea40e7f141f7b8f205f5380bddc89f7dfd
7
- data.tar.gz: 19f07ef9290a43606159963d7eef46fc135376f90b9a58aad4e51209aca20950aa37dcea68bdf2bf5e269bffaf251bb0be17450fd771a42556f6057701af440f
6
+ metadata.gz: 6f1fb09bfeee763af03464676cf8d34f011e3351f58d10a0cc180422725ea05edb7721c0a70eae89080cfc7e06a18a61892d4b48ad8be4d95596bfe5ec5263cb
7
+ data.tar.gz: c1f7eabce4c49f817bf5885f469d9bd51e7f5322df51f923d075b469cb72e6e06d395227391347fdfb30828032b26dd1025abb244c921a204ef3afd873269bf6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.3] - 2023-12-10
4
+
5
+ - Updates regex to select mutation object name
6
+
7
+ ## [0.3.2] - 2023-12-10
8
+
9
+ - Adds better resource class responses
10
+
3
11
  ## [0.3.1] - 2023-12-10
4
12
 
5
13
  - Adds error handling for http requests
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shopify_api_bruv (0.3.1)
4
+ shopify_api_bruv (0.3.3)
5
5
  httparty
6
6
  logger
7
7
 
data/docs/README.md CHANGED
@@ -11,5 +11,4 @@
11
11
  | `SHOPIFY_API_BRUV_LOGGER_ENABLED` | `false` | Boolean | No |
12
12
  | `SHOPIFY_API_BRUV_RESOURCE_GRAPHQL_MAX_TRIES` | `3` | Integer | No |
13
13
  | `SHOPIFY_API_BRUV_RESOURCE_GRAPHQL_SLEEP_TIMER` | `4` | Integer | No |
14
- | `SHOPIFY_API_BRUV_RESOURCE_REST_SLEEP_TIMER` | `4` | Integer | No |
15
14
  | `SHOPIFY_API_BRUV_RESOURCE_REST_CREDIT_LEFT_THRESHOLD` | `8` | Integer | No |
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Errors
5
+ class ResourceResponseError < StandardError
6
+ end
7
+ end
8
+ end
@@ -23,12 +23,10 @@ module ShopifyApiBruv
23
23
  end
24
24
 
25
25
  response = client.request(query:, variables:)
26
- body = response.body
27
26
 
28
- handle_response_errors(body:, query:, variables:, tries:)
27
+ handle_response_errors(body: response.body, query:, variables:, tries:)
29
28
 
30
- mutation_object_name = query.match(/mutation\s+(\w+)\s*\(.*/)&.captures&.first
31
- mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name)
29
+ ResourceResponse.new(body: response.body, query:)
32
30
  end
33
31
 
34
32
  private
@@ -36,7 +34,7 @@ module ShopifyApiBruv
36
34
  def handle_response_errors(body:, query:, variables:, tries:)
37
35
  errors = body['errors'] || body.dig('data', 'errors')
38
36
 
39
- raise Errors::ResourceError, errors if tries == MAX_TRIES
37
+ raise Errors::ResourceResponseError, errors if tries == MAX_TRIES
40
38
 
41
39
  if errors&.any? { |error| error['message'] == 'Throttled' }
42
40
  sleep(SLEEP_TIMER)
@@ -44,7 +42,7 @@ module ShopifyApiBruv
44
42
  call(tries: + 1)
45
43
  end
46
44
 
47
- raise Errors::ResourceError, errors unless errors.nil?
45
+ raise Errors::ResourceResponseError, errors unless errors.nil?
48
46
  end
49
47
  end
50
48
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Resources
5
+ module Graphql
6
+ class ResourceResponse
7
+ attr_accessor :data, :extensions, :throttle_status
8
+
9
+ def initialize(body:, query:)
10
+ parse_body(body:, query:)
11
+ end
12
+
13
+ def as_open_struct
14
+ # rubocop:disable Style/OpenStructUse
15
+ JSON.parse(data.to_json, object_class: OpenStruct)
16
+ # rubocop:enable Style/OpenStructUse
17
+ end
18
+
19
+ private
20
+
21
+ def parse_body(body:, query:)
22
+ mutation_object_name = query.match(/mutation.*{\s+(\w+)\s*\(/)&.captures&.first
23
+
24
+ @data = mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name)
25
+ @throttle_status = body.dig('extensions', 'cost', 'throttleStatus')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -19,7 +19,7 @@ module ShopifyApiBruv
19
19
  !page_info[:previous].nil?
20
20
  end
21
21
 
22
- def purpose?
22
+ def page_exists?
23
23
  next_page? || previous_page?
24
24
  end
25
25
 
@@ -7,9 +7,6 @@ module ShopifyApiBruv
7
7
  attr_reader :client, :method, :path, :body
8
8
  attr_accessor :query, :pagination
9
9
 
10
- SLEEP_TIMER = ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_REST_SLEEP_TIMER', 4).to_i
11
- CREDIT_LEFT_THRESHOLD = ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_REST_CREDIT_LEFT_THRESHOLD', 8).to_i
12
-
13
10
  def initialize(config:, method:, path:, body: nil, query: nil)
14
11
  @client = Clients::Rest::Client.new(config:)
15
12
  @method = method
@@ -17,38 +14,25 @@ module ShopifyApiBruv
17
14
  @body = body
18
15
  @query = query
19
16
 
20
- validate_arguments
17
+ validate
21
18
  end
22
19
 
23
20
  def call
24
21
  response = client.public_send(method, path:, body:, query:)
25
22
 
26
- handle_response_api_limits(headers: response.headers)
27
-
28
23
  pagination = Pagination.new(resource: self, page_info: response.page_info)
29
- @pagination = pagination if pagination.purpose?
24
+ @pagination = pagination if pagination.page_exists?
30
25
 
31
- response.body
26
+ ResourceResponse.new(body: response.body, headers: response.headers)
32
27
  end
33
28
 
34
29
  private
35
30
 
36
- def validate_arguments
31
+ def validate
37
32
  if [:put, :post].include?(method)
38
33
  raise Errors::ResourceError, "Argument 'body' is required for method: #{method}" if body.nil?
39
34
  end
40
35
  end
41
-
42
- def handle_response_api_limits(headers:)
43
- api_call_limit_header = headers['x-shopify-shop-api-call-limit'].split('/')
44
-
45
- limit = api_call_limit_header.pop.to_i - 1
46
- used = api_call_limit_header.shift.to_i
47
-
48
- if (limit - used) <= CREDIT_LEFT_THRESHOLD
49
- sleep(SLEEP_TIMER)
50
- end
51
- end
52
36
  end
53
37
  end
54
38
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Resources
5
+ module Rest
6
+ class ResourceResponse
7
+ attr_accessor :data
8
+ attr_reader :credit_limit, :credit_used, :credit_left
9
+
10
+ CREDIT_LEFT_THRESHOLD = ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_REST_CREDIT_LEFT_THRESHOLD', 8).to_i
11
+
12
+ def initialize(body:, headers:)
13
+ @data = body
14
+
15
+ parse_api_call_limit(headers:)
16
+ end
17
+
18
+ def as_open_struct
19
+ # rubocop:disable Style/OpenStructUse
20
+ JSON.parse(data.to_json, object_class: OpenStruct)
21
+ # rubocop:enable Style/OpenStructUse
22
+ end
23
+
24
+ def credit_left?
25
+ credit_left > CREDIT_LEFT_THRESHOLD
26
+ end
27
+
28
+ private
29
+
30
+ def parse_api_call_limit(headers:)
31
+ api_call_limit_header = headers['x-shopify-shop-api-call-limit'].split('/')
32
+
33
+ @credit_limit = api_call_limit_header.pop.to_i - 1
34
+ @credit_used = api_call_limit_header.shift.to_i
35
+ @credit_left = credit_limit - credit_used
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ShopifyApiBruv
4
- VERSION = '0.3.1'
4
+ VERSION = '0.3.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify_api_bruv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Idjent
@@ -130,11 +130,14 @@ files:
130
130
  - lib/shopify_api_bruv/errors/http_request_error.rb
131
131
  - lib/shopify_api_bruv/errors/http_response_error.rb
132
132
  - lib/shopify_api_bruv/errors/resource_error.rb
133
+ - lib/shopify_api_bruv/errors/resource_response_error.rb
133
134
  - lib/shopify_api_bruv/logger.rb
134
135
  - lib/shopify_api_bruv/resources/base.rb
135
136
  - lib/shopify_api_bruv/resources/graphql/resource.rb
137
+ - lib/shopify_api_bruv/resources/graphql/resource_response.rb
136
138
  - lib/shopify_api_bruv/resources/rest/pagination.rb
137
139
  - lib/shopify_api_bruv/resources/rest/resource.rb
140
+ - lib/shopify_api_bruv/resources/rest/resource_response.rb
138
141
  - lib/shopify_api_bruv/version.rb
139
142
  - sig/shopify_api_bruv.rbs
140
143
  homepage: https://github.com/Idjent/shopify_api_bruv