Class: ShopifyClient::Response

Inherits:
Struct
  • Object
show all
Defined in:
lib/shopify-client/response.rb,
lib/shopify-client/response.rb,
lib/shopify-client/response.rb

Overview

NOTE: Reopened for proper scoping of error classes.

Defined Under Namespace

Classes: Error

Constant Summary collapse

ClientError =

Client errors in the 4xx range.

Class.new(Error)
ServerError =

Server errors in the 5xx range.

Class.new(Error)
InvalidAccessTokenError =

The access token was not accepted.

Class.new(ClientError)
ShopError =

The shop is frozen/locked/unavailable.

Class.new(ClientError)
GraphQLClientError =

The GraphQL API always responds with a status code of 200.

Class.new(ClientError) do
  def message
    case
    when response.errors?
      "bad response: #{response.errors.messages.first}"
    when response.user_errors?
      "bad response: #{response.user_errors.messages.first}"
    else
      "bad response"
    end
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataHash

Returns:

  • (Hash)


14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

#headersHash

Returns:

  • (Hash)


14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

#requestRequest

Returns:



14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

#status_codeInteger

Returns:

  • (Integer)


14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

Class Method Details

.from_faraday_response(faraday_response, client = nil) ⇒ Response

Parameters:

  • faraday_response (Faraday::Response)
  • client (Client) (defaults to: nil)

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shopify-client/response.rb', line 23

def from_faraday_response(faraday_response, client = nil)
  uri = Addressable::URI.parse(faraday_response.env[:url])

  new(
    Request.new(
      # Merchant myshopify.domain.
      uri.host,
      # Merchant access token.
      faraday_response.env[:request_headers]['X-Shopify-Access-Token'],
      # Request HTTP method.
      faraday_response.env[:method],
      # Request path.
      uri.path,
      # Request params.
      uri.query_values,
      # Request headers.
      faraday_response.env[:request_headers],
      # Request data.
      faraday_response.env[:request_body],
      # Client used for the request.
      client,
    ),
    faraday_response.status,
    faraday_response.headers,
    faraday_response.body || {},
  ).tap(&:assert!)
end

Instance Method Details

#assert!Object

Raises:

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/shopify-client/response.rb', line 56

def assert!
  case status_code
  when 401
    if errors.message?([/access token/i])
      raise InvalidAccessTokenError.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 errors.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.graphql? && (errors? || user_errors?)
    raise GraphQLClientError.new(request, self)
  end
end

#errorsResponseErrors

Response errors (usually included with a 422 response).

Returns:



121
122
123
# File 'lib/shopify-client/response.rb', line 121

def errors
  @errors ||= ResponseErrors.from_response_data(data)
end

#errors?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/shopify-client/response.rb', line 126

def errors?
  errors.any?
end

#inspectString

Returns:

  • (String)


147
148
149
# File 'lib/shopify-client/response.rb', line 147

def inspect
  "#<ShopifyClient::Response (#{status_code}, #{request.inspect})>"
end

#next_page(client = request.client) ⇒ Response?

Request the next page for a GET request, if any.

Parameters:

Returns:

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
# File 'lib/shopify-client/response.rb', line 97

def next_page(client = request.client)
  raise ArgumentError, 'missing client' if client.nil?

  return nil unless link[:next]

  client.get(request.path, link[:next])
end

#previous_page(client = request.client) ⇒ Response?

Request the next page for a GET request, if any.

Parameters:

Returns:

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
# File 'lib/shopify-client/response.rb', line 110

def previous_page(client = request.client)
  raise ArgumentError, 'missing client' if client.nil?

  return nil unless link[:previous]

  client.get(request.path, link[:previous])
end

#user_errorsResponseUserErrors?

GraphQL user errors (errors in mutation input).

Returns:



133
134
135
136
137
# File 'lib/shopify-client/response.rb', line 133

def user_errors
  return nil unless request.graphql?

  @user_errors ||= ResponseUserErrors.from_response_data(data)
end

#user_errors?Boolean

Returns:

  • (Boolean)


140
141
142
143
144
# File 'lib/shopify-client/response.rb', line 140

def user_errors?
  return false unless request.graphql?

  user_errors.any?
end