Class: Hudkins::Response
- Inherits:
-
Object
- Object
- Hudkins::Response
- Defined in:
- lib/hudkins/restclient.rb
Overview
Description
Convenience class for RestClient responses
RestClient.get returns (configurable via &block parameter to Hudkins#get) [#response, #request, #result]. I wanted to have all these available but not as an array :/
Example
# get response headers
hud.get( "/path" ).response.headers
# do something based on response success
response = hud.get "/path"
raise response. unless response.success?
Instance Attribute Summary collapse
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
-
#body ⇒ Object
Description response = get “/path” JSON.pasre response.body.
-
#code ⇒ Object
Description response status code.
-
#errors ⇒ Object
Description RestClient response error.
-
#initialize(response, request, result) ⇒ Response
constructor
Example hud.get(“/path”) do |*args| Hudkins::Response.new( *args ) end # => returns Hudkins::Response object.
-
#message ⇒ Object
Description #errors message.
-
#response_type ⇒ Object
Hudson returns type :javascript for get “/api/json” even if I explicietly set the :accept header and :html for config.xml so I’m going to use the request headers to get the returned content type…
-
#success? ⇒ Boolean
Description was the response successful? does a simple test if the response code was 2xx.
- #type ⇒ Object
Constructor Details
#initialize(response, request, result) ⇒ Response
25 26 27 28 29 |
# File 'lib/hudkins/restclient.rb', line 25 def initialize response, request, result @response = response @request = request @result = result end |
Instance Attribute Details
#request ⇒ Object
Returns the value of attribute request.
18 19 20 |
# File 'lib/hudkins/restclient.rb', line 18 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
18 19 20 |
# File 'lib/hudkins/restclient.rb', line 18 def response @response end |
#result ⇒ Object
Returns the value of attribute result.
18 19 20 |
# File 'lib/hudkins/restclient.rb', line 18 def result @result end |
Instance Method Details
#body ⇒ Object
Description
response = get “/path” JSON.pasre response.body
35 36 37 |
# File 'lib/hudkins/restclient.rb', line 35 def body response.body end |
#code ⇒ Object
Description
response status code
42 43 44 |
# File 'lib/hudkins/restclient.rb', line 42 def code result.code.to_i end |
#errors ⇒ Object
Description
RestClient response error
49 50 51 |
# File 'lib/hudkins/restclient.rb', line 49 def errors RestClient::Exceptions::EXCEPTIONS_MAP[response.code].new response end |
#message ⇒ Object
Description
#errors message
56 57 58 |
# File 'lib/hudkins/restclient.rb', line 56 def errors. end |
#response_type ⇒ Object
Hudson returns type :javascript for get “/api/json” even if I explicietly set the :accept header and :html for config.xml so I’m going to use the request headers to get the returned content type… blah
92 93 94 95 96 |
# File 'lib/hudkins/restclient.rb', line 92 def response_type # unbelievable hudson! # this probably isn't the best way, but MIME::Types returns an array... MIME::Type.new request.headers[:accept] end |
#success? ⇒ Boolean
Description
was the response successful? does a simple test if the response code was 2xx
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hudkins/restclient.rb', line 64 def success? # I think RestClient::Response doesn't use Net::HTTP... any more, but I # couldn't figure out how they want you to do this without doing case # format or something like RestClient::Ok.. etc. (There is no equivalent # RestClient::Success, although there is a RestClient::Redirect case result when Net::HTTPSuccess, Net::HTTPRedirection then true else false end #case code #when (200..299) #true #when (302) ## Found. seems to be status code for successful posts.. :/ #true #else #false #end end |
#type ⇒ Object
98 99 100 |
# File 'lib/hudkins/restclient.rb', line 98 def type response_type.sub_type.to_sym end |