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, GraphQLClientError
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
- #user_error_messages ⇒ Array<String>
-
#user_errors ⇒ Hash
GraphQL user errors.
-
#user_errors? ⇒ Boolean
GraphQL user errors.
Instance Method Details
#[](key) ⇒ Object
252 253 254 |
# File 'lib/lucid/shopify/response.rb', line 252 def [](key) data_hash[key] end |
#as_json ⇒ Hash
259 260 261 |
# File 'lib/lucid/shopify/response.rb', line 259 def as_json(*) to_h end |
#assert! ⇒ self
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lucid/shopify/response.rb', line 115 def assert! case status_code when 402 raise ShopError.new(request, self), 'Shop is frozen, awaiting payment' when 403 # NOTE: Not sure what this one means (undocumented). if ([/unavailable shop/i]) raise ShopError.new(request, self), 'Shop is unavailable' else raise ClientError.new(request, self) end 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?(GraphQLPostRequest) && (errors? || user_errors?) raise GraphQLClientError.new(request, self) end self end |
#build_link ⇒ Hash
63 64 65 |
# File 'lib/lucid/shopify/response.rb', line 63 def build_link Container[:parse_link_header].(headers['Link']) end |
#data ⇒ String
57 |
# File 'lib/lucid/shopify/response.rb', line 57 param :data |
#data_hash ⇒ Hash Also known as: to_h
The parsed response body.
98 99 100 101 102 |
# File 'lib/lucid/shopify/response.rb', line 98 def data_hash return {} unless json? @data_hash ||= JSON.parse(data) end |
#each(&block) ⇒ Object
245 246 247 |
# File 'lib/lucid/shopify/response.rb', line 245 def each(&block) data_hash.each(&block) end |
#error_message?(messages) ⇒ Boolean
231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/lucid/shopify/response.rb', line 231 def () = + .any? do || case when Regexp .any? { || .match?() } when String .include?() end end end |
#error_messages ⇒ Array<String>
215 216 217 218 219 |
# File 'lib/lucid/shopify/response.rb', line 215 def errors.map do |field, | "#{message} [#{field}]" end 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.
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/lucid/shopify/response.rb', line 188 def errors errors = data_hash['errors'] case when errors.nil? {} when errors.is_a?(String) {'resource' => errors} else errors end end |
#errors? ⇒ Boolean
153 154 155 |
# File 'lib/lucid/shopify/response.rb', line 153 def errors? data_hash.has_key?('errors') # should be only on 422 end |
#failure? ⇒ Boolean
148 149 150 |
# File 'lib/lucid/shopify/response.rb', line 148 def failure? !success? end |
#headers ⇒ Hash
55 |
# File 'lib/lucid/shopify/response.rb', line 55 param :headers |
#link ⇒ Hash
60 |
# File 'lib/lucid/shopify/response.rb', line 60 param :link, default: -> { build_link } |
#next(client: Container[:client], limit: nil) ⇒ Response?
Request the next page of a GET request, if any.
72 73 74 75 76 77 78 79 |
# File 'lib/lucid/shopify/response.rb', line 72 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.
86 87 88 89 90 91 92 93 |
# File 'lib/lucid/shopify/response.rb', line 86 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
51 |
# File 'lib/lucid/shopify/response.rb', line 51 param :request |
#status_code ⇒ Integer
53 |
# File 'lib/lucid/shopify/response.rb', line 53 param :status_code |
#success? ⇒ Boolean
143 144 145 |
# File 'lib/lucid/shopify/response.rb', line 143 def success? status_code.between?(200, 299) end |
#to_json(*args) ⇒ String
264 265 266 |
# File 'lib/lucid/shopify/response.rb', line 264 def to_json(*args) as_json.to_json(*args) end |
#user_error_messages ⇒ Array<String>
222 223 224 225 226 |
# File 'lib/lucid/shopify/response.rb', line 222 def user_errors.map do |field, | "#{message} [#{field}]" end end |
#user_errors ⇒ Hash
GraphQL user errors.
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/lucid/shopify/response.rb', line 203 def user_errors errors = find_user_errors return {} if errors.nil? || errors.empty? errors.map do |error| [ error['field'] ? error['field'].join('.') : '.', error['message'], ] end.to_h end |
#user_errors? ⇒ Boolean
GraphQL user errors.
160 161 162 163 164 |
# File 'lib/lucid/shopify/response.rb', line 160 def user_errors? errors = find_user_errors !errors.nil? && !errors.empty? end |