Class: Lazada::API::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



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

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/lazada_dino/api/response.rb', line 4

def response
  @response
end

Instance Method Details

#body_error_messagesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lazada_dino/api/response.rb', line 47

def body_error_messages
  return if response.dig('ErrorResponse').nil?

  # Parent error coming in the header
  hash = { error: error_message }

  # Error coming in the body
  response.dig('ErrorResponse', 'Body', 'Errors')&.each do |error|
    hash[error['Field'].dup] = error['Message']
  end

  hash
end

#error?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/lazada_dino/api/response.rb', line 18

def error?
  response['ErrorResponse'].present?
end

#error_codeObject



43
44
45
# File 'lib/lazada_dino/api/response.rb', line 43

def error_code
  response.dig('ErrorResponse', 'Head', 'ErrorCode')
end

#error_messageObject



35
36
37
# File 'lib/lazada_dino/api/response.rb', line 35

def error_message
  response.dig('ErrorResponse', 'Head', 'ErrorMessage')
end

#error_typeObject



39
40
41
# File 'lib/lazada_dino/api/response.rb', line 39

def error_type
  response.dig('ErrorResponse', 'Head', 'ErrorType')
end

#request_idObject



10
11
12
# File 'lib/lazada_dino/api/response.rb', line 10

def request_id
  response.dig('SuccessResponse', 'Head', 'RequestId')
end

#success?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/lazada_dino/api/response.rb', line 14

def success?
  response['SuccessResponse'].present?
end

#warning?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/lazada_dino/api/response.rb', line 22

def warning?
  response.dig('SuccessResponse', 'Body', 'Warnings').present?
end

#warning_messagesObject



26
27
28
29
30
31
32
33
# File 'lib/lazada_dino/api/response.rb', line 26

def warning_messages
  hash = {}
  response.dig('SuccessResponse', 'Body', 'Warnings').each do |warning|
    hash[warning['Field'].dup] = warning['Message']
  end

  hash
end