Module: Buby::HttpRequestResponseHelper
- Defined in:
- lib/buby/extends/http_request_response.rb
Class Method Summary (collapse)
-
+ (Object) implant(base)
one-shot method to implant ourselves onto a target object's class interface in ruby.
- + (Boolean) implanted?
Instance Method Summary (collapse)
-
- (Object) request_body
(also: #req_body)
Returns the request message body or an empty string if there is none.
-
- (Object) request_headers
(also: #req_headers)
Returns a split array of headers.
-
- (Object) request_str
(also: #request_string, #req_str)
Returns the full request as a Ruby String - returns an empty string if request is nil.
-
- (Object) response_body
(also: #rsp_body)
Returns the message body of the response, minus headers.
-
- (Object) response_headers
(also: #rsp_headers)
returns an array of response headers split into header name and value.
-
- (Object) response_str
(also: #response_string, #rsp_str)
returns the response as a Ruby String object - returns an empty string if response is nil.
-
- (Object) uri
Returns a Ruby URI object derived from the java.net.URL object.
Class Method Details
+ (Object) implant(base)
one-shot method to implant ourselves onto a target object's class interface in ruby. All later instances will also get 'us' for free!
85 86 87 88 89 |
# File 'lib/buby/extends/http_request_response.rb', line 85 def self.implant(base) return if @implanted base.class.instance_eval { include(HttpRequestResponseHelper) } @implanted = true end |
+ (Boolean) implanted?
92 |
# File 'lib/buby/extends/http_request_response.rb', line 92 def self.implanted? ; @implanted; end |
Instance Method Details
- (Object) request_body Also known as: req_body
Returns the request message body or an empty string if there is none.
71 72 73 |
# File 'lib/buby/extends/http_request_response.rb', line 71 def request_body (@req_split ||= req_str.split(/\r?\n\r?\n/, 2))[1] end |
- (Object) request_headers Also known as: req_headers
Returns a split array of headers. Example:
[
["GET / HTTP/1.1"],
["Host", "www.example.org"],
["User-Agent", "Mozilla/5.0 (..."],
...
]
62 63 64 65 66 |
# File 'lib/buby/extends/http_request_response.rb', line 62 def request_headers if headers=(@req_split ||= req_str.split(/\r?\n\r?\n/, 2))[0] @req_headers ||= headers.split(/\r?\n/).map {|h| h.split(/\s*:\s*/,2)} end end |
- (Object) request_str Also known as: request_string, req_str
Returns the full request as a Ruby String - returns an empty string if request is nil.
48 49 50 |
# File 'lib/buby/extends/http_request_response.rb', line 48 def request_str return request().nil? ? "" : ::String.from_java_bytes(request()) end |
- (Object) response_body Also known as: rsp_body
Returns the message body of the response, minus headers
40 41 42 |
# File 'lib/buby/extends/http_request_response.rb', line 40 def response_body (@rsp_split ||= rsp_str.split(/\r?\n\r?\n/, 2))[1] end |
- (Object) response_headers Also known as: rsp_headers
returns an array of response headers split into header name and value. For example:
[
["HTTP/1.1 301 Moved Permanently"],
["Server", "Apache/1.3.41 ..."],
...
]
32 33 34 35 36 |
# File 'lib/buby/extends/http_request_response.rb', line 32 def response_headers if headers=(@rsp_split ||= rsp_str.split(/\r?\n\r?\n/, 2))[0] @rsp_headers ||= headers.split(/\r?\n/).map {|h| h.split(/\s*:\s*/,2)} end end |
- (Object) response_str Also known as: response_string, rsp_str
returns the response as a Ruby String object - returns an empty string if response is nil.
18 19 20 |
# File 'lib/buby/extends/http_request_response.rb', line 18 def response_str return response().nil? ? "" : ::String.from_java_bytes(response()) end |
- (Object) uri
Returns a Ruby URI object derived from the java.net.URL object
78 79 80 |
# File 'lib/buby/extends/http_request_response.rb', line 78 def uri @uri ||= URI.parse url.to_s if not url.nil? end |