rbvimeo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Matt Pruitt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,28 +1,52 @@
1
1
  rbVimeo
2
- by Matt Pruitt
2
+
3
+ Matt Pruitt
4
+
3
5
  www.guitsaru.com
4
6
 
5
- == DESCRIPTION:
7
+ h2. Description
6
8
 
7
9
  A ruby wrapper for the Vimeo API
8
10
 
9
- == FEATURES/PROBLEMS:
11
+ h2. Features/Problems
10
12
 
11
13
  This does not handle authentication. It only gets public videos from Vimeo.
12
14
 
13
- == SYNOPSIS:
15
+ h2. Usage
16
+
17
+ @vimeo = RBVIMEO::Vimeo.new(api_key, api_signature)@
18
+
19
+ @video = vimeo.video(video_id)@
20
+
21
+ <br />
22
+
23
+ See the docs for all Video properties
24
+
25
+ @video.title@
26
+
27
+ @video.caption@
28
+
29
+ @video.comments # Returns an array of comment objects@
30
+
31
+ @video.likes@
32
+
33
+ @video.plays@
34
+
35
+ h2. Documentation
36
+
37
+ "Documentation":http://rbvimeo.rubyforge.org
38
+
39
+ h2. Installation
14
40
 
15
- vimeo = RBVIMEO::Vimeo.new(api_key, api_signature)
16
- vid = vimeo.video(video_id)
41
+ <pre>sudo gem install guitsaru-rbvimeo -s http://gems.github.com</pre>
17
42
 
18
- == INSTALL:
43
+ h2. Contributing
19
44
 
20
- To install, run 'gem install rbvimeo'
45
+ Fork the project on "github.":http://github.com/guitsaru/rbvimeo
21
46
 
22
- In order to run the specs, first make a test_settings.yml file (a sample is
23
- provided) using your api key and api secret.
47
+ In order to run the specs, edit the test_settings.yml file (a sample is provided) using your api key and api secret.
24
48
 
25
- == LICENSE:
49
+ h2. License
26
50
 
27
51
  (The MIT License)
28
52
 
data/Rakefile CHANGED
@@ -1,21 +1,83 @@
1
- # Look in the tasks/setup.rb file for the various options that can be
2
- # configured in this Rakefile. The .rake files in the tasks directory
3
- # are where the options are used.
1
+ require 'rubygems'
2
+ require 'rake'
4
3
 
5
- load 'tasks/setup.rb'
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rbvimeo"
8
+ gem.summary = %Q{A ruby wrapper for the vimeo api.}
9
+ gem.email = "guitsaru@gmail.com"
10
+ gem.homepage = "http://github.com/guitsaru/rbvimeo"
11
+ gem.authors = ["Matt Pruitt"]
12
+ gem.rubyforge_project = "rbvimeo"
13
+ gem.add_dependency('hpricot', '>= 0.6')
14
+
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
19
+ end
6
20
 
7
- ensure_in_path 'lib'
8
- require 'rbVimeo'
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
9
27
 
10
- task :default => 'spec:run'
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
11
40
 
12
- PROJ.name = 'rbvimeo'
13
- PROJ.authors = 'Matt Pruitt'
14
- PROJ.email = 'guitsaru@gmail.com'
15
- PROJ.url = 'www.guitsaru.com'
16
- PROJ.rubyforge.name = 'rbvimeo'
17
- PROJ.version = '0.2.0'
18
- PROJ.spec.opts << '--color'
19
- PROJ.gem.dependencies << 'hpricot'
20
41
 
21
- # EOF
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION.yml')
47
+ config = YAML.load(File.read('VERSION.yml'))
48
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "rbvimeo #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
59
+ begin
60
+ require 'rake/contrib/sshpublisher'
61
+ namespace :rubyforge do
62
+
63
+ desc "Release gem and RDoc documentation to RubyForge"
64
+ task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
65
+
66
+ namespace :release do
67
+ desc "Publish RDoc to RubyForge."
68
+ task :docs => [:rdoc] do
69
+ config = YAML.load(
70
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
71
+ )
72
+
73
+ host = "#{config['username']}@rubyforge.org"
74
+ remote_dir = "/var/www/gforge-projects/rbvimeo/"
75
+ local_dir = 'rdoc'
76
+
77
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
78
+ end
79
+ end
80
+ end
81
+ rescue LoadError
82
+ puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
83
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 3
4
+ :patch: 0
@@ -0,0 +1,21 @@
1
+ module RBVIMEO
2
+ class Comment
3
+ attr_accessor :id, :author, :authorname, :datecreate, :permalink, :text, :portraits
4
+
5
+ def initialize
6
+ @portraits = []
7
+ end
8
+
9
+ def date
10
+ return @datecreate
11
+ end
12
+
13
+ def url
14
+ return @permalink
15
+ end
16
+
17
+ def id
18
+ return @id.to_i
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,9 @@
1
1
  module RBVIMEO
2
2
  class User
3
3
  attr_accessor :id, :username, :fullname
4
+
5
+ def id
6
+ return @id.to_i
7
+ end
4
8
  end
5
9
  end
@@ -0,0 +1,138 @@
1
+ module RBVIMEO
2
+ class Video
3
+ attr_reader :id, :title, :caption, :upload_date, :number_of_likes, :number_of_plays
4
+ attr_reader :number_of_comments, :width, :height, :owner, :tags, :url
5
+ attr_reader :thumbs
6
+
7
+
8
+ # Fetches data about a video from the Vimeo site
9
+ # id is the id of the the Vimeo video
10
+ # vimeo is an instance of RBVIMEO::Vimeo
11
+ #
12
+ # To load a movie with vimeo id 339189:
13
+ # @vimeo = RBVIMEO::Vimeo.new api_key, api_secret
14
+ # video = RBVIMEO::Video.new 339189, @vimeo
15
+ def initialize id, vimeo
16
+ @thumbs = []
17
+ @comments = []
18
+ @id = id
19
+ @vimeo = vimeo
20
+
21
+ url = vimeo.generate_url({"method" => "vimeo.videos.getInfo",
22
+ "video_id" => id, "api_key" => vimeo.api_key}, "read")
23
+
24
+ xml_doc = @vimeo.get_xml(url)
25
+
26
+ return @id = -1 if parse_xml(xml_doc).nil?
27
+ end
28
+
29
+ # Returns the code to embed the video
30
+ def embed width, height
31
+ string = <<EOF
32
+ <object type="application/x-shockwave-flash" width=#{width} height=#{height} data="http://www.vimeo.com/moogaloop.swf?clip_id=#{@id}&server=www.vimeo.com&fullscreen=0&show_title=0'&show_byline=0&showportrait=0&color=00ADEF">
33
+ <param name="quality" value="best" />
34
+ <param name="allowfullscreen" value="false" />
35
+ <param name="scale" value="showAll" />
36
+ <param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=#{@id}&server=www.vimeo.com&fullscreen=0&show_title=0&show_byline=0&showportrait=0&color=00ADEF" /></object>
37
+ EOF
38
+ string.gsub("\n", "")
39
+ end
40
+
41
+ def comments
42
+ get_comments if @comments.empty?
43
+ return @comments
44
+ end
45
+
46
+ def likes
47
+ @number_of_likes.to_i
48
+ end
49
+
50
+ def plays
51
+ @number_of_plays.to_i
52
+ end
53
+
54
+ def num_comments
55
+ @number_of_comments.to_i
56
+ end
57
+
58
+ def number_of_likes
59
+ @number_of_likes.to_i
60
+ end
61
+
62
+ def number_of_plays
63
+ @number_of_plays.to_i
64
+ end
65
+
66
+ def number_of_comments
67
+ @number_of_comments.to_i
68
+ end
69
+
70
+ def height
71
+ @height.to_i
72
+ end
73
+
74
+ def width
75
+ @width.to_i
76
+ end
77
+
78
+ private
79
+ # Parses data using the xml recieved from the Vimeo REST API
80
+ # Should not need to be called by anything other than the initialize method
81
+ def parse_xml xml_doc
82
+ return nil if xml_doc.at("title").nil?
83
+ @id = id
84
+
85
+ %w[title caption upload_date number_of_likes number_of_plays width height number_of_comments url].each do |attribute|
86
+ instance_variable_set("@#{attribute}", xml_doc.at(attribute).inner_html)
87
+ end
88
+
89
+ @owner = User.new
90
+ %w[id username fullname].each do |attribute|
91
+ @owner.instance_variable_set("@#{attribute}", xml_doc.at("owner").attributes[attribute])
92
+ end
93
+
94
+ (xml_doc/:thumbnail).each do |thumbnail|
95
+ @thumbs << build_thumbnail(thumbnail)
96
+ end
97
+ end
98
+
99
+ # Fetches the comments for the specified Video
100
+ # id is the id of the Vimeo video
101
+ # vimeo is an instance of RBVIMEO::Vimeo
102
+ # returns an Array of comments
103
+ #
104
+ # To load a movie with vimeo id 339189:
105
+ #
106
+ # comments = video.comments 339189, @vimeo
107
+ def get_comments
108
+ xml_doc = @vimeo.get_xml(@vimeo.generate_url({"method" => "vimeo.videos.comments.getList",
109
+ "video_id" => @id, "api_key" => @vimeo.api_key}, "read"))
110
+
111
+ (xml_doc/:comment).each do |comment|
112
+ @comments << build_comment(comment)
113
+ end
114
+ return self
115
+ end
116
+
117
+ def build_comment(c)
118
+ comment = Comment.new
119
+
120
+ comment.text = c.children.select{|e| e.text?}.join
121
+
122
+ %w[id author authorname datecreate permalink].each do |attribute|
123
+ comment.instance_variable_set("@#{attribute}", c.attributes[attribute])
124
+ end
125
+
126
+ (c/'portraits'/'portrait').each do |portrait|
127
+ comment.portraits << build_thumbnail(portrait)
128
+ end
129
+
130
+ return comment
131
+ end
132
+
133
+ def build_thumbnail(t)
134
+ thumbnail = Thumbnail.new(t.inner_html, t.attributes['width'].to_i, t.attributes['height'].to_i)
135
+ end
136
+
137
+ end
138
+ end
@@ -1,25 +1,11 @@
1
- # Matt Pruitt
2
- # Ruby Library for working with Vimeo
3
- # Based on the sample PHP Vimeo API
4
-
5
- require 'digest/md5'
6
- require 'net/http'
7
- require 'rexml/document'
8
-
9
- require 'Video'
10
- require 'Thumbnail'
11
- require 'User'
12
- require 'Comment'
13
-
14
-
15
1
  module RBVIMEO
16
2
  class Vimeo
17
3
  attr_accessor :api_key, :api_secret
18
-
4
+
19
5
  @@API_REST_URL = "http://www.vimeo.com/api/rest"
20
6
  @@API_AUTH_URL = "http://www.vimeo.com/services/auth/"
21
7
  @@API_UPLOAD_URL = "http://www.vimeo.com/services/upload/"
22
-
8
+
23
9
  # api_key and api_secret should both be generated on www.vimeo.com
24
10
  def initialize api_key, api_secret
25
11
  @api_key = api_key
@@ -38,7 +24,12 @@ module RBVIMEO
38
24
  url += "&api_sig=#{generate_signature(parameters)}"
39
25
  return url
40
26
  end
41
-
27
+
28
+ # Returns the xml from the given url
29
+ def get_xml url
30
+ return Hpricot(open(url))
31
+ end
32
+
42
33
  # parameters is a hash
43
34
  def generate_signature parameters
44
35
  temp = ''
@@ -49,15 +40,15 @@ module RBVIMEO
49
40
  signature = @api_secret + temp
50
41
  Digest::MD5.hexdigest(signature)
51
42
  end
52
-
43
+
53
44
  # Provides easier access to RBVIMEO::Video
54
45
  # video = @vimeo.video 339189
55
- def video id, xml=nil
56
- vid = Video.new(id, self, xml)
46
+ def video id
47
+ vid = Video.new(id, self)
57
48
  return nil if vid.id == -1
58
49
  return vid
59
50
  end
60
-
51
+
61
52
  # Provides easier access to RBVIMEO::User
62
53
  # user = @vimeo.user
63
54
  def user
data/lib/rbvimeo.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'digest/md5'
3
+ require 'open-uri'
4
+ require 'hpricot'
5
+
6
+ require 'rbvimeo/vimeo'
7
+ require 'rbvimeo/video'
8
+ require 'rbvimeo/thumbnail'
9
+ require 'rbvimeo/user'
10
+ require 'rbvimeo/comment'
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class RbvimeoTest < Test::Unit::TestCase
4
+ context "Vimeo" do
5
+ setup do
6
+ @api_key = '56a9c1c65e700ca10a678a2bcd8e77af'
7
+ @api_secret = '59c629610'
8
+ @vimeo = RBVIMEO::Vimeo.new(@api_key, @api_secret)
9
+ @params = {"method" => "vimeo.videos.getInfo", "video_id" => "339189", "api_key" => @api_key}
10
+ end
11
+
12
+ should "initialize" do
13
+ assert_not_nil(@api_key)
14
+ assert_not_nil(@api_secret)
15
+ end
16
+
17
+ should "generate a signature" do
18
+ assert_equal("09cc6d8b963c73caf647e436b2147810", @vimeo.generate_signature(@params))
19
+ end
20
+
21
+ should "generate a url" do
22
+ assert_equal("http://www.vimeo.com/api/rest?api_key=#{@api_key}&method=vimeo.videos.getInfo&video_id=339189&api_sig=09cc6d8b963c73caf647e436b2147810", @vimeo.generate_url(@params, "read"))
23
+ end
24
+
25
+ should "generate a video" do
26
+ test_video_xml_file = File.join(File.dirname(__FILE__), %w[XML/339189.xml])
27
+
28
+ video_hpricot = open(test_video_xml_file) {|file| Hpricot(file)}
29
+ @vimeo.stubs(:get_xml).returns(video_hpricot)
30
+
31
+ @vid = @vimeo.video(339189)
32
+
33
+ assert_not_nil(@vid)
34
+ end
35
+
36
+ should "generate a user" do
37
+ assert_not_nil(@vimeo.user)
38
+ end
39
+
40
+ should "return an xml file" do
41
+ test_video_xml_file = File.join(File.dirname(__FILE__), %w[XML/339189.xml])
42
+
43
+ video_hpricot = open(test_video_xml_file) {|file| Hpricot(file)}
44
+ @vimeo.stubs(:get_xml).returns(video_hpricot)
45
+
46
+ assert_not_nil(@vimeo.get_xml(@vimeo.generate_url(@params, "read")).at('video'))
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'rbvimeo'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,100 @@
1
+ require 'test_helper'
2
+
3
+ class VideoTest < Test::Unit::TestCase
4
+ context "initialization" do
5
+ setup do
6
+ @api_key = '56a9c1c65e700ca10a678a2bcd8e77af'
7
+ @api_secret = '59c629610'
8
+ @vimeo = RBVIMEO::Vimeo.new(@api_key, @api_secret)
9
+ @params = {"method" => "vimeo.videos.getInfo", "video_id" => "339189", "api_key" => @api_key}
10
+ end
11
+
12
+ context "a valid video" do
13
+ setup do
14
+ test_video_xml_file = File.join(File.dirname(__FILE__), %w[XML/339189.xml])
15
+
16
+ video_hpricot = open(test_video_xml_file) {|file| Hpricot(file)}
17
+ @vimeo.stubs(:get_xml).returns(video_hpricot)
18
+
19
+ @vid = @vimeo.video(339189)
20
+ end
21
+
22
+ should "have a title" do
23
+ assert_equal("Upload Tutorial", @vid.title.chomp)
24
+ end
25
+
26
+ should "have a caption" do
27
+ assert_equal("This is a tutorial about our new Uploader. Enjoy!", @vid.caption.chomp)
28
+ end
29
+
30
+ should "have an upload date" do
31
+ assert_equal("2007-10-12 16:30:32", @vid.upload_date)
32
+ end
33
+
34
+ should "have likes" do
35
+ assert_equal(174, @vid.likes)
36
+ assert_equal(174, @vid.number_of_likes)
37
+ end
38
+
39
+ should "have plays" do
40
+ assert_equal(515407, @vid.plays)
41
+ assert_equal(515407, @vid.number_of_plays)
42
+ end
43
+
44
+ should "have comments" do
45
+ test_video_xml_file = File.join(File.dirname(__FILE__), %w[XML/339189.comments.xml])
46
+
47
+ comment_hpricot = open(test_video_xml_file) {|file| Hpricot(file)}
48
+ @vimeo.stubs(:get_xml).returns(comment_hpricot)
49
+
50
+ assert_equal(34, @vid.num_comments)
51
+ assert_equal(34, @vid.number_of_comments)
52
+ assert_equal(265313, @vid.comments[0].id)
53
+ assert_equal("ctd3", @vid.comments[0].author)
54
+ assert_equal("CTD3", @vid.comments[0].authorname)
55
+ assert_equal("2007-10-12 17:47:13", @vid.comments[0].date)
56
+ assert_equal("http://www.vimeo.com/339189#comment_265313", @vid.comments[0].url)
57
+ assert_match(/Sure is! Great changes!/, @vid.comments[0].text)
58
+ assert_equal("http://80.media.vimeo.com/d1/5/35/44/42/portrait-35444268.jpg", @vid.comments[0].portraits[0].url)
59
+ assert_equal(24, @vid.comments[0].portraits[0].width)
60
+ assert_equal(24, @vid.comments[0].portraits[0].height)
61
+ end
62
+
63
+ should "have an owner" do
64
+ assert_equal(152184, @vid.owner.id)
65
+ assert_equal("staff", @vid.owner.username)
66
+ assert_equal("Vimeo Staff", @vid.owner.fullname)
67
+ end
68
+
69
+ should "have a url" do
70
+ assert_equal("http://www.vimeo.com/339189/l:app-230", @vid.url)
71
+ end
72
+
73
+ should "have a thumbnail" do
74
+ assert_equal("http://40.media.vimeo.com/d1/5/36/63/98/thumbnail-36639824.jpg", @vid.thumbs[0].url)
75
+ assert_equal(96, @vid.thumbs[0].width)
76
+ assert_equal(72, @vid.thumbs[0].height)
77
+ end
78
+
79
+ should "have embed code" do
80
+ assert_not_nil(@vid.embed(240, 480))
81
+ end
82
+ end
83
+
84
+ context "an invalid video" do
85
+ setup do
86
+ not_found_xml_file = File.join(File.dirname(__FILE__), %w[XML/not_found.xml])
87
+
88
+ video_hpricot = open(not_found_xml_file) {|file| Hpricot(file)}
89
+ @vimeo.stubs(:get_xml).returns(video_hpricot)
90
+
91
+ @vid = @vimeo.video(-34)
92
+ end
93
+
94
+ should "return nil on nonexistant video" do
95
+ assert_nil(@vid)
96
+ end
97
+ end
98
+
99
+ end
100
+ end