Class: DPN::Client::Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(httpclient_response = nil) ⇒ Response

Returns a new instance of Response.



15
16
17
18
19
# File 'lib/dpn/client/response.rb', line 15

def initialize(httpclient_response = nil)
  if httpclient_response
    load_from_response!(httpclient_response)
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/dpn/client/response.rb', line 13

def body
  @body
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/dpn/client/response.rb', line 13

def status
  @status
end

Class Method Details

.from_data(status, body) ⇒ Object

Manually create a response.



23
24
25
# File 'lib/dpn/client/response.rb', line 23

def self.from_data(status, body)
  self.new.load_from_data!(status, body)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



77
78
79
# File 'lib/dpn/client/response.rb', line 77

def ==(other)
  status == other.status && body == other.body
end

#[](key) ⇒ Object



46
47
48
# File 'lib/dpn/client/response.rb', line 46

def [](key)
  @body[key.to_sym]
end

#jsonObject Also known as: to_json, to_s



28
29
30
# File 'lib/dpn/client/response.rb', line 28

def json
  @cached_json ||= @body.to_json
end

#keysObject



41
42
43
# File 'lib/dpn/client/response.rb', line 41

def keys
  @body.keys
end

#load_from_data!(status, body) ⇒ Object



51
52
53
54
55
# File 'lib/dpn/client/response.rb', line 51

def load_from_data!(status, body)
  @body = body
  @status = status
  return self
end

#load_from_response!(httpclient_message_response) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dpn/client/response.rb', line 57

def load_from_response!(httpclient_message_response)
  raw_body = httpclient_message_response.body
  @status = httpclient_message_response.header.status_code
  begin
    @body = JSON.parse(raw_body, symbolize_names: true)
    @cached_json = raw_body
  rescue JSON::ParserError
    @body = {
      status: @status,
      parsed: nil,
      raw: raw_body
    }
    @cached_json = @body.to_json
    if success? # It wasn't actually successful
      @status = 999
    end
  end
  return self
end

#ok?Boolean Also known as: success?

Returns:

  • (Boolean)


35
36
37
# File 'lib/dpn/client/response.rb', line 35

def ok?
  [ 200, 201, 202, 203, 204, 205, 206, 207, 208, 226].include?(@status)
end