Class: LucidShopify::Response
- Inherits:
-
Object
- Object
- LucidShopify::Response
- Extended by:
- Dry::Initializer
- Includes:
- Enumerable
- Defined in:
- lib/lucid_shopify/response.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #as_json ⇒ Hash
- #assert! ⇒ self
- #data ⇒ String
-
#data_hash ⇒ Hash
(also: #to_h)
The parsed response body.
- #each(&block) ⇒ Object
-
#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
-
#request ⇒ Request
The original request.
- #status_code ⇒ Integer
- #success? ⇒ Boolean
- #to_json(*args) ⇒ String
Instance Method Details
#[](key) ⇒ Object
124 125 126 |
# File 'lib/lucid_shopify/response.rb', line 124 def [](key) data_hash[key] end |
#as_json ⇒ Hash
133 134 135 |
# File 'lib/lucid_shopify/response.rb', line 133 def as_json(*) to_h end |
#assert! ⇒ self
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lucid_shopify/response.rb', line 68 def assert! case status_code when 400..499 raise ClientError.new(request, self) when 500..599 raise ServerError.new(request, self) end self end |
#data ⇒ String
42 |
# File 'lib/lucid_shopify/response.rb', line 42 param :data |
#data_hash ⇒ Hash Also known as: to_h
The parsed response body.
49 50 51 52 53 |
# File 'lib/lucid_shopify/response.rb', line 49 def data_hash return {} unless json? @data_hash ||= JSON.parse(data) end |
#each(&block) ⇒ Object
115 116 117 |
# File 'lib/lucid_shopify/response.rb', line 115 def each(&block) data_hash.each(&block) 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.
106 107 108 109 110 |
# File 'lib/lucid_shopify/response.rb', line 106 def errors errors = data_hash['errors'] return {'resource' => errors} if errors.is_a?(String) errors end |
#errors? ⇒ Boolean
96 97 98 |
# File 'lib/lucid_shopify/response.rb', line 96 def errors? data_hash.has_key?('errors') end |
#failure? ⇒ Boolean
89 90 91 |
# File 'lib/lucid_shopify/response.rb', line 89 def failure? !success? end |
#headers ⇒ Hash
40 |
# File 'lib/lucid_shopify/response.rb', line 40 param :headers |
#request ⇒ Request
36 |
# File 'lib/lucid_shopify/response.rb', line 36 param :request |
#status_code ⇒ Integer
38 |
# File 'lib/lucid_shopify/response.rb', line 38 param :status_code |
#success? ⇒ Boolean
82 83 84 |
# File 'lib/lucid_shopify/response.rb', line 82 def success? status_code.between?(200, 299) end |
#to_json(*args) ⇒ String
140 141 142 |
# File 'lib/lucid_shopify/response.rb', line 140 def to_json(*args) as_json.to_json(*args) end |