Class: MemoriClient::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response, request: nil) ⇒ Response

Returns a new instance of Response.



4
5
6
7
# File 'lib/memori_client/response.rb', line 4

def initialize(response, request: nil)
  @response = response
  @request = request
end

Instance Method Details

#as_jsonObject



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

def as_json
  {
    status: status,
    success: success?,
    error: error?,
    parsed_body: parsed_body
  }
end

#error?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/memori_client/response.rb', line 26

def error?
  !success?
end

#parsed_bodyObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/memori_client/response.rb', line 30

def parsed_body
  return @parsed_body if @parsed_body
  
  body = @response.read_body

  if @request['Content-Type'] == 'application/json' || @response.content_type == 'application/json'
    if body.nil? || body == ''
      @parsed_body = {}
    else
      @parsed_body = JSON.parse(body)
    end
  else
    @parsed_body = body
  end

  @parsed_body
end

#statusObject



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

def status
  @response.code.to_i
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  @response.code.to_i == 200
end