Class | Atom::HTTP |
In: |
lib/atom/http.rb
|
Parent: | Object |
An object which handles the details of HTTP - particularly authentication and caching (neither of which are fully implemented).
This object can be used on its own, or passed to an Atom::Service, Atom::Collection or Atom::Feed, where it will be used for requests.
All its HTTP methods return a Net::HTTPResponse
allow_all_redirects | [RW] |
automatically handle redirects, even for POST/PUT/DELETE requests?
defaults to false, which will transparently redirect GET requests but return a Net::HTTPRedirection object when the server indicates to redirect a POST/PUT/DELETE |
always_auth | [RW] |
when set to :basic, :wsse or :authsub, this will send an Authentication
header with every request instead of waiting for a challenge from the
server.
be careful; always_auth :basic will send your username and password in plain text to every URL this object requests. :digest won‘t work, since Digest authentication requires an initial challenge to generate a response defaults to nil |
pass | [RW] | used by the default when_auth |
strict_ssl | [RW] | if this is true, we tell Net::HTTP to die if it can‘t verify the SSL when doing https |
token | [RW] | the token used for Google‘s AuthSub authentication |
user | [RW] | used by the default when_auth |
if set, ‘cache’ should be a directory for a disk cache, or an object with the same interface as Atom::FileCache
# File lib/atom/http.rb, line 147 def initialize cache = nil if cache.is_a? String @cache = FileCache.new(cache) elsif cache @cache = cache else @cache = NilCache.new end # initialize default #when_auth @get_auth_details = lambda do |abs_url, realm| if @user and @pass [@user, @pass] else nil end end end
DELETEs to url
# File lib/atom/http.rb, line 182 def delete url, body = nil, headers = {} http_request(url, Net::HTTP::Delete, body, headers) end
GETs an url
# File lib/atom/http.rb, line 167 def get url, headers = {} http_request(url, Net::HTTP::Get, nil, headers) end
GET a URL and turn it into an Atom::Entry
# File lib/atom/http.rb, line 202 def get_atom_entry(url) res = get(url, "Accept" => "application/atom+xml") # XXX handle other HTTP codes if res.code != "200" raise Atom::HTTPException, "failed to fetch entry: expected 200 OK, got #{res.code}" end # be picky for atom:entrys res.validate_content_type( [ "application/atom+xml" ] ) Atom::Entry.parse(res.body, url) end
POSTs body to an url
# File lib/atom/http.rb, line 172 def post url, body, headers = {} http_request(url, Net::HTTP::Post, body, headers) end
PUTs body to an url
# File lib/atom/http.rb, line 177 def put url, body, headers = {} http_request(url, Net::HTTP::Put, body, headers) end
PUT an Atom::Entry to a URL
# File lib/atom/http.rb, line 217 def put_atom_entry(entry, url = entry.edit_url) raise "Cowardly refusing to PUT a non-Atom::Entry (#{entry.class})" unless entry.is_a? Atom::Entry headers = {"Content-Type" => "application/atom+xml" } put(url, entry.to_s, headers) end
a block that will be called when a remote server responds with 401 Unauthorized, so that your application can prompt for authentication details.
the default is to use the values of @user and @pass.
your block will be called with two parameters:
abs_url: | the base URL of the request URL |
realm: | the realm used in the WWW-Authenticate header (maybe nil) |
your block should return [username, password], or nil
# File lib/atom/http.rb, line 197 def when_auth &block # :yields: abs_url, realm @get_auth_details = block end