vimeo 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +2 -1
  2. data/README.markdown +312 -0
  3. data/Rakefile +1 -1
  4. data/VERSION.yml +2 -1
  5. data/init.rb +2 -0
  6. data/lib/vimeo.rb +1 -0
  7. data/lib/vimeo/advanced.rb +0 -1
  8. data/lib/vimeo/advanced/album.rb +8 -10
  9. data/lib/vimeo/advanced/base.rb +56 -46
  10. data/lib/vimeo/advanced/channel.rb +6 -9
  11. data/lib/vimeo/advanced/contact.rb +2 -3
  12. data/lib/vimeo/advanced/group.rb +5 -11
  13. data/lib/vimeo/advanced/person.rb +5 -6
  14. data/lib/vimeo/advanced/test.rb +7 -24
  15. data/lib/vimeo/advanced/upload.rb +4 -9
  16. data/lib/vimeo/advanced/video.rb +14 -15
  17. data/lib/vimeo/advanced/video_embed.rb +1 -2
  18. data/test/fixtures/advanced/auth/check_access_token.json +9 -0
  19. data/test/fixtures/advanced/auth/request_token.txt +1 -0
  20. data/test/fixtures/advanced/channel/get_all.json +17 -55
  21. data/test/fixtures/advanced/group/get_all.json +5 -29
  22. data/test/fixtures/advanced/group/get_info.json +1 -2
  23. data/test/fixtures/advanced/group_events/get_month.json +3 -8
  24. data/test/fixtures/advanced/group_events/get_past.json +3 -8
  25. data/test/fixtures/advanced/group_events/get_upcoming.json +3 -8
  26. data/test/fixtures/advanced/group_forums/get_topic_comments.json +1 -3
  27. data/test/fixtures/advanced/person/get_info.json +1 -2
  28. data/test/fixtures/advanced/video/get_by_tag.json +1 -3
  29. data/test/fixtures/advanced/video/get_subscriptions.json +3 -7
  30. data/test/fixtures/advanced/video/search.json +1 -2
  31. data/test/test_helper.rb +17 -6
  32. data/test/vimeo/advanced/album_test.rb +54 -67
  33. data/test/vimeo/advanced/auth_test.rb +17 -25
  34. data/test/vimeo/advanced/base_test.rb +7 -9
  35. data/test/vimeo/advanced/channels_test.rb +5 -5
  36. data/test/vimeo/advanced/contacts_test.rb +1 -1
  37. data/test/vimeo/advanced/group_events_test.rb +1 -1
  38. data/test/vimeo/advanced/group_forums_test.rb +1 -1
  39. data/test/vimeo/advanced/groups_test.rb +4 -4
  40. data/test/vimeo/advanced/person_test.rb +9 -8
  41. data/test/vimeo/advanced/test_test.rb +3 -3
  42. data/test/vimeo/advanced/upload_test.rb +6 -6
  43. data/test/vimeo/advanced/videos_embed_test.rb +6 -5
  44. data/test/vimeo/advanced/videos_test.rb +15 -15
  45. data/vimeo.gemspec +13 -11
  46. metadata +10 -9
  47. data/README.rdoc +0 -322
  48. data/lib/vimeo/advanced/auth.rb +0 -22
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  *.sw?
2
2
  .DS_Store
3
- coverage
3
+ coverage
4
+ pkg/
data/README.markdown ADDED
@@ -0,0 +1,312 @@
1
+ # Vimeo API Gem
2
+
3
+ This gem implements a full-featured Ruby interface for the Vimeo API v2.
4
+
5
+ For a more in depth look at the API check out [Vimeo's Simple API Documentation](http://www.vimeo.com/api/docs/simple-api) or [Vimeo's Advanced API Documentation](http://www.vimeo.com/api/docs/advanced-api). I would also recommend checking out the [API Forums](http://www.vimeo.com/forum:api) if
6
+ things aren't working as they should.
7
+
8
+ __I have been experiencing trouble with intermittent 303 errors (Invalid Signature). As far as I can tell, this is a server-side issue.__
9
+
10
+ ## Install
11
+
12
+ First, install [Gemcutter](http://gemcutter.org), then:
13
+
14
+ sudo gem install vimeo
15
+
16
+ If you're using Rails, add the following to your environment.rb file:
17
+
18
+ config.gem "vimeo"
19
+
20
+ ## How to Use
21
+
22
+ There are two modules:
23
+
24
+ Vimeo::Simple
25
+ Vimeo::Advanced
26
+
27
+ ## Simple API
28
+
29
+ Let's look at the [Simple API](http://www.vimeo.com/api/docs/simple-api) first.
30
+
31
+ The wrapper for the Simple API consists of several classes. To use the Simple API, just call one of the class methods. For example:
32
+
33
+ user_info = Vimeo::Simple::User.info("matthooks")
34
+ # =>
35
+ # {
36
+ # "id":"888046",
37
+ # "display_name":"Matt Hooks",
38
+ # "created_on":"2008-10-30 14:17:32",
39
+ # "is_staff":"0",
40
+ # "is_plus":"0",
41
+ # "location":"Chicago, IL",
42
+ # "url":"http:\/\/blackholeinthemidwest.com\/",
43
+ # "bio":"",
44
+ # "profile_url":"http:\/\/vimeo.com\/matthooks",
45
+ # "videos_url":"http:\/\/vimeo.com\/matthooks\/videos",
46
+ # "total_videos_uploaded":2,
47
+ # "total_videos_appears_in":0,
48
+ # "total_videos_liked":2,
49
+ # "total_contacts":3,
50
+ # "total_albums":0,
51
+ # "total_channels":1,
52
+ # "portrait_small":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_30.jpg",
53
+ # "portrait_medium":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_75.jpg",
54
+ # "portrait_large":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_100.jpg",
55
+ # "portrait_huge":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_300.jpg"
56
+ # }
57
+
58
+ Thanks to HTTParty, the data is parsed and ready to use.
59
+
60
+ user_info["location"]
61
+ # => "Chicago, IL"
62
+
63
+ ## Overview of the Simple API
64
+
65
+ ### Vimeo::Simple::Activity
66
+
67
+ Vimeo::Simple::Activity.user_did("username")
68
+ Vimeo::Simple::Activity.happened_to_user("username")
69
+ Vimeo::Simple::Activity.contacts_did("username")
70
+ Vimeo::Simple::Activity.happened_to_contacts("username")
71
+ Vimeo::Simple::Activity.everyone_did("username")
72
+
73
+ ### Vimeo::Simple::Album
74
+
75
+ Vimeo::Simple::Album.videos("album_id")
76
+ Vimeo::Simple::Album.info("album_id")
77
+
78
+ ### Vimeo::Simple::Channel
79
+
80
+ Vimeo::Simple::Channel.videos("channelname")
81
+ Vimeo::Simple::Channel.info("channelname")
82
+
83
+ ### Vimeo::Simple::Group
84
+
85
+ Vimeo::Simple::Group.videos("groupname")
86
+ Vimeo::Simple::Group.users("groupname")
87
+ Vimeo::Simple::Group.info("groupname")
88
+
89
+ ### Vimeo::Simple::User
90
+
91
+ Vimeo::Simple::User.info("username")
92
+ Vimeo::Simple::User.videos("username")
93
+ Vimeo::Simple::User.likes("username")
94
+ Vimeo::Simple::User.appears_in("username")
95
+ Vimeo::Simple::User.all_videos("username")
96
+ Vimeo::Simple::User.subscriptions("username")
97
+ Vimeo::Simple::User.albums("username")
98
+ Vimeo::Simple::User.channels("username")
99
+ Vimeo::Simple::User.groups("username")
100
+ Vimeo::Simple::User.contacts_videos("username")
101
+ Vimeo::Simple::User.contacts_like("username")
102
+
103
+ ### Vimeo::Simple::Video
104
+
105
+ Vimeo::Simple::Video.info("video_id")
106
+
107
+ ## Advanced API
108
+
109
+ To use the [Advanced API](http://www.vimeo.com/api/docs/advanced-api), first you must authenticate your user using OAuth.
110
+
111
+ ### Authentication Example using Rails
112
+
113
+ First, instantiate the Base class:
114
+
115
+ base = Vimeo::Advanced::Base.new("consumer_key", "consumer_secret")
116
+
117
+ Then, send your user to the authorization URL:
118
+
119
+ redirect_to base.authorize_url
120
+
121
+ Once the user has allowed your application to access their account, they will be redirected to the callback URL you set up for your application. You will be given two parameters `oauth_token` and `oauth_verifier`. Re-instantiate your Base class, then get an access token:
122
+
123
+ base = Vimeo::Advanced::Base.new("consumer_key", "consumer_secret")
124
+ access_token = base.get_access_token(params[:oauth_token], params[:oauth_verifier])
125
+ # You'll want to hold on to the user's token and secret. I'll save it to the database.
126
+ user.token = access_token.token
127
+ user.secret = access_token.secret
128
+ user.save
129
+
130
+ Now you've got everything you need to use the Advanced API. Let's get a user's videos:
131
+
132
+ video = Vimeo::Advanced::Video.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
133
+ video.get_videos("matthooks")
134
+
135
+ # => {"videos"=> { ... }, "stat"=>"ok", "generated_in"=>"0.5753"}
136
+
137
+ Piece of cake.
138
+
139
+ Some methods have optional variables. Pass these as a hash at the end of a call.
140
+
141
+ video.get_all("matthooks", :page => "2", :per_page => "50")
142
+
143
+ ## Overview of the Advanced API
144
+
145
+ ### Vimeo::Advanced::Album
146
+
147
+ album = Vimeo::Advanced::Album.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
148
+
149
+ album.add_video("album_id", "video_id")
150
+ album.create("title", "video_id", { :description => "description", "videos" => "123,124,125" })
151
+ album.delete("album_id")
152
+ album.get_all("user_id", { :page => "1", :per_page => "25", :sort => "newest" })
153
+ album.get_videos("album_id", { :page => "1", :per_page => "25", :full_response => "0", :password => nil })
154
+ album.remove_video("album_id", "video_id")
155
+ album.set_description("album_id", "description")
156
+ album.get_password("album_id", "password")
157
+ album.get_title("album_id", "title")
158
+
159
+ ### Vimeo::Advanced::Base
160
+
161
+ base = Vimeo::Advanced::Base.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
162
+
163
+ base.check_access_token
164
+
165
+ ### Vimeo::Advanced::Channel
166
+
167
+ channel = Vimeo::Advanced::Channel.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
168
+
169
+ channel.add_video("channel_id", "video_id")
170
+ channel.get_all({ :page => "1", :per_page => "25", :sort => "newest", :user_id => "user_id" })
171
+ channel.get_info(channel_id)
172
+ channel.get_moderators(channel_id, { :page => "1", :per_page => "25" })
173
+ channel.get_subscribers(channel_id, { :page => "1", :per_page => "25" })
174
+ channel.get_videos(channel_id, { :page => "1", :per_page => "25", :full_response => "0" })
175
+ channel.remove_video("channel_id", "video_id")
176
+ channel.subscribe("channel_id")
177
+ channel.unsubscribe("channel_id")
178
+
179
+ ### Vimeo::Advanced::Contact
180
+
181
+ contact = Vimeo::Advanced::Contact.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
182
+
183
+ contact.get_all(user_id, { :page => "1", :per_page => "25", :sort => "newest" })
184
+ contact.get_mutual(user_id, { :page => "1", :per_page => "25" })
185
+ contact.get_online({ :page => "1", :per_page => "25" })
186
+ contact.get_who_added(user_id, { :page => "1", :per_page => "25", :sort => "newest" })
187
+
188
+ ### Vimeo::Advanced::Group
189
+
190
+ group = Vimeo::Advanced::Group.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
191
+
192
+ group.add_video("group_id", "video_id")
193
+ group.get_all({ :page => "1", :per_page => "25", :sort => "newest", :user_id => "user_id" })
194
+ group.get_files("group_id", { :page => "1", :per_page => "25" })
195
+ group.get_info("group_id")
196
+ group.get_members("group_id", { :page => "1", :per_page => "25", :sort => "newest" })
197
+ group.get_moderators("group_id", { :page => "1", :per_page => "25" })
198
+ group.get_video_comments("group_id", "video_id", { :page => "1", :per_page => "25" })
199
+ group.get_videos("group_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
200
+ group.join("group_id")
201
+ group.leave("group_id")
202
+
203
+ ### Vimeo::Advanced::GroupEvents
204
+
205
+ group_events = Vimeo::Advanced::GroupEvents.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
206
+
207
+ group_events.get_month("group_id", { :page => "1", :per_page => "25", :month => nil, :year => nil })
208
+ group_events.get_past("group_id", { :page => "1", :per_page => "25" })
209
+ group_events.get_year("group_id", { :page => "1", :per_page => "25" })
210
+
211
+ ### Vimeo::Advanced::GroupForums
212
+
213
+ group_forums = Vimeo::Advanced::GroupForums.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
214
+
215
+ group_forums.get_topic_comments("group_id", "topic_id", { :page => "1", :per_page => "25" })
216
+ group_forums.get_topics("group_id", { :page => "1", :per_page => "25" })
217
+
218
+ ### Vimeo::Advanced::Person
219
+
220
+ person = Vimeo::Advanced::Person.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
221
+
222
+ person.add_contact("user_id")
223
+ person.add_subscription("user_id", "types") # Types is a comma-delimited string. Valid: "likes", "appears", "uploads"
224
+ person.find_by_email("user_id")
225
+ person.get_hd_embeds
226
+ person.get_info("user_id")
227
+ person.get_portrait_urls("user_id")
228
+ person.remove_contact("user_id")
229
+ person.remove_subscription("user_id", "types") # Types is a comma-delimited string. Valid: "likes", "appears", "uploads"
230
+
231
+ ### Vimeo::Advanced::Test
232
+
233
+ test = Vimeo::Advanced::Test.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
234
+
235
+ test.echo
236
+ test.null
237
+ test.login
238
+
239
+ ### Vimeo::Advanced::Upload
240
+
241
+ upload = Vimeo::Advanced::Upload.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
242
+
243
+ upload.confirm("ticket_id", "json_manifest")
244
+ upload.get_quota
245
+ upload.get_ticket
246
+ upload.upload("path_to_file", "ticket_id", "end_point")
247
+ upload.verify_manifest("ticket_id", "json_manifest")
248
+
249
+ ### Vimeo::Advanced::Video
250
+
251
+ video = Vimeo::Advanced::Video.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
252
+
253
+ video.add_cast("video_id", "user_id", { :role => nil })
254
+ video.add_photos("video_id", "photo_urls")
255
+ video.add_tags("video_id", "tags")
256
+ video.clear_tags("video_id")
257
+ video.delete("video_id")
258
+ video.get_all("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
259
+ video.get_appears_in("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
260
+ video.get_by_tag("tag", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
261
+ video.get_cast("video_id", { :page => "1", :per_page => "25" })
262
+ video.get_contacts_liked("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
263
+ video.get_contacts_uploaded("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
264
+ video.get_info("video_id")
265
+ video.get_likes("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
266
+ video.get_subscriptions("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
267
+ video.get_thumbnail_urls("video_id")
268
+ video.get_uploaded("user_id", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest" })
269
+ video.remove_cast("video_id", "user_id")
270
+ video.remove_tag("video_id", "tag_id")
271
+ video.search("query", { :page => "1", :per_page => "25", :full_response => "0", :sort => "newest", :user_id => nil })
272
+ video.set_description("video_id", "description")
273
+ video.set_like("video_id", "like")
274
+ video.set_privacy("video_id", "privacy", { :users => nil, :password => nil })
275
+ video.set_title("video_id", "title")
276
+
277
+ # comments
278
+ video.add_comment("video_id", "comment_text", { :reply_to_comment_id => nil })
279
+ video.delete_comment("video_id", "comment_id")
280
+ video.edit_comment("video_id", "comment_id", "comment_text")
281
+ video.get_comments_list("video_id", { :page => "1", :per_page => "25" })
282
+
283
+ ### Vimeo::Advanced::VideoEmbed
284
+
285
+ video_embed = Vimeo::Advanced::VideoEmbed.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
286
+
287
+ video_embed.get_presets({ :page => "1", :per_page => "25" })
288
+ video_embed.set_preset("video_id", "preset_id")
289
+
290
+ ## Uploads
291
+
292
+ __Uploads are not working since the move to OAuth. They will be fixed shortly.__
293
+
294
+ ## Todo
295
+
296
+ * Fix tests that cannot be stubbed because of OAuth nonce.
297
+ * Better structure. There's too many classes. Is there a way to simplify the Advanced API?
298
+ * Better error handling.
299
+ * More re-factoring.
300
+ * Make tests more robust and faster. If anyone has any ideas let me know.
301
+
302
+ ## Contributors
303
+
304
+ * {matthooks}[http://github.com/matthooks]
305
+ * {sirlantis}[http://github.com/sirlantis]
306
+
307
+ ## Thanks to
308
+
309
+ * {HTTParty}[http://github.com/jnunemaker/httparty/tree/master]: Easily one of the best tools I have used since I started using Ruby.
310
+ * {Jeweler}[http://github.com/technicalpickles/jeweler/tree/master]: Great tool for creating gems for Github.
311
+
312
+ ### Copyright (c) 2009 Matt Hooks. See LICENSE for details.
data/Rakefile CHANGED
@@ -21,8 +21,8 @@ begin
21
21
  gem.extra_rdoc_files = ['README.rdoc', 'LICENSE', 'CHANGELOG.rdoc']
22
22
 
23
23
  gem.add_dependency "httparty", ">= 0.4.5"
24
- gem.add_dependency "curb", ">= 0.5.4.0"
25
24
  gem.add_dependency "json", ">= 1.1.9"
25
+ gem.add_dependency "oauth", ">= 0.3.6"
26
26
  end
27
27
  Jeweler::GemcutterTasks.new
28
28
  rescue LoadError
data/VERSION.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
- :minor: 0
2
+ :build:
3
3
  :patch: 0
4
4
  :major: 1
5
+ :minor: 1
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ require 'lib/vimeo'
data/lib/vimeo.rb CHANGED
@@ -3,6 +3,7 @@ require 'httparty'
3
3
  require 'digest/md5'
4
4
 
5
5
  $:.unshift(File.dirname(__FILE__))
6
+ require 'oauth_client'
6
7
  require 'vimeo/simple'
7
8
  require 'vimeo/advanced'
8
9
 
@@ -1,7 +1,6 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
2
  require 'advanced/base'
3
3
  require 'advanced/album'
4
- require 'advanced/auth'
5
4
  require 'advanced/channel'
6
5
  require 'advanced/contact'
7
6
  require 'advanced/group'
@@ -5,20 +5,18 @@ module Vimeo
5
5
  # Adds a video to an album.
6
6
  create_api_method :add_video,
7
7
  "vimeo.albums.addVideo",
8
- :required => [:auth_token, :album_id, :video_id]
8
+ :required => [:album_id, :video_id]
9
9
 
10
10
  # Creates an album.
11
- # FIXME: Nothing is returned if you add multiple videos to an album.
12
- # The optional parameter videos is a comma-separated list of video ids.
13
11
  create_api_method :create,
14
12
  "vimeo.albums.create",
15
- :required => [:auth_token, :title, :video_id],
13
+ :required => [:title, :video_id],
16
14
  :optional => [:description, :videos]
17
15
 
18
16
  # Deletes an album.
19
17
  create_api_method :delete,
20
18
  "vimeo.albums.delete",
21
- :required => [:auth_token, :album_id]
19
+ :required => [:album_id]
22
20
 
23
21
  # Returns a list of a user's albums.
24
22
  create_api_method :get_all,
@@ -29,28 +27,28 @@ module Vimeo
29
27
  # Returns a list of the videos in an album.
30
28
  create_api_method :get_videos,
31
29
  "vimeo.albums.getVideos",
32
- :required => [:auth_token, :album_id],
30
+ :required => [:album_id],
33
31
  :optional => [:page, :per_page, :full_response, :password]
34
32
 
35
33
  # Removes a video from an album.
36
34
  create_api_method :remove_video,
37
35
  "vimeo.albums.removeVideo",
38
- :required => [:auth_token, :album_id, :video_id]
36
+ :required => [:album_id, :video_id]
39
37
 
40
38
  # Sets the description of an album.
41
39
  create_api_method :set_description,
42
40
  "vimeo.albums.setDescription",
43
- :required => [:auth_token, :album_id, :description]
41
+ :required => [:album_id, :description]
44
42
 
45
43
  # Sets the password of an album.
46
44
  create_api_method :set_password,
47
45
  "vimeo.albums.setPassword",
48
- :required => [:auth_token, :album_id, :password]
46
+ :required => [:album_id, :password]
49
47
 
50
48
  # Sets the title of an album.
51
49
  create_api_method :set_title,
52
50
  "vimeo.albums.setTitle",
53
- :required => [:auth_token, :album_id, :title]
51
+ :required => [:album_id, :title]
54
52
 
55
53
  end # Album
56
54
  end # Advanced
@@ -1,26 +1,25 @@
1
1
  module CreateApiMethod
2
2
 
3
- # Small DSL that creates a method that calls a Vimeo method through the advanced API.
3
+ # Creates a method that calls a Vimeo Method through the Advanced API.
4
4
  #
5
5
  # @param [String] method The name of the method being created.
6
6
  # @param [String] vimeo_method The name of the advanced API Method the function should call.
7
7
  # @param [Hash] options Some optional parameters.
8
8
  # @option options [Array] :required An array of required parameters.
9
9
  # @option options [Array] :optional An array of optional parameters.
10
- # @option options [Array] :unsigned An array of required parameters that should not be included in the api_sig.
11
10
  def create_api_method(method, vimeo_method, options={})
12
- options = { :required => [], :optional => [], :unsigned => [] }.merge(options)
11
+ options = { :required => [], :optional => [] }.merge(options)
12
+
13
+ method = method.to_s
14
+ camelized_method = camelize(method, false)
13
15
 
14
16
  raise ArgumentError, 'Required parameters must be an array.' unless options[:required].is_a? Array
15
17
  raise ArgumentError, 'Optional parameters must be an array.' unless options[:optional].is_a? Array
16
- raise ArgumentError, 'Unsigned parameters must be an array.' unless options[:optional].is_a? Array
17
18
 
18
- required = (options[:required] + options[:unsigned]).map { |r| r.to_s }.join(",")
19
- required_hash = options[:required].map { |r| ":#{r} => #{r}" }.join(",")
20
- optional_hash = options[:optional].map { |o| ":#{o} => nil" }.join(",")
21
- unsigned_hash = options[:unsigned].map { |u| ":#{u} => #{u}" }.join(",")
19
+ required = options[:required].map { |r| r.to_s }.join(",")
20
+ optional = options[:optional].map { |o| ":#{o} => nil" }.join(",")
22
21
 
23
- parameters = "(#{required unless required.blank?}#{',' unless required.blank?}options={#{optional_hash}})"
22
+ parameters = "(#{required unless required.blank?}#{',' unless required.blank?}options={#{optional}})"
24
23
 
25
24
  method_string = <<-method
26
25
 
@@ -35,71 +34,82 @@ module CreateApiMethod
35
34
  #{ options[:required].map { |r| "sig_options.merge! :#{r} => #{r}"}.join("\n") }
36
35
  #{ options[:optional].map { |o| "sig_options.merge! :#{o} => options[:#{o}] unless options[:#{o}].nil?" }.join("\n") }
37
36
 
38
- make_request sig_options#{ ", :unsigned => {" + unsigned_hash + "}" unless options[:unsigned].empty? }
37
+ make_request sig_options
39
38
  end
40
39
 
40
+ alias #{camelized_method} #{method}
41
+
41
42
  method
42
43
 
43
44
  class_eval method_string
44
45
  end
45
-
46
+
47
+ protected
48
+
49
+ # taken from ActiveSupport-2.3.4, activesupport/lib/active_support/inflector.rb, line 178
50
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
51
+ if first_letter_in_uppercase
52
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
53
+ else
54
+ lower_case_and_underscored_word[0..0].downcase + camelize(lower_case_and_underscored_word)[1..-1]
55
+ end
56
+ end
46
57
  end
47
58
 
48
59
  module Vimeo
49
60
  module Advanced
50
61
 
62
+ class RequestFailed < StandardError; end
63
+
51
64
  class Base
52
- include HTTParty
53
- base_uri 'vimeo.com'
54
65
  extend CreateApiMethod
55
66
 
56
- # TODO: Pass an auth token, if you've already got one
57
- # TODO: implement format_options
58
- # Requires your API key and secret phrase.
59
- # The API key and secret are prepended to every request.
60
- def initialize(api_key, secret, format_options={})
61
- @auth = { :api_key => api_key }
62
- @secret = secret
67
+ def initialize(consumer_key, consumer_secret, options = {})
68
+ @oauth_consumer = OAuth::Consumer.new(consumer_key, consumer_secret, :site => 'http://vimeo.com', :http_method => :get, :scheme => :query_string)
69
+ unless options[:token].nil? && options[:secret].nil?
70
+ @access_token = OAuth::AccessToken.new(@oauth_consumer, options[:token], options[:secret])
71
+ end
63
72
  end
64
73
 
65
- # Generates a link that allows a user to authorize
66
- # your web application to use the advanced API
67
- def web_login_link(perms)
68
- api_sig = generate_api_sig :perms => perms
69
- "http://vimeo.com/services/auth/?api_key=#{@auth[:api_key]}&perms=#{perms}&api_sig=#{api_sig}"
74
+ def authorize_url(permission = "delete")
75
+ get_request_token.authorize_url :permission => permission
70
76
  end
71
77
 
72
- # Generates a link that allows a user to authorize
73
- # your desktop application to use the advanced API
74
- def desktop_login_link(perms, frob)
75
- api_sig = generate_api_sig :frob => frob, :perms => perms
76
- "http://vimeo.com/services/auth/?api_key=#{@auth[:api_key]}&perms=#{perms}&frob=#{frob}&api_sig=#{api_sig}"
78
+ def get_access_token(oauth_token=nil, oauth_verifier=nil)
79
+ @access_token ||= get_request_token.get_access_token :oauth_token => oauth_token, :oauth_verifier => oauth_verifier
77
80
  end
78
81
 
82
+ # TODO: Move this to OAuth
83
+ create_api_method :check_access_token,
84
+ "vimeo.oauth.checkAccessToken"
85
+
79
86
  private
80
87
 
81
- def make_request(sig_options, options={})
82
- options = { :unsigned => [] }.merge(options)
83
- unsigned = options[:unsigned]
84
-
85
- api_sig = generate_api_sig sig_options
86
- self.class.post "/api/rest/v2", :query => query(sig_options, api_sig), :body => unsigned
88
+ def get_request_token
89
+ @request_token ||= @oauth_consumer.get_request_token :scheme => :query_string
87
90
  end
88
91
 
89
- # Generates a MD5 hashed API signature for Advanced API requests
90
- def generate_api_sig(options={})
91
- # Every request requires the api_key parameter
92
- options.merge! @auth
93
- # Keys must be sorted alphabetically
94
- api_sig = options.sort { |a, b| a.to_s <=> b.to_s }.join
95
- Digest::MD5.hexdigest("#{@secret}#{api_sig}")
92
+ def make_request(options)
93
+ response = Crack::JSON.parse @oauth_consumer.request(:post, "http://vimeo.com/api/rest/v2", get_access_token, {}, options).body
94
+ validate_response! response
95
+ response
96
96
  end
97
97
 
98
- def query(sig_options, api_sig)
99
- sig_options.merge :api_key => @auth[:api_key], :api_sig => api_sig
98
+ # Raises an exception if the response does contain a +stat+ different from "ok"
99
+ def validate_response!(response)
100
+ raise "empty response" unless response
101
+
102
+ status = response["stat"]
103
+ if status and status != "ok"
104
+ error = response["err"]
105
+ if error
106
+ raise RequestFailed, "#{error["code"]}: #{error["msg"]}, explanation: #{error["expl"]}"
107
+ else
108
+ raise RequestFailed, "Error: #{status}, no error message"
109
+ end
110
+ end
100
111
  end
101
112
 
102
113
  end # Base
103
-
104
114
  end # Advanced
105
115
  end # Vimeo