Class: Lucid::Shopify::Response
- Inherits:
-
Object
- Object
- Lucid::Shopify::Response
- Extended by:
- Dry::Initializer
- Includes:
- Enumerable
- Defined in:
- lib/lucid/shopify/response.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- ClientError =
Class.new(Error)
- ServerError =
Class.new(Error)
- ShopError =
Class.new(Error)
Instance Method Summary collapse
- #[](key) ⇒ Object
- #as_json ⇒ Hash
- #assert! ⇒ self
- #build_link ⇒ Hash
- #data ⇒ String
-
#data_hash ⇒ Hash
(also: #to_h)
The parsed response body.
- #each(&block) ⇒ Object
- #error_message?(messages) ⇒ Boolean
- #error_messages ⇒ Array<String>
-
#errors ⇒ Hash
A string rather than an object is returned by Shopify in the case of, e.g., ‘Not found’.
- #errors? ⇒ Boolean
- #failure? ⇒ Boolean
- #headers ⇒ Hash
- #link ⇒ Hash
-
#next(client: Container[:client], limit: nil) ⇒ Response?
Request the next page of a GET request, if any.
-
#previous(client: Container[:client], limit: nil) ⇒ Response?
Request the previous page of a GET request, if any.
-
#request ⇒ Request
The original request.
- #status_code ⇒ Integer
- #success? ⇒ Boolean
- #to_json(*args) ⇒ String
Instance Method Details
#[](key) ⇒ Object
200 201 202 |
# File 'lib/lucid/shopify/response.rb', line 200 def [](key) data_hash[key] end |
#as_json ⇒ Hash
207 208 209 |
# File 'lib/lucid/shopify/response.rb', line 207 def as_json(*) to_h end |
#assert! ⇒ self
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/lucid/shopify/response.rb', line 102 def assert! case status_code when 402 raise ShopError.new(request, self), 'Shop is frozen, awaiting payment' when 423 raise ShopError.new(request, self), 'Shop is locked' when 400..499 raise ClientError.new(request, self) when 500..599 raise ServerError.new(request, self) end # GraphQL always has status 200. if request.is_a?(PostGraphQLRequest) && errors? raise ClientError.new(request, self) end self end |
#build_link ⇒ Hash
50 51 52 |
# File 'lib/lucid/shopify/response.rb', line 50 def build_link Container[:parse_link_header].(headers['Link']) end |
#data ⇒ String
44 |
# File 'lib/lucid/shopify/response.rb', line 44 param :data |
#data_hash ⇒ Hash Also known as: to_h
The parsed response body.
85 86 87 88 89 |
# File 'lib/lucid/shopify/response.rb', line 85 def data_hash return {} unless json? @data_hash ||= JSON.parse(data) end |
#each(&block) ⇒ Object
193 194 195 |
# File 'lib/lucid/shopify/response.rb', line 193 def each(&block) data_hash.each(&block) end |
#error_message?(messages) ⇒ Boolean
188 189 190 |
# File 'lib/lucid/shopify/response.rb', line 188 def () .any? { || .include?() } end |
#error_messages ⇒ Array<String>
181 182 183 |
# File 'lib/lucid/shopify/response.rb', line 181 def errors.map { |field, | "#{field} #{message}" } end |
#errors ⇒ Hash
A string rather than an object is returned by Shopify in the case of, e.g., ‘Not found’. In this case, we return it under the ‘resource’ key.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lucid/shopify/response.rb', line 152 def errors errors = data_hash['errors'] errors = case when errors.nil? {} when errors.is_a?(String) {'resource' => errors} else errors end errors.merge(user_errors) end |
#errors? ⇒ Boolean
133 134 135 136 137 |
# File 'lib/lucid/shopify/response.rb', line 133 def errors? return user_errors? if user_errors? data_hash.has_key?('errors') # should be only on 422 end |
#failure? ⇒ Boolean
128 129 130 |
# File 'lib/lucid/shopify/response.rb', line 128 def failure? !success? end |
#headers ⇒ Hash
42 |
# File 'lib/lucid/shopify/response.rb', line 42 param :headers |
#link ⇒ Hash
47 |
# File 'lib/lucid/shopify/response.rb', line 47 param :link, default: -> { build_link } |
#next(client: Container[:client], limit: nil) ⇒ Response?
Request the next page of a GET request, if any.
59 60 61 62 63 64 65 66 |
# File 'lib/lucid/shopify/response.rb', line 59 def next(client: Container[:client], limit: nil) return nil unless link[:next] limit = limit || request..dig(:params, :limit) || link[:next][:limit] client.get(request.credentials, request.path, {**link[:next], limit: limit}) end |
#previous(client: Container[:client], limit: nil) ⇒ Response?
Request the previous page of a GET request, if any.
73 74 75 76 77 78 79 80 |
# File 'lib/lucid/shopify/response.rb', line 73 def previous(client: Container[:client], limit: nil) return nil unless link[:previous] limit = limit || request..dig(:params, :limit) || link[:previous][:limit] client.get(request.credentials, request.path, {**link[:previous], limit: limit}) end |
#request ⇒ Request
Returns the original request.
38 |
# File 'lib/lucid/shopify/response.rb', line 38 param :request |
#status_code ⇒ Integer
40 |
# File 'lib/lucid/shopify/response.rb', line 40 param :status_code |
#success? ⇒ Boolean
123 124 125 |
# File 'lib/lucid/shopify/response.rb', line 123 def success? status_code.between?(200, 299) end |
#to_json(*args) ⇒ String
212 213 214 |
# File 'lib/lucid/shopify/response.rb', line 212 def to_json(*args) as_json.to_json(*args) end |