Class: Lucid::Shopify::Response

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Includes:
Enumerable
Defined in:
lib/lucid/shopify/response.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

ClientError =
Class.new(Error)
ServerError =
Class.new(Error)
ShopError =
Class.new(Error)
AccessTokenError =
Class.new(ClientError)
GraphQLClientError =
Class.new(ClientError) do
  def message
    case
    when response.errors?
      "bad response: #{response.error_messages.first}"
    when response.user_errors?
      "bad response: #{response.user_error_messages.first}"
    else
      "bad response"
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String)

Returns:

  • (Object)


258
259
260
# File 'lib/lucid/shopify/response.rb', line 258

def [](key)
  data_hash[key]
end

#as_jsonHash

Returns:

  • (Hash)


265
266
267
# File 'lib/lucid/shopify/response.rb', line 265

def as_json(*)
  to_h
end

#assert!self

Returns:

  • (self)

Raises:



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
141
142
143
144
145
146
# 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

Returns:

  • (Hash)


63
64
65
# File 'lib/lucid/shopify/response.rb', line 63

def build_link
  Container[:parse_link_header].(headers['Link'])
end

#dataString

Returns:

  • (String)


57
# File 'lib/lucid/shopify/response.rb', line 57

param :data

#data_hashHash Also known as: to_h

The parsed response body.

Returns:

  • (Hash)


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

See Also:

  • Hash#each


251
252
253
# File 'lib/lucid/shopify/response.rb', line 251

def each(&block)
  data_hash.each(&block)
end

#error_message?(messages) ⇒ Boolean

Parameters:

  • messages (Array<Regexp, String>)

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
245
246
247
248
# 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

#error_messagesArray<String>

Returns:

  • (Array<String>)


221
222
223
224
225
# File 'lib/lucid/shopify/response.rb', line 221

def error_messages
  errors.map do |field, message|
    "#{message} [#{field}]"
  end
end

#errorsHash

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.

Returns:

  • (Hash)


194
195
196
197
198
199
200
201
202
203
204
# 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

#errors?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/lucid/shopify/response.rb', line 159

def errors?
  data_hash.has_key?('errors') # should be only on 422
end

#failure?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/lucid/shopify/response.rb', line 154

def failure?
  !success?
end

#headersHash

Returns:

  • (Hash)


55
# File 'lib/lucid/shopify/response.rb', line 55

param :headers

Returns:

  • (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.

Parameters:

  • client (Client) (defaults to: Container[:client])

Returns:



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.options.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.

Parameters:

  • client (Client) (defaults to: Container[:client])

Returns:



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.options.dig(:params, :limit) ||
          link[:previous][:limit]
  client.get(request.credentials, request.path, {**link[:previous], limit: limit})
end

#requestRequest

Returns the original request.

Returns:

  • (Request)

    the original request



51
# File 'lib/lucid/shopify/response.rb', line 51

param :request

#status_codeInteger

Returns:

  • (Integer)


53
# File 'lib/lucid/shopify/response.rb', line 53

param :status_code

#success?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/lucid/shopify/response.rb', line 149

def success?
  status_code.between?(200, 299)
end

#to_json(*args) ⇒ String

Returns:

  • (String)


270
271
272
# File 'lib/lucid/shopify/response.rb', line 270

def to_json(*args)
  as_json.to_json(*args)
end

#user_error_messagesArray<String>

Returns:

  • (Array<String>)


228
229
230
231
232
# File 'lib/lucid/shopify/response.rb', line 228

def user_error_messages
  user_errors.map do |field, message|
    "#{message} [#{field}]"
  end
end

#user_errorsHash

GraphQL user errors.

Returns:

  • (Hash)


209
210
211
212
213
214
215
216
217
218
# 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

#user_errors?Boolean

GraphQL user errors.

Returns:

  • (Boolean)


166
167
168
169
170
# File 'lib/lucid/shopify/response.rb', line 166

def user_errors?
  errors = find_user_errors

  !errors.nil? && !errors.empty?
end