Class: Picasa
- Inherits:
-
Object
- Object
- Picasa
- Defined in:
- lib/ruby_picasa.rb
Overview
Authorization
RubyPicasa makes authorizing a Rails app easy. It is a two step process:
First redirect the user to the authorization url, if the user authorizes your application, Picasa will redirect the user back to the url you specify (in this case authorize_picasa_url).
Next, pass the Rails request object to the authorize_token method which will make the api call to upgrade the token and if successful return an initialized Picasa session object ready to use. The token object can be retrieved from the token attribute.
class PicasaController < ApplicationController
def
redirect_to Picasa.()
end
def
if Picasa.token_in_request?(request)
begin
picasa = Picasa.(request)
current_user.picasa_token = picasa.token
current_user.save
flash[:notice] = 'Picasa authorization complete'
redirect_to picasa_path
rescue PicasaTokenError => e
#
@error = e.
render
end
end
end
end
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
The AuthSub token currently in use.
Class Method Summary collapse
-
.authorization_url(return_to_url, request_session = true, secure = false, authsub_url = nil) ⇒ Object
The user must be redirected to this address to authorize the application to access their Picasa account.
-
.authorize_request(request) ⇒ Object
Takes a Rails request object as in token_from_request, then makes the token authorization request to produce the permanent token.
-
.host ⇒ Object
The url to make requests to without the protocol or path.
-
.host=(h) ⇒ Object
In the unlikely event that you need to access this api on a different url, you can set it here.
-
.is_url?(path) ⇒ Boolean
A simple test used to determine if a given resource id is it’s full identifier url.
-
.path(args = {}) ⇒ Object
For more on possible options and their meanings, see: code.google.com/apis/picasaweb/reference.html.
-
.token_from_request(request) ⇒ Object
Takes a Rails request object and extracts the token from it.
- .token_in_request?(request) ⇒ Boolean
Instance Method Summary collapse
-
#album(album_id_or_url, options = {}) ⇒ Object
Retrieve a RubyPicasa::Album record.
-
#album_by_title(title, options = {}) ⇒ Object
Retrieves the user’s albums and finds the first one with a matching title.
-
#authorize_token! ⇒ Object
Attempt to upgrade the current AuthSub token to a permanent one.
-
#get_url(url, options = {}) ⇒ Object
Retrieve a RubyPicasa object determined by the type of xml results returned by Picasa.
-
#initialize(token) ⇒ Picasa
constructor
A new instance of Picasa.
-
#recent_photos(user_id_or_url, options = {}) ⇒ Object
Retrieve a RubyPicasa::RecentPhotos object, essentially a User object which contains photos instead of albums.
-
#search(q, options = {}) ⇒ Object
This request does not require authentication.
-
#user(user_id_or_url = nil, options = {}) ⇒ Object
Retrieve a RubyPicasa::User record including all user albums.
-
#xml(options = {}) ⇒ Object
Returns the raw xml from Picasa.
Constructor Details
#initialize(token) ⇒ Picasa
Returns a new instance of Picasa.
194 195 196 197 |
# File 'lib/ruby_picasa.rb', line 194 def initialize(token) @token = token @request_cache = {} end |
Instance Attribute Details
#token ⇒ Object (readonly)
The AuthSub token currently in use.
192 193 194 |
# File 'lib/ruby_picasa.rb', line 192 def token @token end |
Class Method Details
.authorization_url(return_to_url, request_session = true, secure = false, authsub_url = nil) ⇒ Object
The user must be redirected to this address to authorize the application to access their Picasa account. The token_from_request and authorize_request methods can be used to handle the resulting redirect from Picasa.
59 60 61 62 63 64 65 |
# File 'lib/ruby_picasa.rb', line 59 def (return_to_url, request_session = true, secure = false, authsub_url = nil) session = request_session ? '1' : '0' secure = secure ? '1' : '0' return_to_url = CGI.escape(return_to_url) url = authsub_url || 'http://www.google.com/accounts/AuthSubRequest' "#{ url }?scope=http%3A%2F%2F#{ host }%2Fdata%2F&session=#{ session }&secure=#{ secure }&next=#{ return_to_url }" end |
.authorize_request(request) ⇒ Object
Takes a Rails request object as in token_from_request, then makes the token authorization request to produce the permanent token. This will only work if request_session was true when you created the authorization_url.
86 87 88 89 90 |
# File 'lib/ruby_picasa.rb', line 86 def (request) p = Picasa.new(token_from_request(request)) p. p end |
.host ⇒ Object
The url to make requests to without the protocol or path.
93 94 95 |
# File 'lib/ruby_picasa.rb', line 93 def host @host ||= 'picasaweb.google.com' end |
.host=(h) ⇒ Object
In the unlikely event that you need to access this api on a different url, you can set it here. It defaults to picasaweb.google.com
99 100 101 |
# File 'lib/ruby_picasa.rb', line 99 def host=(h) @host = h end |
.is_url?(path) ⇒ Boolean
A simple test used to determine if a given resource id is it’s full identifier url. This is not intended to be a general purpose method as the test is just a check for the http/https protocol prefix.
106 107 108 |
# File 'lib/ruby_picasa.rb', line 106 def is_url?(path) path.to_s =~ %r{\Ahttps?://} end |
.path(args = {}) ⇒ Object
For more on possible options and their meanings, see: code.google.com/apis/picasaweb/reference.html
The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. These images are available as both cropped© and uncropped(u) sizes by appending c or u to the size. As an example, to retrieve a 72 pixel image that is cropped, you would specify 72c, while to retrieve the uncropped image, you would specify 72u for the thumbsize or imgmax query parameter values.
32, 48, 64, 72, 144, 160
The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. These images are available as only uncropped(u) sizes by appending u to the size or just passing the size value without appending anything.
200, 288, 320, 400, 512, 576, 640, 720, 800
The following values are valid for the thumbsize and imgmax query parameters and are not embeddable on a webpage. These image sizes are only available in uncropped format and are accessed using only the size (no u is appended to the size).
912, 1024, 1152, 1280, 1440, 1600
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ruby_picasa.rb', line 136 def path(args = {}) path, = parse_url(args) if path.nil? path = ["/data/feed/api"] if args[:user_id] == 'all' path += ["all"] else path += ["user", CGI.escape(args[:user_id] || 'default')] end path += ['albumid', CGI.escape(args[:album_id])] if args[:album_id] path = path.join('/') end ['kind'] = 'photo' if args[:recent_photos] or args[:album_id] if args[:thumbsize] and not args[:thumbsize].split(/,/).all? { |s| RubyPicasa::Photo::VALID.include?(s) } raise RubyPicasa::PicasaError, 'Invalid thumbsize.' end if args[:imgmax] and not RubyPicasa::Photo::VALID.include?(args[:imgmax]) raise RubyPicasa::PicasaError, 'Invalid imgmax.' end [:max_results, :start_index, :tag, :q, :kind, :access, :thumbsize, :imgmax, :bbox, :l].each do |arg| [arg.to_s.dasherize] = args[arg] if args[arg] end if .empty? path else [path, .map { |k, v| [k.to_s, CGI.escape(v.to_s)].join('=') }.join('&')].join('?') end end |
.token_from_request(request) ⇒ Object
Takes a Rails request object and extracts the token from it. This would happen in the action that is pointed to by the return_to_url argument when the authorization_url is created.
70 71 72 73 74 75 76 |
# File 'lib/ruby_picasa.rb', line 70 def token_from_request(request) if token = request.params['token'] return token else raise RubyPicasa::PicasaTokenError, 'No Picasa authorization token was found.' end end |
.token_in_request?(request) ⇒ Boolean
78 79 80 |
# File 'lib/ruby_picasa.rb', line 78 def token_in_request?(request) request.params['token'] end |
Instance Method Details
#album(album_id_or_url, options = {}) ⇒ Object
Retrieve a RubyPicasa::Album record. If you pass an id or a feed url it will include all photos. If you pass an entry url, it will not include photos.
222 223 224 225 |
# File 'lib/ruby_picasa.rb', line 222 def album(album_id_or_url, = {}) = (:album_id, album_id_or_url, ) get() end |
#album_by_title(title, options = {}) ⇒ Object
Retrieves the user’s albums and finds the first one with a matching title. Returns a RubyPicasa::Album object.
257 258 259 260 261 |
# File 'lib/ruby_picasa.rb', line 257 def album_by_title(title, = {}) if a = user.albums.find { |a| title === a.title } a.load end end |
#authorize_token! ⇒ Object
Attempt to upgrade the current AuthSub token to a permanent one. This only works if the Picasa session is initialized with a single use token.
201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/ruby_picasa.rb', line 201 def http = Net::HTTP.new("www.google.com", 443) http.use_ssl = true response = http.get('/accounts/accounts/AuthSubSessionToken', auth_header) token = response.body.scan(/Token=(.*)/).flatten.compact.first if token @token = token else raise RubyPicasa::PicasaTokenError, 'The request to upgrade to a session token failed.' end @token end |
#get_url(url, options = {}) ⇒ Object
Retrieve a RubyPicasa object determined by the type of xml results returned by Picasa. Any supported type of RubyPicasa resource can be requested with this method.
242 243 244 245 |
# File 'lib/ruby_picasa.rb', line 242 def get_url(url, = {}) = (:url, url, ) get() end |
#recent_photos(user_id_or_url, options = {}) ⇒ Object
Retrieve a RubyPicasa::RecentPhotos object, essentially a User object which contains photos instead of albums.
249 250 251 252 253 |
# File 'lib/ruby_picasa.rb', line 249 def recent_photos(user_id_or_url, = {}) = (:user_id, user_id_or_url, ) [:recent_photos] = true get() end |
#search(q, options = {}) ⇒ Object
This request does not require authentication. Returns a RubyPicasa::Search object containing the first 10 matches. You can call #next and #previous to navigate the paginated results on the Search object.
230 231 232 233 234 235 236 237 |
# File 'lib/ruby_picasa.rb', line 230 def search(q, = {}) h = {} h[:max_results] = 10 h[:user_id] = 'all' h[:kind] = 'photo' # merge options over h, but merge q over options get(h.merge().merge(:q => q)) end |
#user(user_id_or_url = nil, options = {}) ⇒ Object
Retrieve a RubyPicasa::User record including all user albums.
215 216 217 218 |
# File 'lib/ruby_picasa.rb', line 215 def user(user_id_or_url = nil, = {}) = (:user_id, user_id_or_url, ) get() end |
#xml(options = {}) ⇒ Object
Returns the raw xml from Picasa. See the Picasa.path method for valid options.
265 266 267 268 269 270 271 272 |
# File 'lib/ruby_picasa.rb', line 265 def xml( = {}) http = Net::HTTP.new(Picasa.host, 80) path = Picasa.path() response = http.get(path, auth_header) if response.code =~ /20[01]/ response.body end end |