class LucidShopify::Response
Constants
- ClientError
- ServerError
- ShopError
Public Instance Methods
[](key)
click to toggle source
@param key [String]
@return [Object]
# File lib/lucid_shopify/response.rb, line 131 def [](key) data_hash[key] end
as_json(*)
click to toggle source
@return [Hash]
# File lib/lucid_shopify/response.rb, line 140 def as_json(*) to_h end
assert!()
click to toggle source
@return [self]
@raise [ClientError] for status 4xx @raise [ServerError] for status 5xx
@note help.shopify.com/en/api/getting-started/response-status-codes
# File lib/lucid_shopify/response.rb, line 71 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 self end
data_hash()
click to toggle source
The parsed response body.
@return [Hash]
# File lib/lucid_shopify/response.rb, line 50 def data_hash return {} unless json? @data_hash ||= JSON.parse(data) end
Also aliased as: to_h
each(&block)
click to toggle source
@see Hash#each
# File lib/lucid_shopify/response.rb, line 122 def each(&block) data_hash.each(&block) end
errors()
click to toggle source
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, nil]
# File lib/lucid_shopify/response.rb, line 113 def errors errors = data_hash['errors'] return {'resource' => errors} if errors.is_a?(String) errors end
errors?()
click to toggle source
@return [Boolean]
# File lib/lucid_shopify/response.rb, line 103 def errors? data_hash.has_key?('errors') end
failure?()
click to toggle source
@return [Boolean]
# File lib/lucid_shopify/response.rb, line 96 def failure? !success? end
success?()
click to toggle source
@return [Boolean]
# File lib/lucid_shopify/response.rb, line 89 def success? status_code.between?(200, 299) end
to_json(*args)
click to toggle source
@return [String]
# File lib/lucid_shopify/response.rb, line 147 def to_json(*args) as_json.to_json(*args) end
Private Instance Methods
json?()
click to toggle source
@return [Boolean]
# File lib/lucid_shopify/response.rb, line 59 def json? headers['Content-Type'] =~ /application\/json/ && !data.empty? end