Class: Rakuten::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rakuten/response.rb

Constant Summary collapse

JSON =
ActiveSupport::JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Response

Returns a new instance of Response.



8
9
10
# File 'lib/rakuten/response.rb', line 8

def initialize body
  @raw = JSON.decode body
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/rakuten/response.rb', line 6

def raw
  @raw
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rakuten/response.rb', line 20

def empty?
  status_code == 0
end

#errorObject



30
31
32
# File 'lib/rakuten/response.rb', line 30

def error
  errors.first or Rakuten::ApiError.new("Request failed without errors returned")
end

#errorsObject



24
25
26
27
28
# File 'lib/rakuten/response.rb', line 24

def errors
  @errors ||= _errors.map do |error|
    Rakuten::ApiError[error['code']].new *error.values_at('message', 'help')
  end
end

#failed?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rakuten/response.rb', line 16

def failed?
  status_code == -1
end

#resultObject



34
35
36
37
38
39
40
# File 'lib/rakuten/response.rb', line 34

def result
  if success?
    fetch(:result, {}).except('success')
  else
    raise error
  end
end

#success?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/rakuten/response.rb', line 12

def success?
  status_code != -1
end