Class: LucidShopify::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)

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String)

Returns:

  • (Object)


124
125
126
# File 'lib/lucid_shopify/response.rb', line 124

def [](key)
  data_hash[key]
end

#as_jsonHash

Returns:

  • (Hash)


133
134
135
# File 'lib/lucid_shopify/response.rb', line 133

def as_json(*)
  to_h
end

#assert!self

Returns:

  • (self)

Raises:



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

#dataString

Returns:

  • (String)


42
# File 'lib/lucid_shopify/response.rb', line 42

param :data

#data_hashHash Also known as: to_h

The parsed response body.

Returns:

  • (Hash)


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

See Also:

  • Hash#each


115
116
117
# File 'lib/lucid_shopify/response.rb', line 115

def each(&block)
  data_hash.each(&block)
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, nil)


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

Returns:

  • (Boolean)


96
97
98
# File 'lib/lucid_shopify/response.rb', line 96

def errors?
  data_hash.has_key?('errors')
end

#failure?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/lucid_shopify/response.rb', line 89

def failure?
  !success?
end

#headersHash

Returns:

  • (Hash)


40
# File 'lib/lucid_shopify/response.rb', line 40

param :headers

#requestRequest

Returns the original request.

Returns:

  • (Request)

    the original request



36
# File 'lib/lucid_shopify/response.rb', line 36

param :request

#status_codeInteger

Returns:

  • (Integer)


38
# File 'lib/lucid_shopify/response.rb', line 38

param :status_code

#success?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/lucid_shopify/response.rb', line 82

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

#to_json(*args) ⇒ String

Returns:

  • (String)


140
141
142
# File 'lib/lucid_shopify/response.rb', line 140

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