Class: Hudkins::Response

Inherits:
Object
  • Object
show all
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.message unless response.success?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, request, result) ⇒ Response

Example

hud.get(“/path”) do |*args|

Hudkins::Response.new( *args )

end # => returns Hudkins::Response object



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

#requestObject

Returns the value of attribute request.



18
19
20
# File 'lib/hudkins/restclient.rb', line 18

def request
  @request
end

#responseObject

Returns the value of attribute response.



18
19
20
# File 'lib/hudkins/restclient.rb', line 18

def response
  @response
end

#resultObject

Returns the value of attribute result.



18
19
20
# File 'lib/hudkins/restclient.rb', line 18

def result
  @result
end

Instance Method Details

#bodyObject

Description

response = get “/path” JSON.pasre response.body



35
36
37
# File 'lib/hudkins/restclient.rb', line 35

def body
  response.body
end

#codeObject

Description

response status code



42
43
44
# File 'lib/hudkins/restclient.rb', line 42

def code
  result.code.to_i
end

#errorsObject

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

#messageObject

Description

#errors message



56
57
58
# File 'lib/hudkins/restclient.rb', line 56

def message
  errors.message
end

#response_typeObject

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

Returns:

  • (Boolean)


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

#typeObject



98
99
100
# File 'lib/hudkins/restclient.rb', line 98

def type
  response_type.sub_type.to_sym
end