go-picasa-go 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/README.rdoc +135 -0
  2. data/Rakefile +16 -0
  3. data/TODO +10 -0
  4. data/VERSION +1 -0
  5. data/bin/go-picasa-go +64 -0
  6. data/examples/rails-example/README +243 -0
  7. data/examples/rails-example/Rakefile +10 -0
  8. data/examples/rails-example/app/controllers/albums_controller.rb +62 -0
  9. data/examples/rails-example/app/controllers/application_controller.rb +10 -0
  10. data/examples/rails-example/app/controllers/photos_controller.rb +77 -0
  11. data/examples/rails-example/app/helpers/albums_helper.rb +9 -0
  12. data/examples/rails-example/app/helpers/application_helper.rb +3 -0
  13. data/examples/rails-example/app/models/my_user.rb +9 -0
  14. data/examples/rails-example/app/views/albums/_form.html.erb +15 -0
  15. data/examples/rails-example/app/views/albums/edit.html.erb +10 -0
  16. data/examples/rails-example/app/views/albums/index.html.erb +50 -0
  17. data/examples/rails-example/app/views/albums/new.html.erb +10 -0
  18. data/examples/rails-example/app/views/photos/index.html.erb +30 -0
  19. data/examples/rails-example/app/views/photos/new.html.erb +8 -0
  20. data/examples/rails-example/app/views/photos/show.html.erb +19 -0
  21. data/examples/rails-example/config/boot.rb +110 -0
  22. data/examples/rails-example/config/database.yml +22 -0
  23. data/examples/rails-example/config/environment.rb +42 -0
  24. data/examples/rails-example/config/environments/development.rb +17 -0
  25. data/examples/rails-example/config/environments/production.rb +28 -0
  26. data/examples/rails-example/config/environments/test.rb +28 -0
  27. data/examples/rails-example/config/initializers/backtrace_silencers.rb +7 -0
  28. data/examples/rails-example/config/initializers/inflections.rb +10 -0
  29. data/examples/rails-example/config/initializers/mime_types.rb +5 -0
  30. data/examples/rails-example/config/initializers/new_rails_defaults.rb +21 -0
  31. data/examples/rails-example/config/initializers/session_store.rb +15 -0
  32. data/examples/rails-example/config/locales/en.yml +5 -0
  33. data/examples/rails-example/config/routes.rb +49 -0
  34. data/examples/rails-example/db/seeds.rb +7 -0
  35. data/examples/rails-example/doc/README_FOR_APP +2 -0
  36. data/examples/rails-example/log/development.log +6923 -0
  37. data/examples/rails-example/log/production.log +0 -0
  38. data/examples/rails-example/log/server.log +0 -0
  39. data/examples/rails-example/log/test.log +0 -0
  40. data/examples/rails-example/public/404.html +30 -0
  41. data/examples/rails-example/public/422.html +30 -0
  42. data/examples/rails-example/public/500.html +30 -0
  43. data/examples/rails-example/public/favicon.ico +0 -0
  44. data/examples/rails-example/public/images/rails.png +0 -0
  45. data/examples/rails-example/public/index.html +275 -0
  46. data/examples/rails-example/public/javascripts/application.js +2 -0
  47. data/examples/rails-example/public/javascripts/controls.js +963 -0
  48. data/examples/rails-example/public/javascripts/dragdrop.js +973 -0
  49. data/examples/rails-example/public/javascripts/effects.js +1128 -0
  50. data/examples/rails-example/public/javascripts/prototype.js +4320 -0
  51. data/examples/rails-example/public/robots.txt +5 -0
  52. data/examples/rails-example/script/about +4 -0
  53. data/examples/rails-example/script/console +3 -0
  54. data/examples/rails-example/script/dbconsole +3 -0
  55. data/examples/rails-example/script/destroy +3 -0
  56. data/examples/rails-example/script/generate +3 -0
  57. data/examples/rails-example/script/performance/benchmarker +3 -0
  58. data/examples/rails-example/script/performance/profiler +3 -0
  59. data/examples/rails-example/script/plugin +3 -0
  60. data/examples/rails-example/script/runner +3 -0
  61. data/examples/rails-example/script/server +3 -0
  62. data/examples/rails-example/test/functional/albums_controller_test.rb +8 -0
  63. data/examples/rails-example/test/performance/browsing_test.rb +9 -0
  64. data/examples/rails-example/test/test_helper.rb +38 -0
  65. data/examples/rails-example/test/unit/helpers/albums_helper_test.rb +4 -0
  66. data/go_picasa_go.gemspec +184 -0
  67. data/init.rb +1 -0
  68. data/lib/generators/authentication_token_generator.rb +12 -0
  69. data/lib/generators/template/user_class.erb +9 -0
  70. data/lib/generators/user_class_generator.rb +50 -0
  71. data/lib/go_picasa_go.rb +26 -0
  72. data/lib/patchs/object.rb +21 -0
  73. data/lib/patchs/ssl.rb +9 -0
  74. data/lib/picasa/album.rb +267 -0
  75. data/lib/picasa/authentication.rb +19 -0
  76. data/lib/picasa/default_album.rb +9 -0
  77. data/lib/picasa/default_photo.rb +6 -0
  78. data/lib/picasa/default_user.rb +6 -0
  79. data/lib/picasa/http/album.rb +113 -0
  80. data/lib/picasa/http/authentication.rb +40 -0
  81. data/lib/picasa/http/photo.rb +117 -0
  82. data/lib/picasa/missing.rb +22 -0
  83. data/lib/picasa/photo.rb +190 -0
  84. data/lib/picasa/template/album.xml.erb +20 -0
  85. data/lib/picasa/template/photo.erb +15 -0
  86. data/lib/picasa/user.rb +57 -0
  87. data/lib/picasa/util.rb +33 -0
  88. data/script.rb +46 -0
  89. data/spec/album_spec.rb +245 -0
  90. data/spec/authentication_spec.rb +19 -0
  91. data/spec/default_classes_spec.rb +50 -0
  92. data/spec/fixture/photo.jpg +0 -0
  93. data/spec/fixture/photo2.jpg +0 -0
  94. data/spec/generators/user_class_generator_spec.rb +40 -0
  95. data/spec/http/album_spec.rb +129 -0
  96. data/spec/http/authentication_spec.rb +15 -0
  97. data/spec/http/photo_spec.rb +78 -0
  98. data/spec/http_response_helper.rb +23 -0
  99. data/spec/mock_helper.rb +113 -0
  100. data/spec/photo_spec.rb +201 -0
  101. data/spec/spec_helper.rb +151 -0
  102. data/spec/suites/all.rb +14 -0
  103. data/spec/suites/all_mocked.rb +5 -0
  104. data/spec/user_spec.rb +74 -0
  105. metadata +222 -0
@@ -0,0 +1,117 @@
1
+ # Module that offers methods to do the basic HTTP requests to services provided
2
+ # by Picasa to manipulate photos.
3
+ module Picasa
4
+ module HTTP
5
+ module Photo
6
+
7
+ # Do a get request to retrieve all the photos from an album
8
+
9
+ def self.get_photos user_id, album_id, auth_token
10
+ uri = photos_uri user_id, album_id
11
+ http = Net::HTTP.new(uri.host)
12
+ http.get uri.path, auth_header(auth_token)
13
+ end
14
+
15
+ # Do a get request to retrieve one photo from an album
16
+
17
+ def self.get_photo user_id, album_id, photo_id, auth_token
18
+ uri = photo_uri user_id, album_id, photo_id
19
+ http = Net::HTTP.new(uri.host)
20
+ http.get uri.path, auth_header(auth_token)
21
+ end
22
+
23
+ # Do a post request to save a new photo.
24
+
25
+ def self.post_photo user_id, album_id, auth_token, summary, file
26
+ uri = photos_uri user_id, album_id
27
+
28
+ template_path = File.dirname(__FILE__) + '/../template/'
29
+
30
+ template = ERB.new File.open(template_path+"photo.erb").read
31
+ body = template.result(binding)
32
+
33
+ http = Net::HTTP.new(uri.host, uri.port)
34
+ request = Net::HTTP::Post.new(uri.request_uri)
35
+ request.body = body
36
+ request["Content-Type"] = "multipart/related; boundary=\"END_OF_PART\""
37
+ request["Authorization"] = "GoogleLogin auth=#{auth_token}"
38
+
39
+ http.request(request)
40
+ end
41
+
42
+ # Do a delete request to delete a photo from an album
43
+
44
+ def self.delete_photo user_id, album_id, photo_id, auth_token
45
+ uri = photo_uri user_id, album_id, photo_id
46
+ headers = photos_headers auth_token, "If-Match" => "*"
47
+
48
+ http = Net::HTTP.new(uri.host, uri.port)
49
+ http.send_request('DELETE',uri.path, nil, headers)
50
+ end
51
+
52
+ # Do a put request to update a photo from an album
53
+
54
+ def self.update_photo user_id, album_id, photo_id, auth_token, summary, file
55
+ uri = photo_media_uri user_id, album_id, photo_id
56
+
57
+ template_path = File.dirname(__FILE__) + '/../template/'
58
+
59
+ template = ERB.new File.open(template_path+"photo.erb").read
60
+ body = template.result(binding)
61
+
62
+ http = Net::HTTP.new(uri.host, uri.port)
63
+ request = Net::HTTP::Put.new(uri.request_uri)
64
+ request.body = body
65
+ request["Content-Type"] = "multipart/related; boundary=\"END_OF_PART\""
66
+ request["Authorization"] = "GoogleLogin auth=#{auth_token}"
67
+ request["If-Match"] = "*"
68
+
69
+ http.request(request)
70
+ end
71
+
72
+ def self.download_image link
73
+ uri = URI.parse link
74
+
75
+ file_name = link.split("/").last
76
+
77
+ http = Net::HTTP.new(uri.host)
78
+ resp = http.get uri.path
79
+
80
+ tempfilename = File.join(Dir.tmpdir, file_name)
81
+ tempfile = File.new(tempfilename, "w")
82
+ tempfile.write resp.body
83
+ tempfile.close
84
+ tempfile
85
+ end
86
+
87
+ private
88
+
89
+ def self.photos_uri user_id, album_id
90
+ URI.parse "http://picasaweb.google.com/data/feed/api/user/#{user_id}/albumid/#{album_id}"
91
+ end
92
+
93
+ def self.photo_uri user_id, album_id, photo_id
94
+ URI.parse "http://picasaweb.google.com/data/entry/api/user/#{user_id}/albumid/#{album_id}/photoid/#{photo_id}"
95
+ end
96
+
97
+ def self.photo_media_uri user_id, album_id, photo_id
98
+ URI.parse "http://picasaweb.google.com/data/media/api/user/#{user_id}/albumid/#{album_id}/photoid/#{photo_id}"
99
+ end
100
+
101
+ def self.auth_header auth_token
102
+ headers = {
103
+ "Authorization" => "GoogleLogin auth=#{auth_token}"
104
+ }
105
+ end
106
+
107
+ def self.photos_headers auth_token, opts = {}
108
+ headers = {
109
+ "Authorization" => "GoogleLogin auth=#{auth_token}",
110
+ "Content-Type" => "application/atom+xml"
111
+ }
112
+
113
+ headers.merge opts
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,22 @@
1
+ module Picasa::Missing
2
+ def method_missing(method, *args, &block)
3
+ case method
4
+ when :save
5
+ self.send(:picasa_save)
6
+ when :save!
7
+ self.send(:picasa_save!)
8
+ when :update
9
+ self.send(:picasa_update)
10
+ when :update!
11
+ self.send(:picasa_update!)
12
+ when :update_attributes
13
+ self.send(:picasa_update_attributes, args)
14
+ when :update_attributes!
15
+ self.send(:picasa_update_attributes!, args)
16
+ when :destroy
17
+ self.send(:picasa_destroy)
18
+ when :destroy!
19
+ self.send(:picasa_destroy!)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,190 @@
1
+ # Module that offers methods to do high level operations concerning
2
+ # a album within Picasa.
3
+
4
+ module Picasa::Photo
5
+
6
+ include Picasa::Missing
7
+
8
+ # Class methods to be added to the class that will include this module.
9
+
10
+ module ClassMethods
11
+
12
+ # Sets the user class configured so it can be used later.
13
+
14
+ def belongs_to_picasa_album params
15
+ unless params[:class_name] and params[:class_name].class == String
16
+ raise Exception, 'You should pass the string of the class name that includes Picasa::Album.'
17
+ end
18
+
19
+ define_dependent_class_methods :album_class, params[:class_name]
20
+ end
21
+
22
+ # Find a photo by user_id, album_id and photo_id
23
+ # If no album is found, then an exception is raised.
24
+
25
+ def picasa_find user_id, album_id, photo_id, auth_token
26
+ resp, data = Picasa::HTTP::Photo.get_photo user_id, album_id, photo_id, auth_token
27
+
28
+ if resp.code != "200" or resp.message != "OK"
29
+ raise Exception, "Photo not found"
30
+ end
31
+
32
+ photo = new
33
+ photo.send(:populate_attributes_from_xml, data)
34
+ photo.album = album_class.picasa_find user_id, album_id, auth_token
35
+ photo.file = Picasa::HTTP::Photo.download_image photo.media_content_url
36
+ photo
37
+ end
38
+
39
+ # Find all photos from an album using user_id, album_id
40
+ # If no album is found, then an exception is raised.
41
+
42
+ def picasa_find_all user_id, album_id, auth_token
43
+ resp, data = Picasa::HTTP::Photo.get_photos user_id, album_id, auth_token
44
+
45
+ if resp.code != "200" or resp.message != "OK"
46
+ raise Exception, "Error while retrieving photos. Code: #{resp.code}, Message: #{resp.message}\n"+
47
+ "#{user_id}, #{album_id}, #{auth_token}"
48
+ end
49
+
50
+ photos = []
51
+ album = album_class.picasa_find user_id, album_id, auth_token
52
+ doc = Nokogiri::XML(data)
53
+
54
+ doc.xpath('//xmlns:entry').each do |entry|
55
+ photo = new
56
+ photo.send(:populate_attributes, entry)
57
+ photo.album = album
58
+ photos << photo
59
+ end
60
+ photos
61
+ end
62
+ end
63
+
64
+ def self.included(base)
65
+ base.extend(ClassMethods)
66
+ end
67
+
68
+ attr_reader :picasa_id, :albumid, :width, :height, :size, :timestamp, :comment_count,
69
+ :media_title, :media_content_url,
70
+ :media_thumbnail_url1, :media_thumbnail_url2,
71
+ :media_thumbnail_url3, :album
72
+
73
+ attr_accessor :album, :description, :file
74
+
75
+ # Post a photo into a picasa album.
76
+ # If cannot post, an exception is raised.
77
+
78
+ def picasa_save!
79
+ if self.picasa_id and self.picasa_id.length > 0
80
+ return picasa_update!
81
+ end
82
+
83
+ resp, data = Picasa::HTTP::Photo.post_photo user_id, album_id, auth_token, description, file
84
+
85
+ if resp.code != "201" or resp.message != "Created"
86
+ raise Exception, "Error posting photo: #{resp.message}."
87
+ end
88
+
89
+ populate_attributes_from_xml data
90
+ end
91
+
92
+ # Post a photo into a picasa album.
93
+ # If cannot post, returns false.
94
+
95
+ def picasa_save
96
+ raise_exception? do
97
+ self.picasa_save!
98
+ end
99
+ end
100
+
101
+ # Update photo's file and metadata.
102
+ # If cannot update, an exception is raised.
103
+
104
+ def picasa_update!
105
+ resp, data = Picasa::HTTP::Photo.update_photo(
106
+ 'bandmanagertest', album_id, picasa_id, auth_token, description, file
107
+ )
108
+
109
+ if resp.code != "200" or resp.message != "OK"
110
+ raise Exception, "Error updating photo: #{resp.message}."
111
+ end
112
+
113
+ populate_attributes_from_xml data
114
+ end
115
+
116
+ # Update photo's file and metadata.
117
+ # If cannot update, returns false.
118
+
119
+ def picasa_update
120
+ raise_exception? do
121
+ self.picasa_update!
122
+ end
123
+ end
124
+
125
+ # Delete the current photo from album.
126
+ # If cannot delete, an exception is raised.
127
+
128
+ def destroy!
129
+ resp, data = Picasa::HTTP::Photo.delete_photo user_id, album_id, picasa_id, auth_token
130
+
131
+ if resp.code != "200" or resp.message != "OK"
132
+ raise Exception, "Error destroying photo: #{resp.message}."
133
+ end
134
+ end
135
+
136
+ # Delete the current photo from album.
137
+ # If cannot delete, an exception is raised.
138
+
139
+ def destroy
140
+ raise_exception? do
141
+ self.destroy
142
+ end
143
+ end
144
+
145
+ private
146
+
147
+ def populate_attributes_from_xml xml
148
+ doc = Nokogiri::XML xml
149
+ entry = doc.css('entry')
150
+ populate_attributes entry
151
+ end
152
+
153
+ def populate_attributes doc
154
+ hash = doc_to_hash doc
155
+
156
+ hash.keys.each do |k|
157
+ self.instance_variable_set("@#{k}", hash[k])
158
+ end
159
+ end
160
+
161
+ def doc_to_hash doc
162
+ hash = {
163
+ :picasa_id => doc.at_xpath('.//gphoto:id').content,
164
+ :albumid => doc.at_xpath('.//gphoto:albumid').content,
165
+ :width => doc.at_xpath('.//gphoto:width').content.to_i,
166
+ :height => doc.at_xpath('.//gphoto:height').content.to_i,
167
+ :size => doc.at_xpath('./gphoto:size').content.to_i,
168
+ :timestamp => Time.at(doc.at_xpath('.//gphoto:timestamp').content.slice(0..9).to_i),
169
+ :comment_count => doc.at_xpath('.//gphoto:commentCount').content.to_i,
170
+ :media_title => doc.at_xpath('.//media:title').content,
171
+ :description => doc.at_xpath('.//media:description').content,
172
+ :media_content_url => doc.at_xpath('.//media:content').attr('url'),
173
+ :media_thumbnail_url1 => doc.xpath('.//media:thumbnail')[0].attr('url'),
174
+ :media_thumbnail_url2 => doc.xpath('.//media:thumbnail')[1].attr('url'),
175
+ :media_thumbnail_url3 => doc.xpath('.//media:thumbnail')[2].attr('url')
176
+ }
177
+ end
178
+
179
+ def user_id
180
+ @album.user.picasa_id
181
+ end
182
+
183
+ def album_id
184
+ @album.picasa_id
185
+ end
186
+
187
+ def auth_token
188
+ @album.user.auth_token
189
+ end
190
+ end
@@ -0,0 +1,20 @@
1
+ <entry xmlns='http://www.w3.org/2005/Atom'
2
+ xmlns:media='http://search.yahoo.com/mrss/'
3
+ xmlns:gphoto='http://schemas.google.com/photos/2007'>
4
+
5
+ <title type='text'><%= params[:title] %></title>
6
+ <summary type='text'><%= params[:summary] %></summary>
7
+
8
+ <gphoto:location><%= params[:location] %></gphoto:location>
9
+ <gphoto:access><%= params[:access] %></gphoto:access>
10
+ <gphoto:timestamp><%= "#{Time.now.to_i}000" %></gphoto:timestamp>
11
+
12
+ <media:group>
13
+ <media:keywords><%= params[:keywords] %></media:keywords>
14
+ </media:group>
15
+
16
+ <category
17
+ scheme='http://schemas.google.com/g/2005#kind'
18
+ term='http://schemas.google.com/photos/2007#album'>
19
+ </category>
20
+ </entry>
@@ -0,0 +1,15 @@
1
+ --END_OF_PART
2
+ Content-Type: application/atom+xml
3
+
4
+ <entry xmlns='http://www.w3.org/2005/Atom'>
5
+ <title><%= File.basename file.path %></title>
6
+ <summary><%= summary %></summary>
7
+ <category scheme="http://schemas.google.com/g/2005#kind"
8
+ term="http://schemas.google.com/photos/2007#photo"/>
9
+ </entry>
10
+ --END_OF_PART
11
+ Content-Type: image/jpeg
12
+
13
+ <%= IO.read file.path %>
14
+ --END_OF_PART--
15
+
@@ -0,0 +1,57 @@
1
+ # Module that offers methods to do high level operations concerning
2
+ # a google user within Picasa.
3
+
4
+ module Picasa::User
5
+
6
+ # Class methods that will be included in the class that include this module.
7
+
8
+ module ClassMethods
9
+
10
+ # Method used to tell the gem what is the class that implements the
11
+ # Picasa::Album module.
12
+
13
+ def has_many_picasa_albums params
14
+ unless params[:class_name] and params[:class_name].class == String
15
+ raise Exception, 'You should pass the string of the class name that includes Picasa::Album.'
16
+ end
17
+
18
+ define_dependent_class_methods :album_class, params[:class_name]
19
+ end
20
+ end
21
+
22
+ def self.included(base)
23
+ ClassMethods.extend Picasa::Util
24
+ base.extend(ClassMethods)
25
+ end
26
+
27
+ attr_accessor :picasa_id, :auth_token
28
+ attr_writer :password
29
+
30
+ # Authenticates user using the attributes of the instance.
31
+ # Returns the authentication token if the authentication succedes, otherwise
32
+ # an exception is raised.
33
+
34
+ def authenticate
35
+ @auth_token ||= Picasa::Authentication.authenticate picasa_id, @password
36
+ end
37
+
38
+ # Find an album from the current user using the album_id
39
+
40
+ def find_album album_id
41
+ album = album_class.picasa_find picasa_id, album_id, auth_token
42
+ album
43
+ end
44
+
45
+ # Find all albums from the current user. The operation is done only one time
46
+ # for an instance, if it it's needed to do it to refresh the data you can
47
+ # pass the parameter true so it can be reloaded.
48
+
49
+ def albums(reload = false)
50
+ @albums = album_class.picasa_find_all(picasa_id, auth_token) if reload
51
+ @albums ||= album_class.picasa_find_all(picasa_id, auth_token)
52
+ end
53
+ end
54
+
55
+
56
+
57
+
@@ -0,0 +1,33 @@
1
+ module Picasa::Util
2
+
3
+ def extract_auth_token body
4
+ body[/Auth=(.*)/, 1]
5
+ end
6
+
7
+ def raise_exception?
8
+ begin
9
+ yield
10
+ rescue
11
+ return false
12
+ end
13
+ true
14
+ end
15
+
16
+ def define_dependent_class_method method_name, class_name
17
+ define_method method_name do
18
+ eval(class_name)
19
+ end
20
+ end
21
+
22
+ def define_static_dependent_class_method method_name, class_name
23
+ (class << self; self; end).instance_eval {
24
+ define_dependent_class_method method_name, class_name
25
+ }
26
+ end
27
+
28
+ def define_dependent_class_methods method_name, class_name
29
+ define_dependent_class_method method_name, class_name
30
+ define_static_dependent_class_method method_name, class_name
31
+ end
32
+
33
+ end