class Lucid::Shopify::Response
Constants
- AccessTokenError
- ClientError
- GraphQLClientError
- ServerError
- ShopError
Public Instance Methods
@param key [String]
@return [Object]
# File lib/lucid/shopify/response.rb, line 258 def [](key) data_hash[key] end
@return [Hash]
# File lib/lucid/shopify/response.rb, line 265 def as_json(*) to_h end
@return [self]
@raise [ClientError] for status 4xx @raise [ServerError] for status 5xx
@note shopify.dev/concepts/about-apis/response-codes
# File lib/lucid/shopify/response.rb, line 115 def assert! case status_code when 401 if error_message?([/access token/i]) raise AccessTokenError.new(request, self), 'Invalid access token' else raise ClientError.new(request, self) end when 402 raise ShopError.new(request, self), 'Shop is frozen, awaiting payment' when 403 # NOTE: Not sure what this one means (undocumented). if error_message?([/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
@return [Hash]
# File lib/lucid/shopify/response.rb, line 63 def build_link Container[:parse_link_header].(headers['Link']) end
The parsed response body.
@return [Hash]
# File lib/lucid/shopify/response.rb, line 98 def data_hash return {} unless json? @data_hash ||= JSON.parse(data) end
@see Hash#each
# File lib/lucid/shopify/response.rb, line 251 def each(&block) data_hash.each(&block) end
@param messages [Array<Regexp, String>]
@return [Boolean]
# File lib/lucid/shopify/response.rb, line 237 def error_message?(messages) all_messages = error_messages + user_error_messages messages.any? do |message| case message when Regexp all_messages.any? { |other_message| other_message.match?(message) } when String all_messages.include?(message) end end end
@return [Array<String>]
# File lib/lucid/shopify/response.rb, line 221 def error_messages errors.map do |field, message| "#{message} [#{field}]" end end
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.
@return [Hash]
# File lib/lucid/shopify/response.rb, line 194 def errors errors = data_hash['errors'] case when errors.nil? {} when errors.is_a?(String) {'resource' => errors} else errors end end
@return [Boolean]
# File lib/lucid/shopify/response.rb, line 159 def errors? data_hash.has_key?('errors') # should be only on 422 end
@return [Boolean]
# File lib/lucid/shopify/response.rb, line 154 def failure? !success? end
Request
the next page of a GET request, if any.
@param client [Client]
@return [Response, nil]
# File lib/lucid/shopify/response.rb, line 72 def next(client: Container[:client], limit: nil) return nil unless link[:next] limit = limit || request.options.dig(:params, :limit) || link[:next][:limit] client.get(request.credentials, request.path, {**link[:next], limit: limit}) end
Request
the previous page of a GET request, if any.
@param client [Client]
@return [Response, nil]
# File lib/lucid/shopify/response.rb, line 86 def previous(client: Container[:client], limit: nil) return nil unless link[:previous] limit = limit || request.options.dig(:params, :limit) || link[:previous][:limit] client.get(request.credentials, request.path, {**link[:previous], limit: limit}) end
@return [Boolean]
# File lib/lucid/shopify/response.rb, line 149 def success? status_code.between?(200, 299) end
@return [String]
# File lib/lucid/shopify/response.rb, line 270 def to_json(*args) as_json.to_json(*args) end
@return [Array<String>]
# File lib/lucid/shopify/response.rb, line 228 def user_error_messages user_errors.map do |field, message| "#{message} [#{field}]" end end
GraphQL user errors.
@return [Hash]
# File lib/lucid/shopify/response.rb, line 209 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
GraphQL user errors.
@return [Boolean]
# File lib/lucid/shopify/response.rb, line 166 def user_errors? errors = find_user_errors !errors.nil? && !errors.empty? end
Private Instance Methods
GraphQL user errors (find recursively).
@param hash [Hash]
@return [Array, nil]
# File lib/lucid/shopify/response.rb, line 177 def find_user_errors(hash = data_hash) return nil unless request.is_a?(GraphQLPostRequest) hash.each do |k, v| return v if k == 'userErrors' if v.is_a?(Hash) errors = find_user_errors(v) return errors if errors end end nil end
@return [Boolean]
# File lib/lucid/shopify/response.rb, line 105 def json? headers['Content-Type'] =~ /application\/json/ && !data.empty? end