Class: Picasa::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/picasa/client.rb

Constant Summary collapse

BASE_URL =
'https://picasaweb.google.com/data'

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, api_secret = nil, token = nil, secret = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/picasa/client.rb', line 8

def initialize(api_key = nil, api_secret = nil, token = nil, secret = nil)
  @api_key, @api_secret = api_key, api_secret
  @client = ::Typhoeus::Hydra.new
  # if api call protected, create token and consumer
  if protected_api_call?
    @consumer = ::OAuth::Consumer.new(api_key,api_secret, :site => BASE_URL)
    @token = ::OAuth::Token.new(token, secret)
  end
end

Instance Method Details

#albums(user) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/picasa/client.rb', line 26

def albums(user)
  xml = call("/feed/api/user/#{user}")
  albums = []
  xml['entry'].each do |entry|
    album = Picasa::Album.new
    album.id = entry['id'][1]
    album.title = entry['title'][0]['content']
    album.summary = entry['summary'][0]['content']
    album.size = entry['numphotos'][0].to_i
    album.image = entry['group'][0]['content']['url']
    album.thumbnail = entry['group'][0]['thumbnail'][0]['url']
    albums << album
  end if xml['entry']
  albums
end

#call(uri) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/picasa/client.rb', line 18

def call(uri)
  request = ::Typhoeus::Request.new(BASE_URL + uri)
  authorize_request!(request) if protected_api_call?
  @client.queue(request)
  @client.run
  XmlSimple.xml_in(request.response.body)
end

#photos(user, album_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/picasa/client.rb', line 42

def photos(user, album_id)
  xml = call("/feed/api/user/#{user}/albumid/#{album_id}")
  photos = []
  xml['entry'].each do |entry|
    photo = Picasa::Photo.new
    photo.title = entry['group'][0]['description'][0]['content']
    photo.thumbnails = [
      entry['group'][0]['thumbnail'][0]['url'],
      entry['group'][0]['thumbnail'][1]['url'],
      entry['group'][0]['thumbnail'][2]['url']
    ]
    photo.image = entry['content']['src']
    photos << photo
  end if xml['entry']
  photos
end