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)
ShopError =
Class.new(Error)

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String)

Returns:

  • (Object)


131
132
133
# File 'lib/lucid_shopify/response.rb', line 131

def [](key)
  data_hash[key]
end

#as_jsonHash

Returns:

  • (Hash)


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

def as_json(*)
  to_h
end

#assert!self

Returns:

  • (self)

Raises:



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

#dataString

Returns:

  • (String)


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

param :data

#data_hashHash Also known as: to_h

The parsed response body.

Returns:

  • (Hash)


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

See Also:

  • Hash#each


122
123
124
# File 'lib/lucid_shopify/response.rb', line 122

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)


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

Returns:

  • (Boolean)


103
104
105
# File 'lib/lucid_shopify/response.rb', line 103

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  !success?
end

#headersHash

Returns:

  • (Hash)


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

param :headers

#requestRequest

Returns the original request.

Returns:

  • (Request)

    the original request



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

param :request

#status_codeInteger

Returns:

  • (Integer)


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

param :status_code

#success?Boolean

Returns:

  • (Boolean)


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

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

#to_json(*args) ⇒ String

Returns:

  • (String)


147
148
149
# File 'lib/lucid_shopify/response.rb', line 147

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