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
131 132 133 |
# File 'lib/lucid_shopify/response.rb', line 131 def [](key) data_hash[key] end |
#as_json ⇒ Hash
140 141 142 |
# File 'lib/lucid_shopify/response.rb', line 140 def as_json(*) to_h end |
#assert! ⇒ self
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ String
43 |
# File 'lib/lucid_shopify/response.rb', line 43 param :data |
#data_hash ⇒ Hash Also known as: to_h
The parsed response body.
50 51 52 53 54 |
# File 'lib/lucid_shopify/response.rb', line 50 def data_hash return {} unless json? @data_hash ||= JSON.parse(data) end |
#each(&block) ⇒ Object
122 123 124 |
# File 'lib/lucid_shopify/response.rb', line 122 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.
113 114 115 116 117 |
# 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? ⇒ Boolean
103 104 105 |
# File 'lib/lucid_shopify/response.rb', line 103 def errors? data_hash.has_key?('errors') end |
#failure? ⇒ Boolean
96 97 98 |
# File 'lib/lucid_shopify/response.rb', line 96 def failure? !success? end |
#headers ⇒ Hash
41 |
# File 'lib/lucid_shopify/response.rb', line 41 param :headers |
#request ⇒ Request
Returns the original request.
37 |
# File 'lib/lucid_shopify/response.rb', line 37 param :request |
#status_code ⇒ Integer
39 |
# File 'lib/lucid_shopify/response.rb', line 39 param :status_code |
#success? ⇒ Boolean
89 90 91 |
# File 'lib/lucid_shopify/response.rb', line 89 def success? status_code.between?(200, 299) end |
#to_json(*args) ⇒ String
147 148 149 |
# File 'lib/lucid_shopify/response.rb', line 147 def to_json(*args) as_json.to_json(*args) end |