Module: RestMan::AbstractResponse

Included in:
RawResponse, Response
Defined in:
lib/restman/abstract_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



8
9
10
# File 'lib/restman/abstract_response.rb', line 8

def duration
  @duration
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



8
9
10
# File 'lib/restman/abstract_response.rb', line 8

def end_time
  @end_time
end

#net_http_resObject (readonly)

Returns the value of attribute net_http_res.



8
9
10
# File 'lib/restman/abstract_response.rb', line 8

def net_http_res
  @net_http_res
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/restman/abstract_response.rb', line 8

def request
  @request
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



8
9
10
# File 'lib/restman/abstract_response.rb', line 8

def start_time
  @start_time
end

Class Method Details

.beautify_headers(headers) ⇒ Object

:include: _doc/lib/restman/abstract_response/beautify_headers.rdoc



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/restman/abstract_response.rb', line 137

def self.beautify_headers(headers)
  headers.inject({}) do |out, (key, value)|
    key_sym = key.tr('-', '_').downcase.to_sym

    # Handle Set-Cookie specially since it cannot be joined by comma.
    if key.downcase == 'set-cookie'
      out[key_sym] = value
    else
      out[key_sym] = value.join(', ')
    end

    out
  end
end

Instance Method Details

#codeObject

HTTP status code



30
31
32
# File 'lib/restman/abstract_response.rb', line 30

def code
  @code ||= @net_http_res.code.to_i
end

:include: _doc/lib/restman/abstract_response/cookie_jar.rdoc



81
82
83
84
85
86
87
88
89
90
# File 'lib/restman/abstract_response.rb', line 81

def cookie_jar
  return @cookie_jar if defined?(@cookie_jar) && @cookie_jar

  jar = @request.cookie_jar.dup
  headers.fetch(:set_cookie, []).each do |cookie|
    jar.parse(cookie, @request.uri)
  end

  @cookie_jar = jar
end

#cookiesObject

:include: _doc/lib/restman/abstract_response/cookies.rdoc



70
71
72
73
74
75
76
77
78
# File 'lib/restman/abstract_response.rb', line 70

def cookies
  hash = {}

  cookie_jar.cookies(@request.uri).each do |cookie|
    hash[cookie.name] = cookie.value
  end

  hash
end

#descriptionObject



118
119
120
# File 'lib/restman/abstract_response.rb', line 118

def description
  "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n"
end

#follow_get_redirection(&block) ⇒ Object

:include: _doc/lib/restman/abstract_response/follow_get_redirection.rdoc



128
129
130
131
132
133
134
# File 'lib/restman/abstract_response.rb', line 128

def follow_get_redirection(&block)
  new_args = request.args.dup
  new_args[:method] = :get
  new_args.delete(:payload)

  (new_args, &block)
end

#follow_redirection(&block) ⇒ Object

:include: _doc/lib/restman/abstract_response/follow_redirection.rdoc



123
124
125
# File 'lib/restman/abstract_response.rb', line 123

def follow_redirection(&block)
  (request.args.dup, &block)
end

#headersObject

:include: _doc/lib/restman/abstract_response/headers.rdoc



43
44
45
# File 'lib/restman/abstract_response.rb', line 43

def headers
  @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
end

#historyObject



38
39
40
# File 'lib/restman/abstract_response.rb', line 38

def history
  @history ||= request.redirection_history || []
end

#inspectObject

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/restman/abstract_response.rb', line 10

def inspect
  raise NotImplementedError.new('must override in subclass')
end

#logObject

Logger from the request, potentially nil.



15
16
17
# File 'lib/restman/abstract_response.rb', line 15

def log
  request.log
end

#log_responseObject



19
20
21
22
23
24
25
26
27
# File 'lib/restman/abstract_response.rb', line 19

def log_response
  return unless log

  code = net_http_res.code
  res_name = net_http_res.class.to_s.gsub(/\ANet::HTTP/, '')
  content_type = (net_http_res['Content-type'] || '').gsub(/;.*\z/, '')

  log << "# => #{code} #{res_name} | #{content_type} #{size} bytes, #{sprintf('%.2f', duration)}s\n"
end

#raw_headersObject

The raw headers.



48
49
50
# File 'lib/restman/abstract_response.rb', line 48

def raw_headers
  @raw_headers ||= @net_http_res.to_hash
end

#response_set_vars(net_http_res, request, start_time) ⇒ Object

:include: _doc/lib/restman/abstract_response/response_set_vars.rdoc



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/restman/abstract_response.rb', line 53

def response_set_vars(net_http_res, request, start_time)
  @net_http_res = net_http_res
  @request = request
  @start_time = start_time
  @end_time = Time.now

  if @start_time
    @duration = @end_time - @start_time
  else
    @duration = nil
  end

  # prime redirection history
  history
end

#return!(&block) ⇒ Object

:include: _doc/lib/restman/abstract_response/return.rdoc



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/restman/abstract_response.rb', line 93

def return!(&block)
  case code
  when 200..207
    self
  when 301, 302, 307
    case request.method
    when 'get', 'head'
      check_max_redirects
      follow_redirection(&block)
    else
      raise exception_with_response
    end
  when 303
    check_max_redirects
    follow_get_redirection(&block)
  else
    raise exception_with_response
  end
end

#success?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/restman/abstract_response.rb', line 34

def success?
  (200..299).include?(code)
end

#to_iObject



113
114
115
116
# File 'lib/restman/abstract_response.rb', line 113

def to_i
  warn('warning: calling Response#to_i is not recommended')
  super
end