Class: Atom::HTTP

Inherits:
Object
  • 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

#initializeHTTP

:nodoc:



139
140
141
142
143
144
145
146
147
# File 'lib/atom/http.rb', line 139

def initialize # :nodoc:
  @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



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

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



130
131
132
# File 'lib/atom/http.rb', line 130

def always_auth
  @always_auth
end

#passObject

used by the default #when_auth



114
115
116
# File 'lib/atom/http.rb', line 114

def pass
  @pass
end

#tokenObject

the token used for Google’s AuthSub authentication



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

def token
  @token
end

#userObject

used by the default #when_auth



114
115
116
# File 'lib/atom/http.rb', line 114

def user
  @user
end

Instance Method Details

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

DELETEs to url



165
166
167
# File 'lib/atom/http.rb', line 165

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

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

GETs an url



150
151
152
# File 'lib/atom/http.rb', line 150

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



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/atom/http.rb', line 185

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



155
156
157
# File 'lib/atom/http.rb', line 155

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

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

PUTs body to an url



160
161
162
# File 'lib/atom/http.rb', line 160

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



200
201
202
203
204
205
# File 'lib/atom/http.rb', line 200

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



180
181
182
# File 'lib/atom/http.rb', line 180

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