Class: Atom::HTTP
Overview
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
Constant Summary
Constants included from DigestAuth
Instance Attribute Summary collapse
-
#allow_all_redirects ⇒ Object
automatically handle redirects, even for POST/PUT/DELETE requests?.
-
#always_auth ⇒ Object
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.
-
#pass ⇒ Object
used by the default #when_auth.
-
#token ⇒ Object
the token used for Google’s AuthSub authentication.
-
#user ⇒ Object
used by the default #when_auth.
Instance Method Summary collapse
-
#delete(url, body = nil, headers = {}) ⇒ Object
DELETEs to url.
-
#get(url, headers = {}) ⇒ Object
GETs an url.
-
#get_atom_entry(url) ⇒ Object
GET a URL and turn it into an Atom::Entry.
-
#initialize(cache = nil) ⇒ HTTP
constructor
if set, ‘cache’ should be a directory for a disk cache, or an object with the same interface as Atom::FileCache.
-
#post(url, body, headers = {}) ⇒ Object
POSTs body to an url.
-
#put(url, body, headers = {}) ⇒ Object
PUTs body to an url.
-
#put_atom_entry(entry, url = entry.edit_url) ⇒ Object
PUT an Atom::Entry to a URL.
-
#when_auth(&block) ⇒ Object
a block that will be called when a remote server responds with 401 Unauthorized, so that your application can prompt for authentication details.
Methods included from DigestAuth
#digest_authenticate, #h, #kd, #parse_wwwauth_digest
Constructor Details
#initialize(cache = nil) ⇒ HTTP
if set, ‘cache’ should be a directory for a disk cache, or an object with the same interface as Atom::FileCache
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/atom/http.rb', line 143 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 |
Instance Attribute Details
#allow_all_redirects ⇒ Object
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
139 140 141 |
# File 'lib/atom/http.rb', line 139 def allow_all_redirects @allow_all_redirects end |
#always_auth ⇒ Object
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
132 133 134 |
# File 'lib/atom/http.rb', line 132 def always_auth @always_auth end |
#pass ⇒ Object
used by the default #when_auth
116 117 118 |
# File 'lib/atom/http.rb', line 116 def pass @pass end |
#token ⇒ Object
the token used for Google’s AuthSub authentication
119 120 121 |
# File 'lib/atom/http.rb', line 119 def token @token end |
#user ⇒ Object
used by the default #when_auth
116 117 118 |
# File 'lib/atom/http.rb', line 116 def user @user end |
Instance Method Details
#delete(url, body = nil, headers = {}) ⇒ Object
DELETEs to url
178 179 180 |
# File 'lib/atom/http.rb', line 178 def delete url, body = nil, headers = {} http_request(url, Net::HTTP::Delete, body, headers) end |
#get(url, headers = {}) ⇒ Object
GETs an url
163 164 165 |
# File 'lib/atom/http.rb', line 163 def get url, headers = {} http_request(url, Net::HTTP::Get, nil, headers) end |
#get_atom_entry(url) ⇒ Object
GET a URL and turn it into an Atom::Entry
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/atom/http.rb', line 198 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 |
#post(url, body, headers = {}) ⇒ Object
POSTs body to an url
168 169 170 |
# File 'lib/atom/http.rb', line 168 def post url, body, headers = {} http_request(url, Net::HTTP::Post, body, headers) end |
#put(url, body, headers = {}) ⇒ Object
PUTs body to an url
173 174 175 |
# File 'lib/atom/http.rb', line 173 def put url, body, headers = {} http_request(url, Net::HTTP::Put, body, headers) end |
#put_atom_entry(entry, url = entry.edit_url) ⇒ Object
PUT an Atom::Entry to a URL
213 214 215 216 217 218 |
# File 'lib/atom/http.rb', line 213 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 |
#when_auth(&block) ⇒ Object
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
193 194 195 |
# File 'lib/atom/http.rb', line 193 def when_auth &block # :yields: abs_url, realm @get_auth_details = block end |