has_vimeo_video 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create 1.9.2@has_vimeo_video
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in has_vimeo_video.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_vimeo_video (0.0.1)
5
+ supermodel
6
+ vimeo
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.0.11)
12
+ activesupport (= 3.0.11)
13
+ builder (~> 2.1.2)
14
+ i18n (~> 0.5.0)
15
+ activesupport (3.0.11)
16
+ builder (2.1.2)
17
+ diff-lcs (1.1.3)
18
+ httparty (0.8.1)
19
+ multi_json
20
+ multi_xml
21
+ httpclient (2.2.4)
22
+ i18n (0.5.0)
23
+ json (1.6.4)
24
+ metaclass (0.0.1)
25
+ mocha (0.10.0)
26
+ metaclass (~> 0.0.1)
27
+ multi_json (1.0.4)
28
+ multi_xml (0.4.1)
29
+ multipart-post (1.1.4)
30
+ oauth (0.4.5)
31
+ rake (0.8.7)
32
+ rspec (2.7.0)
33
+ rspec-core (~> 2.7.0)
34
+ rspec-expectations (~> 2.7.0)
35
+ rspec-mocks (~> 2.7.0)
36
+ rspec-core (2.7.1)
37
+ rspec-expectations (2.7.0)
38
+ diff-lcs (~> 1.1.2)
39
+ rspec-mocks (2.7.0)
40
+ supermodel (0.1.6)
41
+ activemodel (~> 3.0.0)
42
+ vimeo (1.4.2)
43
+ httparty (>= 0.4.5)
44
+ httpclient (>= 2.1.5.2)
45
+ json (>= 1.1.9)
46
+ multipart-post (>= 1.0.1)
47
+ oauth (>= 0.4.3)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ has_vimeo_video!
54
+ mocha
55
+ rake
56
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Engage
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Has Vimeo Video
2
+
3
+ Validate a Vimeo URL attribute in Active Record, and retrieve access to the video's data through Vimeo API.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add to your Gemfile and run the `bundle` command to install it.
9
+
10
+ ```ruby
11
+ gem "has_vimeo_video"
12
+ ```
13
+
14
+ **Requires Ruby 1.9.2 or later, and Rails 3.0.0 or later.**
15
+
16
+
17
+ ## Usage
18
+
19
+ Call `has_vimeo_video` in an ActiveRecord class and pass the name of the attribute that will contain the URL for the video on Vimeo, e.g. http://vimeo.com/1234567890.
20
+
21
+ ```ruby
22
+ class Post < ActiveRecord::Base
23
+ has_vimeo_video :video_url
24
+ end
25
+ ```
26
+
27
+ This will automatically validate the format of the URL to match Vimeo's, and will add a `vimeo` object to your model, which will contain the following methods:
28
+
29
+ * `info` will give you access to a hash with the parsed response from Vimeo API
30
+ * `id` will parse the id of the video from the URL (no API requests here)
31
+ * `embed_url` will return a URL ready for embedding your video (no API requests as well)
32
+ * `thumbnail` will return the URL for the large thumbnail from Vimeo API (the same as `info['thumbnail_large']`)
33
+
34
+ ```ruby
35
+ post = Post.create video_url: 'http://www.vimeo.com/32733074'
36
+ post.vimeo.info # {"id"=>32733074, "title"=>"Belo Monte, Anúncio de uma Guerra (CATARSE)", "description"=>"Mais do que um filme, queremos que esse documentário seja um ato político da sociedade, uma luta pelo acesso à informação e pelo direito de participar das decisões do país.<br />\n<br />\nEntre em http://catarse.me/pt/projects/459-belo-monte-anuncio-de-uma-guerra, assista o vídeo e apoie essa causa.", "url"=>"http://vimeo.com/32733074", "upload_date"=>"2011-11-27 06:49:45", "mobile_url"=>"http://vimeo.com/m/32733074", "thumbnail_small"=>"http://b.vimeocdn.com/ts/221/819/221819626_100.jpg", "thumbnail_medium"=>"http://b.vimeocdn.com/ts/221/819/221819626_200.jpg", "thumbnail_large"=>"http://b.vimeocdn.com/ts/221/819/221819626_640.jpg", "user_name"=>"André Vilela D'Elia", "user_url"=>"http://vimeo.com/cinedelia", "user_portrait_small"=>"http://b.vimeocdn.com/ps/283/288/2832886_30.jpg", "user_portrait_medium"=>"http://b.vimeocdn.com/ps/283/288/2832886_75.jpg", "user_portrait_large"=>"http://b.vimeocdn.com/ps/283/288/2832886_100.jpg", "user_portrait_huge"=>"http://b.vimeocdn.com/ps/283/288/2832886_300.jpg", "stats_number_of_likes"=>221, "stats_number_of_plays"=>107330, "stats_number_of_comments"=>49, "duration"=>661, "width"=>640, "height"=>360, "tags"=>"belo, monte, anúncio, cinedelia, gota, guerra, catarse, indigenas, xingu, rio, river, raoni, kayapo, vivo, eletro, norte, felicio, pontes, pare, amazonia", "embed_privacy"=>"anywhere"}
37
+ post.vimeo.id # 32733074
38
+ post.vimeo.embed_url # http://player.vimeo.com/video/32733074
39
+ post.vimeo.thumbnail # http://b.vimeocdn.com/ts/221/819/221819626_640.jpg
40
+ ```
41
+
42
+ ## Customizing validation message
43
+
44
+ You can customize the ActiveRecord validation message by passing the `message` option to `has_vimeo_video`.
45
+
46
+
47
+ ```ruby
48
+ class Post < ActiveRecord::Base
49
+ has_vimeo_video :video_url, message: "only Vimeo URLs are allowed"
50
+ end
51
+ ```
52
+
53
+ ## Development
54
+
55
+ Questions or problems? Please post them on the [issue tracker](https://github.com/softa/has_vimeo_video/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
56
+
57
+ This gem is created by Engage and is under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "has_vimeo_video/version"
4
+
5
+ Gem::Specification.new do |s|
6
+
7
+ s.name = "has_vimeo_video"
8
+ s.version = HasVimeoVideo::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Engage"]
11
+ s.email = ["what@engage.is"]
12
+ s.homepage = ""
13
+ s.summary = %q{Adds Vimeo integration to ActiveRecord}
14
+ s.description = %q{Adds video URL validation and API integration for using Vimeo on ActiveRecord models}
15
+
16
+ s.rubyforge_project = "has_vimeo_video"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency "vimeo"
24
+
25
+ s.add_development_dependency "rspec"
26
+ s.add_development_dependency "mocha"
27
+ s.add_development_dependency "rake"
28
+
29
+ s.add_runtime_dependency "supermodel"
30
+
31
+ end
@@ -0,0 +1,10 @@
1
+ require 'httparty'
2
+ require 'vimeo/simple'
3
+
4
+ require 'has_vimeo_video/version'
5
+ require 'has_vimeo_video/vimeo_video'
6
+ require 'has_vimeo_video/model_additions'
7
+
8
+ module HasVimeoVideo
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,16 @@
1
+ module HasVimeoVideo
2
+ module ModelAdditions
3
+
4
+ def has_vimeo_video(attribute, options = nil)
5
+ message = (options and options[:message]) || "only Vimeo URLs are allowed"
6
+ validates_format_of attribute, with: HasVimeoVideo::VimeoVideo.regex, message: message
7
+ self.class_eval <<-EOF
8
+ def vimeo
9
+ return @vimeo if @vimeo
10
+ @vimeo = HasVimeoVideo::VimeoVideo.new self.#{attribute}
11
+ end
12
+ EOF
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module HasVimeoVideo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ module HasVimeoVideo
2
+ class VimeoVideo
3
+
4
+ def self.regex
5
+ /http:\/\/(www\.)?vimeo.com\/(\d+)/
6
+ end
7
+
8
+ def initialize url
9
+ @url = url
10
+ end
11
+
12
+ def id
13
+ return @id if @id
14
+ return unless @url
15
+ if result = @url.match(self.class.regex)
16
+ @id = result[2]
17
+ end
18
+ end
19
+
20
+ def info
21
+ return @info if @info
22
+ return unless id
23
+ @info = Vimeo::Simple::Video.info(id)
24
+ if @info.parsed_response and @info.parsed_response[0]
25
+ @info = @info.parsed_response[0]
26
+ else
27
+ @info = nil
28
+ end
29
+ rescue
30
+ @info = nil
31
+ end
32
+
33
+ def embed_url
34
+ "http://player.vimeo.com/video/#{id}"
35
+ end
36
+
37
+ def thumbnail
38
+ info["thumbnail_large"]
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ class Post < SuperModel::Base
4
+ extend HasVimeoVideo::ModelAdditions
5
+ has_vimeo_video :video_url
6
+ end
7
+
8
+ class PostWithMessage < SuperModel::Base
9
+ extend HasVimeoVideo::ModelAdditions
10
+ has_vimeo_video :video_url, message: "must be a Vimeo URL"
11
+ end
12
+
13
+ describe HasVimeoVideo::ModelAdditions do
14
+
15
+ subject{ Post.new video_url: "http://vimeo.com/32733074" }
16
+
17
+ its(:vimeo) do
18
+ subject.id.empty?.should be_false
19
+ subject.info.empty?.should be_false
20
+ subject.embed_url.empty?.should be_false
21
+ subject.thumbnail.empty?.should be_false
22
+ end
23
+
24
+ it "should have a valid Vimeo video URL" do
25
+ Post.new(video_url: "http://www.vimeo.com/foobar").should_not be_valid
26
+ Post.new(video_url: "http://vimeo.com/foobar").should_not be_valid
27
+ Post.new(video_url: "http://www.youtube.com/17298435").should_not be_valid
28
+ Post.new(video_url: "http://youtube.com/17298435").should_not be_valid
29
+
30
+ Post.new(video_url: "http://www.vimeo.com/17298435").should be_valid
31
+ Post.new(video_url: "http://vimeo.com/17298435").should be_valid
32
+ end
33
+
34
+ it "should have a customized error message on validation failures" do
35
+ subject = Post.new(video_url: "http://youtube.com/17298435")
36
+ subject.should_not be_valid
37
+ subject.errors[:video_url].should == ["only Vimeo URLs are allowed"]
38
+ subject = PostWithMessage.new(video_url: "http://youtube.com/17298435")
39
+ subject.should_not be_valid
40
+ subject.errors[:video_url].should == ["must be a Vimeo URL"]
41
+ end
42
+
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasVimeoVideo::VimeoVideo do
4
+
5
+ def new_video url
6
+ HasVimeoVideo::VimeoVideo.new(url)
7
+ end
8
+
9
+ subject{ new_video "http://vimeo.com/32733074" }
10
+
11
+ its(:id){ should == "32733074" }
12
+
13
+ its(:info) do
14
+ subject["id"].should == 32733074
15
+ subject["title"].empty?.should be_false
16
+ subject["description"].empty?.should be_false
17
+ subject["url"].empty?.should be_false
18
+ subject["thumbnail_small"].empty?.should be_false
19
+ subject["thumbnail_medium"].empty?.should be_false
20
+ subject["thumbnail_large"].empty?.should be_false
21
+ end
22
+
23
+ its(:embed_url){ should == "http://player.vimeo.com/video/32733074" }
24
+
25
+ it "should have a thumbnail that is equal to Vimeo's 'thumbnail_large'" do
26
+ subject.thumbnail.should == subject.info["thumbnail_large"]
27
+ end
28
+
29
+ it "should have a nil info object if the video doesn't exist" do
30
+ new_video("http://vimeo.com/000000000").info.should be_nil
31
+ end
32
+
33
+ it "should correctly parse video_url" do
34
+ new_video(" http://vimeo.com/6428069 ").id.should == "6428069"
35
+ new_video("xyzhttp://vimeo.com/6428069bar").id.should == "6428069"
36
+ end
37
+
38
+ it "should have a nil info object even if we get an error from Vimeo" do
39
+ Vimeo::Simple::Video.stubs(:info).returns(Exception.new)
40
+ subject.info.should be_nil
41
+ end
42
+
43
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasVimeoVideo do
4
+
5
+
6
+
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'has_vimeo_video'
2
+ require 'supermodel'
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :mocha
6
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_vimeo_video
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Engage
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-12-30 00:00:00 -02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: vimeo
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: rake
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: supermodel
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :runtime
84
+ version_requirements: *id005
85
+ description: Adds video URL validation and API integration for using Vimeo on ActiveRecord models
86
+ email:
87
+ - what@engage.is
88
+ executables: []
89
+
90
+ extensions: []
91
+
92
+ extra_rdoc_files: []
93
+
94
+ files:
95
+ - .gitignore
96
+ - .rspec
97
+ - .rvmrc
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - has_vimeo_video.gemspec
104
+ - lib/has_vimeo_video.rb
105
+ - lib/has_vimeo_video/model_additions.rb
106
+ - lib/has_vimeo_video/version.rb
107
+ - lib/has_vimeo_video/vimeo_video.rb
108
+ - spec/has_vimeo_video/model_additions_spec.rb
109
+ - spec/has_vimeo_video/vimeo_video_spec.rb
110
+ - spec/has_vimeo_video_spec.rb
111
+ - spec/spec_helper.rb
112
+ has_rdoc: true
113
+ homepage: ""
114
+ licenses: []
115
+
116
+ post_install_message:
117
+ rdoc_options: []
118
+
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ requirements: []
138
+
139
+ rubyforge_project: has_vimeo_video
140
+ rubygems_version: 1.3.7
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Adds Vimeo integration to ActiveRecord
144
+ test_files:
145
+ - spec/has_vimeo_video/model_additions_spec.rb
146
+ - spec/has_vimeo_video/vimeo_video_spec.rb
147
+ - spec/has_vimeo_video_spec.rb
148
+ - spec/spec_helper.rb