Class: AgentX::Response

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

Defined Under Namespace

Classes: Headers

Constant Summary collapse

CACHEABLE_CODES =
[200, 203, 300, 301, 302, 404, 410]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, body, headers) ⇒ Response

Returns a new instance of Response.



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

def initialize(code, body, headers)
  @code, @body, @headers = code, body, Headers.parse(headers)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

Class Method Details

.from_easy(easy, response = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/agentx/response.rb', line 11

def self.from_easy(easy, response=nil)
  headers = Headers.parse(easy.response_headers)

  r = new(easy.response_code, easy.response_body, headers)

  if response && r.not_modified?
    r = new(response.code, response.body, response.headers.merge(r.headers))
  end

  r
end

.from_hash(h) ⇒ Object



62
63
64
# File 'lib/agentx/response.rb', line 62

def self.from_hash(h)
  new(h['code'], h['body'], h['headers'])
end

Instance Method Details

#cacheable?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/agentx/response.rb', line 45

def cacheable?
  return false if headers.cache_control && headers.cache_control.no_store?
  return false unless CACHEABLE_CODES.include?(code)

  !! (headers.etag || headers.last_modified || fresh?)
end

#cookiesObject



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

def cookies
  Array(headers.set_cookie || [])
end

#expires_atObject



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

def expires_at
  headers.ttl ? Time.now + headers.ttl : Time.at(0)
end

#fresh?Boolean

Returns:

  • (Boolean)


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

def fresh?
  headers.ttl && headers.ttl > 0
end

#inspectObject



52
53
54
# File 'lib/agentx/response.rb', line 52

def inspect
  "(Response #{code})"
end

#not_modified?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/agentx/response.rb', line 31

def not_modified?
  code == 304
end

#ok?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/agentx/response.rb', line 27

def ok?
  code == 200
end

#parse(type = nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/agentx/response.rb', line 66

def parse(type=nil)
  case
    when type == :json || headers.json? then Oj.load(body)
    when type == :html || headers.html? then HTML.new(body)
    when type == :xml  || headers.xml?  then XML.new(body)
                                        else body
  end
end

#to_hashObject



56
57
58
59
60
# File 'lib/agentx/response.rb', line 56

def to_hash
  { 'code'    => code,
    'body'    => body,
    'headers' => headers.to_hash }
end