Class: Atom::HTTP

Inherits:
Object show all
Includes:
DigestAuth
Defined in:
lib/atom/http.rb

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

DigestAuth::CNONCE

Instance Attribute Summary collapse

Instance Method Summary collapse

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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

Instance Attribute Details

#allow_all_redirectsObject

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



143
144
145
# File 'lib/atom/http.rb', line 143

def allow_all_redirects
  @allow_all_redirects
end

#always_authObject

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



134
135
136
# File 'lib/atom/http.rb', line 134

def always_auth
  @always_auth
end

#passObject

used by the default #when_auth



118
119
120
# File 'lib/atom/http.rb', line 118

def pass
  @pass
end

#strict_sslObject

if this is true, we tell Net::HTTP to die if it can't verify the SSL when doing https



136
137
138
# File 'lib/atom/http.rb', line 136

def strict_ssl
  @strict_ssl
end

#tokenObject

the token used for Google's AuthSub authentication



121
122
123
# File 'lib/atom/http.rb', line 121

def token
  @token
end

#userObject

used by the default #when_auth



118
119
120
# File 'lib/atom/http.rb', line 118

def user
  @user
end

Instance Method Details

#delete(url, body = nil, headers = {}) ⇒ Object

DELETEs to url



182
183
184
# File 'lib/atom/http.rb', line 182

def delete url, body = nil, headers = {}
  http_request(url, Net::HTTP::Delete, body, headers)
end

#get(url, headers = {}) ⇒ Object

GETs an url



167
168
169
# File 'lib/atom/http.rb', line 167

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



202
203
204
205
206
207
208
209
210
211
212
213
214
# 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

#post(url, body, headers = {}) ⇒ Object

POSTs body to an url



172
173
174
# File 'lib/atom/http.rb', line 172

def post url, body, headers = {}
  http_request(url, Net::HTTP::Post, body, headers)
end

#put(url, body, headers = {}) ⇒ Object

PUTs body to an url



177
178
179
# File 'lib/atom/http.rb', line 177

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



217
218
219
220
221
222
# 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

#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



197
198
199
# File 'lib/atom/http.rb', line 197

def when_auth &block # :yields: abs_url, realm
  @get_auth_details = block
end