用 ruby 特性实现的 Vimeo API
两个 modules
require 'rubygems'
require 'httparty'
require 'digest/md5'
从上面可以看见它使用了 'httparty'
所以我们可以从 ’提供方‘ 提供的数据中选择自己所真正需要的
user_info = Vimeo::Simple::User.info("matthooks")
# =>
# {
# "id":"888046",
# "display_name":"Matt Hooks",
# "created_on":"2008-10-30 14:17:32",
# "is_staff":"0",
# "is_plus":"0",
# "location":"Chicago, IL",
# "url":"http:\/\/blackholeinthemidwest.com\/",
# "bio":"",
# "profile_url":"http:\/\/vimeo.com\/matthooks",
# "videos_url":"http:\/\/vimeo.com\/matthooks\/videos",
# "total_videos_appears_ined":2,
# "total_videos_appears_in":0,
# "total_videos_liked":2,
# "total_contacts":3,
# "total_albums":0,
# "total_channels":1,
# "portrait_small":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_30.jpg",
# "portrait_medium":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_75.jpg",
# "portrait_large":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_100.jpg",
# "portrait_huge":"http:\/\/images.vimeo.com\/11\/42\/16\/114216178\/114216178_300.jpg"
# }
``` ruby
这样使用
user_info["location"]
# => "Chicago, IL"
从其目录结构就不难看出 * Vimeo::Simple * 包括
Vimeo::Simple::Activity
Vimeo::Simple::Album
Vimeo::Simple::Channel
Vimeo::Simple::Group
Vimeo::Simple::User
Vimeo::Simple::Video
而 * Vimeo::Advanced * 则包括
Vimeo::Advanced::Album
Vimeo::Advanced::Base
Vimeo::Advanced::Channel
Vimeo::Advanced::Contact
Vimeo::Advanced::Group
Vimeo::Advanced::GroupEvents
Vimeo::Advanced::GroupForums
Vimeo::Advanced::Person
Vimeo::Advanced::Test
Vimeo::Advanced::Upload
Vimeo::Advanced::Video
Vimeo::Advanced::VideoEmbed
(共12个)
(使用时需要 OAuth !所以你还需要做一些认准)
具体如下:
** First, instantiate the Base class:
base = Vimeo::Advanced::Base.new("consumer_key", "consumer_secret")
** Get a request token, and save the token secret in the session hash.
request_token = base.get_request_token
session[:oauth_secret] = request_token.secret
** Then, send your user to the authorization URL:
redirect_to base.authorize_url
** 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.
base = Vimeo::Advanced::Base.new("consumer_key", "consumer_secret")
access_token = base.get_access_token(params[:oauth_token], session[:oauth_secret], params[:oauth_verifier])
# You'll want to hold on to the user's access token and secret. I'll save it to the database.
user.token = access_token.token
user.secret = access_token.secret
user.save
** Now you've got everything you need to use the Advanced API. Let's get a user's videos:
video = Vimeo::Advanced::Video.new("consumer_key", "consumer_secret", :token => user.token, :secret => user.secret)
video.get_videos("matthooks")
# => {"videos"=> { ... }, "stat"=>"ok", "generated_in"=>"0.5753"}
Piece of cake.
** Some methods have optional variables. Pass these as a hash at the end of a call.
video.get_all("matthooks", :page => "2", :per_page => "50")