Class: Wegift::Response

Inherits:
Object
  • Object
show all
Includes:
Initializable
Defined in:
lib/wegift/models/response.rb

Direct Known Subclasses

Order, Product, Products, RemoteCode, Stock

Constant Summary collapse

STATUS =
{ success: 'SUCCESS', error: 'ERROR' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Initializable

#initialize

Instance Attribute Details

#error_codeObject

global shared body



18
19
20
# File 'lib/wegift/models/response.rb', line 18

def error_code
  @error_code
end

#error_detailsObject

global shared body



18
19
20
# File 'lib/wegift/models/response.rb', line 18

def error_details
  @error_details
end

#error_stringObject

global shared body



18
19
20
# File 'lib/wegift/models/response.rb', line 18

def error_string
  @error_string
end

#payloadObject

global shared body



18
19
20
# File 'lib/wegift/models/response.rb', line 18

def payload
  @payload
end

#statusObject

global shared body



18
19
20
# File 'lib/wegift/models/response.rb', line 18

def status
  @status
end

Instance Method Details

#is_successful?Boolean

Returns:

  • (Boolean)


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

def is_successful?
  @status&.eql?(STATUS[:success])
end

#parse(response = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wegift/models/response.rb', line 24

def parse(response = {})
  # TODO: JSON responses, when requested?
  # let's fix that with a simpel catch all
  if response.success? && response['content-type'].eql?('application/json')
    @payload = JSON.parse(response.body)
    # TODO: @payload['status'] is only returned for orders! (products etc are plain objects)
    @status = @payload['status'] || STATUS[:success]
    @error_code = @payload['error_code']
    @error_string = @payload['error_string']
    @error_details = @payload['error_details']
  else
    @payload = {}
    @status = STATUS[:error]
    @error_code = response.status
    @error_string = response.reason_phrase
    @error_details = response.reason_phrase
  end
end