Module: REST
- Defined in:
- lib/rest.rb,
lib/rest/request.rb,
lib/rest/response.rb
Overview
REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API for doing REST-style HTTP calls.
Defined Under Namespace
Classes: DisconnectedError, Request, Response
Class Method Summary collapse
-
.delete(uri, headers = {}, options = {}) ⇒ Object
Performs a DELETE on a resource.
-
.get(uri, headers = {}, options = {}) ⇒ Object
Performs a GET on a resource.
-
.head(uri, headers = {}, options = {}) ⇒ Object
Performs a HEAD on a resource.
-
.patch(uri, body, headers = {}, options = {}) ⇒ Object
Performs a PATCH on a resource.
-
.post(uri, body, headers = {}, options = {}) ⇒ Object
Performs a POST on a resource.
-
.put(uri, body, headers = {}, options = {}) ⇒ Object
Performs a PUT on a resource.
Class Method Details
.delete(uri, headers = {}, options = {}) ⇒ Object
Performs a DELETE on a resource. See REST::Request.new for a complete discussion of options.
response = REST.delete('http://example.com/pigeons/12')
if response.ok?
puts "Your pigeon died ): )"
elsif response.found?
puts "Someone moved your pigeon!"
else
puts "Couldn't delete your pigeon (#{response.status_code})"
end
48 49 50 |
# File 'lib/rest.rb', line 48 def self.delete(uri, headers={}, ={}) REST::Request.perform(:delete, URI.parse(uri), nil, headers, ) end |
.get(uri, headers = {}, options = {}) ⇒ Object
Performs a GET on a resource. See REST::Request.new for a complete discussion of options.
response = REST.get('http://example.com/pigeons/12',
{'Accept' => 'text/plain'},
{:username => 'admin', :password => 'secret'}
)
if response.ok?
puts response.body
else
puts "Couldn't fetch your pigeon (#{response.status_code})"
end
20 21 22 |
# File 'lib/rest.rb', line 20 def self.get(uri, headers={}, ={}) REST::Request.perform(:get, URI.parse(uri), nil, headers, ) end |
.head(uri, headers = {}, options = {}) ⇒ Object
Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.
response = REST.head('http://example.com/pigeons/12')
if response.ok?
puts "Your pigeon exists!"
elsif response.found?
puts "Someone moved your pigeon!"
else
puts "Couldn't fetch your pigeon (#{response.status_code})"
end
34 35 36 |
# File 'lib/rest.rb', line 34 def self.head(uri, headers={}, ={}) REST::Request.perform(:head, URI.parse(uri), nil, headers, ) end |
.patch(uri, body, headers = {}, options = {}) ⇒ Object
Performs a PATCH on a resource. See REST::Request.new for a complete discussion of options.
response = REST.patch('http://example.com/pigeons/12',
{'Name' => 'Homer'}.to_xml,
{'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
)
if response.ok?
puts "Your pigeon was renamed to 'Homer'!"
else
puts "Couldn't rename your pigeon (#{response.status_code})"
puts XML.parse(response.body).reason
end
64 65 66 |
# File 'lib/rest.rb', line 64 def self.patch(uri, body, headers={}, ={}) REST::Request.perform(:patch, URI.parse(uri), body, headers, ) end |
.post(uri, body, headers = {}, options = {}) ⇒ Object
Performs a POST on a resource. See REST::Request.new for a complete discussion of options.
response = REST.post('http://example.com/pigeons',
{'Name' => 'Bowser'}.to_xml,
{'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
)
if response.created?
puts "Created a new pigeon called 'Bowser'"
else
puts "Couldn't create your pigeon (#{response.status_code})"
puts XML.parse(response.body).reason
end
96 97 98 |
# File 'lib/rest.rb', line 96 def self.post(uri, body, headers={}, ={}) REST::Request.perform(:post, URI.parse(uri), body, headers, ) end |
.put(uri, body, headers = {}, options = {}) ⇒ Object
Performs a PUT on a resource. See REST::Request.new for a complete discussion of options.
response = REST.put('http://example.com/pigeons/12',
{'Name' => 'Homer'}.to_xml,
{'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
)
if response.ok?
puts "Your pigeon 'Bowser' was replaced by 'Homer'!"
else
puts "Couldn't replace your pigeon (#{response.status_code})"
puts XML.parse(response.body).reason
end
80 81 82 |
# File 'lib/rest.rb', line 80 def self.put(uri, body, headers={}, ={}) REST::Request.perform(:put, URI.parse(uri), body, headers, ) end |