Module: RestMan::AbstractResponse
- Included in:
- RawResponse, Response
- Defined in:
- lib/restman/abstract_response.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#net_http_res ⇒ Object
readonly
Returns the value of attribute net_http_res.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
-
.beautify_headers(headers) ⇒ Object
:include: _doc/lib/restman/abstract_response/beautify_headers.rdoc.
Instance Method Summary collapse
-
#code ⇒ Object
HTTP status code.
-
#cookie_jar ⇒ Object
:include: _doc/lib/restman/abstract_response/cookie_jar.rdoc.
-
#cookies ⇒ Object
:include: _doc/lib/restman/abstract_response/cookies.rdoc.
- #description ⇒ Object
-
#follow_get_redirection(&block) ⇒ Object
:include: _doc/lib/restman/abstract_response/follow_get_redirection.rdoc.
-
#follow_redirection(&block) ⇒ Object
:include: _doc/lib/restman/abstract_response/follow_redirection.rdoc.
-
#headers ⇒ Object
:include: _doc/lib/restman/abstract_response/headers.rdoc.
- #history ⇒ Object
- #inspect ⇒ Object
-
#log ⇒ Object
Logger from the request, potentially nil.
- #log_response ⇒ Object
-
#raw_headers ⇒ Object
The raw headers.
-
#response_set_vars(net_http_res, request, start_time) ⇒ Object
:include: _doc/lib/restman/abstract_response/response_set_vars.rdoc.
-
#return!(&block) ⇒ Object
:include: _doc/lib/restman/abstract_response/return.rdoc.
- #success? ⇒ Boolean
- #to_i ⇒ Object
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
8 9 10 |
# File 'lib/restman/abstract_response.rb', line 8 def duration @duration end |
#end_time ⇒ Object (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_res ⇒ Object (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 |
#request ⇒ Object (readonly)
Returns the value of attribute request.
8 9 10 |
# File 'lib/restman/abstract_response.rb', line 8 def request @request end |
#start_time ⇒ Object (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
#code ⇒ Object
HTTP status code
30 31 32 |
# File 'lib/restman/abstract_response.rb', line 30 def code @code ||= @net_http_res.code.to_i end |
#cookie_jar ⇒ Object
: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 return @cookie_jar if defined?(@cookie_jar) && @cookie_jar jar = @request..dup headers.fetch(:set_cookie, []).each do || jar.parse(, @request.uri) end @cookie_jar = jar end |
#cookies ⇒ Object
: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 hash = {} .(@request.uri).each do || hash[.name] = .value end hash end |
#description ⇒ Object
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) _follow_redirection(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) _follow_redirection(request.args.dup, &block) end |
#headers ⇒ Object
: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 |
#history ⇒ Object
38 39 40 |
# File 'lib/restman/abstract_response.rb', line 38 def history @history ||= request.redirection_history || [] end |
#inspect ⇒ Object
10 11 12 |
# File 'lib/restman/abstract_response.rb', line 10 def inspect raise NotImplementedError.new('must override in subclass') end |
#log ⇒ Object
Logger from the request, potentially nil.
15 16 17 |
# File 'lib/restman/abstract_response.rb', line 15 def log request.log end |
#log_response ⇒ Object
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_headers ⇒ Object
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
34 35 36 |
# File 'lib/restman/abstract_response.rb', line 34 def success? (200..299).include?(code) end |
#to_i ⇒ Object
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 |