Class: WssAgent::Response

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

Direct Known Subclasses

ResponseInventory, ResponsePolicies

Constant Summary collapse

SUCCESS_STATUS =
1
BAD_REQUEST_STATUS =
2
SERVER_ERROR_STATUS =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
# File 'lib/wss_agent/response.rb', line 9

def initialize(response)
  @response = response
  if response.is_a?(Faraday::Error::ClientError)
    parse_error
  else
    parse_response
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/wss_agent/response.rb', line 7

def data
  @data
end

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/wss_agent/response.rb', line 7

def message
  @message
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/wss_agent/response.rb', line 7

def response
  @response
end

#response_dataObject (readonly)

Returns the value of attribute response_data.



7
8
9
# File 'lib/wss_agent/response.rb', line 7

def response_data
  @response_data
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/wss_agent/response.rb', line 7

def status
  @status
end

Instance Method Details

#parse_errorObject



18
19
20
21
# File 'lib/wss_agent/response.rb', line 18

def parse_error
  @status = SERVER_ERROR_STATUS
  @message = response.message
end

#parse_responseObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wss_agent/response.rb', line 23

def parse_response
  if response.success?
    begin
      @response_data = MultiJson.load(response.body)
      @status = @response_data['status'].to_i
      @message = @response_data['message']
    rescue
      @status = SERVER_ERROR_STATUS
      @message = response.body
    end
  else
    @status = SERVER_ERROR_STATUS
    @message = response.body
  end
end

#response_success?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/wss_agent/response.rb', line 39

def response_success?
  if response.is_a?(Faraday::Error::ClientError)
    false
  else
    response.success?
  end
end

#success?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/wss_agent/response.rb', line 47

def success?
  response_success? && status == SUCCESS_STATUS
end