media_meta_hash 0.0.1.1.alpha → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 daltonrenaldo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MediaMetaHash
2
2
 
3
- TODO: Write a gem description
3
+ TODO: Given the url to a video (youtube, vimeo, foxnews, foxbusiness), this return a hash for opengraph (og) and twitter cards which can be used with meta-tags gem to create the html tags
4
4
 
5
5
  ## Installation
6
6
 
@@ -27,3 +27,12 @@ TODO: Write usage instructions here
27
27
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
28
  4. Push to the branch (`git push origin my-new-feature`)
29
29
  5. Create new Pull Request
30
+
31
+
32
+ ## Change Log
33
+ #### Version 0.0.2
34
+ 1. Returns given ```opts``` or ```{}``` if url is not supported
35
+ 2. Partial support for CNBC (no description, no title)
36
+
37
+
38
+
@@ -1,3 +1,3 @@
1
1
  module MediaMetaHash
2
- VERSION = "0.0.1.1.alpha"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -5,15 +5,9 @@ require 'ostruct'
5
5
  module MediaMetaHash
6
6
  HASH_TYPE = Hash.new(:article)
7
7
 
8
- def self.media_meta_hash media_type, url, opts = {}
9
- HASH_TYPE[:video] = self.method("video_hash").call(url, opts)
10
- HASH_TYPE[:article] = self.method("article_hash").call(url, opts)
11
- hash = HASH_TYPE[media_type]
12
- hash
13
- end
14
8
 
15
- def self.get_video_src id
16
- "http://www.youtube.com/v/#{id}?autohide=1&version=3"
9
+ def self.for url, media_type = :video, opts = {}
10
+ self.media_meta_hash(media_type, url, opts)
17
11
  end
18
12
 
19
13
  def self.video_info url
@@ -22,17 +16,42 @@ module MediaMetaHash
22
16
  id = $2
23
17
  OpenStruct.new(
24
18
  :video_id => id,
25
- :provider => "fox_#{partial_domain}",
26
19
  :embed_url => "http://video.fox#{partial_domain}.com/v/video-embed.html?video_id=#{id}",
27
20
  :title => "",
28
21
  :image => "",
29
22
  :description => "",
30
- :provider => "fox#{partial_domain}"
23
+ :provider => "fox#{partial_domain}",
24
+ :width => 480,
25
+ :height => (480 * 0.5625).to_i
26
+ )
27
+ elsif url =~ /video\.cnbc\.com/ && url =~ /video=(\d*)/
28
+ id = $1
29
+ OpenStruct.new(
30
+ :video_id => id,
31
+ :embed_url => "http://video.cnbc.com/gallery/?video=#{id}",
32
+ :title => "",
33
+ :image => "",
34
+ :description => "",
35
+ :provider => "cnbc",
36
+ :width => 480,
37
+ :height => (480 * 0.5625).to_i
31
38
  )
32
39
  else
33
40
  info = VideoInfo.get(url)
34
- if url =~ /(youtube.com|youtu.be)/
35
- info.embed_url = "http://www.youtube.com/v/#{info.video_id}?autohide=1&version=3"
41
+
42
+ if url =~ /(youtube.com|youtu.be)/ && info
43
+ class << info
44
+ def og_url=(val)
45
+ @url = val
46
+ end
47
+
48
+ def og_url
49
+ @url
50
+ end
51
+ end
52
+ info.og_url = self.get_video_src info.video_id
53
+ info.width = 480 if info.width == nil
54
+ info.height = (480 * 0.5625).to_i if info.height == nil
36
55
  end
37
56
  info
38
57
  end
@@ -40,25 +59,54 @@ module MediaMetaHash
40
59
 
41
60
  def self.video_hash url, opts
42
61
  video = self.video_info(url)
43
- common = { :title => video.title,
44
- :description => video.description,
45
- :image => video.thumbnail_medium
46
- }
47
-
48
- { :og => { :video => video.embed_url,
49
- :type => "video"
50
- }.merge!(common).merge!(opts),
51
-
52
- :twitter => { :player => video.embed_url,
53
- :card => "player"
54
- }.merge!(common).merge!(self.twitter_mobile(video.provider.downcase.to_sym, video.video_id)).merge!(opts)
55
- }
62
+
63
+ if video
64
+ common = { :title => video.title,
65
+ :description => video.description,
66
+ :image => video.thumbnail_medium
67
+ }
68
+
69
+ { :og => { :video => [video.og_url || video.embed_url,
70
+ {:height => video.height,
71
+ :width => video.width }],
72
+ :type => "video"
73
+ }.merge!(common).merge!(opts),
74
+
75
+ :twitter => { :player => [video.embed_url.sub("http://", "https://"),
76
+ { :width => video.width,
77
+ :height => video.height
78
+ }],
79
+ :card => "player"
80
+ }.merge!(common).merge!(self.twitter_mobile(video.provider.downcase.to_sym, video.video_id)).merge!(opts)
81
+ }
82
+ else
83
+ {}.merge!(opts)
84
+ end
85
+
56
86
  end
57
87
 
58
88
  def self.article_hash url, opts
59
89
  { :og => { :url => url }.merge!(opts), :twitter => {}.merge!(opts) }
60
90
  end
61
91
 
92
+ private
93
+
94
+ def self.ios_youtube_url id
95
+ "vnd.youtube://watch/#{id}"
96
+ end
97
+
98
+ def self.get_video_src id
99
+ "http://www.youtube.com/v/#{id}?autohide=1&version=3"
100
+ end
101
+
102
+ def self.media_meta_hash media_type, url, opts = {}
103
+ if media_type == :video
104
+ self.video_hash(url, opts)
105
+ else
106
+ self.article_hash(url, opts)
107
+ end
108
+ end
109
+
62
110
  def self.twitter_mobile provider, id
63
111
  tags_for = Hash.new({})
64
112
  tags_for[:youtube] = {
@@ -76,13 +124,21 @@ module MediaMetaHash
76
124
  }
77
125
  }
78
126
  }
127
+ tags_for[:vimeo] = {
128
+ :app => { :name => { :iphone => "Vimeo",
129
+ :ipad => "Vimeo",
130
+ :googleplay => "Vimeo"
131
+ },
132
+ :id => { :iphone => "425194759",
133
+ :ipad => "425194759",
134
+ :googleplay => "com.vimeo.android.videoapp"
135
+ },
136
+ :url => { :iphone => "vimeo://video/#{id}"}
137
+ }
138
+
139
+ }
79
140
 
80
141
  tags_for[provider]
81
142
  end
82
143
 
83
- private
84
-
85
- def self.ios_youtube_url id
86
- "vnd.youtube://watch/#{id}"
87
- end
88
144
  end
@@ -7,10 +7,10 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "media_meta_hash"
8
8
  spec.version = MediaMetaHash::VERSION
9
9
  spec.authors = ["Renaldo Pierre-Louis"]
10
- spec.email = ["rpierrelouis@hedgeye.com"]
11
- spec.description = %q{}
12
- spec.summary = %q{Given a url, you get a meta hash for the type of url}
13
- spec.homepage = ""
10
+ spec.email = ["pirelouisd87@gmail.com"]
11
+ spec.description = %q{Given the url to a video (youtube, vimeo, foxnews, foxbusiness), this return a hash for opengraph (og) and twitter cards which can be used with meta-tags gem to create the html tags}
12
+ spec.summary = %q{This gem takes video url (youtube, vimeo, foxnews, foxbusiness) and returns a hash for og and twitter card}
13
+ spec.homepage = "https://github.com/daltonrenaldo/meta_media_hash"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.add_dependency('video_info')
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rspec-mocks"
25
27
  end
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ describe MediaMetaHash do
4
+ ARTICLE_URL = "http://google.com"
5
+ YOUTUBE_URL = "http://www.youtube.com/watch?v=ZQCINxTQlN4"
6
+ FOX_URL = "http://video.foxbusiness.com/v/2734961183001/a-breach-of-privacy-between-the-irs-and-white-house/"
7
+ VIMEO_URL = "http://vimeo.com/74868773"
8
+ CNBC_URL = "http://video.cnbc.com/gallery/?play=1&video=3000146612"
9
+
10
+ def basic_info opts = {}
11
+ OpenStruct.new({:title => "Boats",
12
+ :description => "Something",
13
+ :image => "http://image.com",
14
+ :type => "video",
15
+ :provider => "vimeo",
16
+ :embed_url => "http://embed_link.com",
17
+ :width => "240",
18
+ :height => "100",
19
+ :video_id => "13142"
20
+ }.merge!(opts))
21
+ end
22
+
23
+ describe "#get" do
24
+ context ":article" do
25
+ before do
26
+ @result = MediaMetaHash.for(ARTICLE_URL, :article)
27
+ end
28
+
29
+ it "should return a hash" do
30
+ @result.should be_instance_of Hash
31
+ end
32
+
33
+ it "should contains these keys" do
34
+ @result.should include(:og, :twitter)
35
+ end
36
+ end
37
+
38
+ context ":video" do
39
+ before do
40
+ @result = MediaMetaHash.for(YOUTUBE_URL)
41
+ end
42
+
43
+ it "should return a hash" do
44
+ @result.should be_instance_of Hash
45
+ end
46
+
47
+ it "should contains these keys" do
48
+ @result.should include(:og, :twitter)
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "#video_info" do
54
+ context "for fox videos" do
55
+ it "returns object with correct attributes" do
56
+ info = MediaMetaHash.video_info FOX_URL
57
+ info.methods.should include(:video_id, :embed_url, :title, :image, :description, :provider, :width, :height)
58
+ end
59
+ end
60
+
61
+ context "for cnbc videos" do
62
+ it "returns object with correct attributes" do
63
+ info = MediaMetaHash.video_info CNBC_URL
64
+ info.methods.should include(:video_id, :embed_url, :title, :image, :description, :provider, :width, :height)
65
+ end
66
+ end
67
+
68
+ context "for youtube videos" do
69
+ before do
70
+ VideoInfo.stub(:get).and_return{ basic_info({:thumbnail_medium => "url"}) }
71
+ end
72
+
73
+ it "returns object with correct attributes" do
74
+ url = "http://www.youtube.com/watch?v=ZQCINxTQlN4"
75
+ info = MediaMetaHash.video_info(url)
76
+ info.methods.should include(:video_id, :embed_url, :title, :description, :provider, :width, :height, :thumbnail_medium, :og_url)
77
+ end
78
+ end
79
+
80
+ context "for other videos" do
81
+ before do
82
+ VideoInfo.stub(:get).and_return{ basic_info }
83
+ end
84
+
85
+ it "returns object with correct attritubes" do
86
+ url = "http://vimeo.com/74868773"
87
+ info = MediaMetaHash.video_info(url)
88
+ info.methods.should_not include(:og_url)
89
+ end
90
+ end
91
+
92
+ context "for unsupported videos" do
93
+ it "returns nil" do
94
+ url = "http://video.com/2552524"
95
+ info = MediaMetaHash.video_info(url)
96
+ info.should be_nil
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "#video_hash" do
102
+ it "gets video info" do
103
+ url = "http://vimeo.com/74868773"
104
+ MediaMetaHash.should_receive(:video_info).and_return { basic_info }
105
+ MediaMetaHash.video_hash url, {}
106
+ end
107
+
108
+ context "for youtube videos" do
109
+ before do
110
+ MediaMetaHash.stub(:video_info).and_return{ basic_info({:og_url => "http://og_url.com", :provider => :youtube}) }
111
+ @info_hash = MediaMetaHash.video_hash YOUTUBE_URL, {}
112
+ end
113
+
114
+ it "the :og key" do
115
+ @info_hash[:og].should include(:title, :description, :image, :type, :video)
116
+ end
117
+
118
+ it "the :twitter key" do
119
+ @info_hash[:twitter].should include(:title, :description, :image, :player, :card, :app)
120
+ end
121
+
122
+ it "the :twitter => :player should be an array" do
123
+ @info_hash[:twitter][:player].should be_instance_of Array
124
+ end
125
+
126
+ it "the :twitter => :player url (array.first) should be secure" do
127
+ @info_hash[:twitter][:player].first.should match(/^https/)
128
+ end
129
+
130
+ it "the :twitter => :player array should have hash" do
131
+ @info_hash[:twitter][:player].last.should include(:width, :height)
132
+ end
133
+
134
+ it "the :og => :video key" do
135
+ @info_hash[:og][:video].should == ["http://og_url.com", {:height => "100", :width => "240"}]
136
+ end
137
+
138
+ it "the :twitter => :app key" do
139
+ @info_hash[:twitter][:app].should include(:id, :name, :url)
140
+ end
141
+
142
+ it "the :twitter => :app => :id key" do
143
+ @info_hash[:twitter][:app][:id].should include(:iphone, :ipad, :googleplay)
144
+ end
145
+
146
+ it "the :twitter => :app => :name key" do
147
+ @info_hash[:twitter][:app][:name].should include(:iphone, :ipad, :googleplay)
148
+ end
149
+
150
+ it "the :twitter => :app => :url key" do
151
+ @info_hash[:twitter][:app][:url].should include(:iphone, :ipad, :googleplay)
152
+ end
153
+ end
154
+
155
+ context "for fox videos" do
156
+ before do
157
+ MediaMetaHash.stub(:video_info).and_return{ basic_info({:provider => "foxbusiness"}) }
158
+ @info_hash = MediaMetaHash.video_hash FOX_URL, {}
159
+ end
160
+
161
+ it "the :og => :video key" do
162
+ @info_hash[:og][:video].should == ["http://embed_link.com", {:height => "100", :width => "240"}]
163
+ end
164
+
165
+ it "the :twitter key" do
166
+ @info_hash[:twitter].should_not include(:app)
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'media_meta_hash'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_meta_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1.alpha
5
- prerelease: 8
4
+ version: 0.0.2
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Renaldo Pierre-Louis
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-09 00:00:00.000000000 Z
12
+ date: 2013-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: video_info
@@ -59,22 +59,59 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- description: ''
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-mocks
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Given the url to a video (youtube, vimeo, foxnews, foxbusiness), this
95
+ return a hash for opengraph (og) and twitter cards which can be used with meta-tags
96
+ gem to create the html tags
63
97
  email:
64
- - rpierrelouis@hedgeye.com
98
+ - pirelouisd87@gmail.com
65
99
  executables: []
66
100
  extensions: []
67
101
  extra_rdoc_files: []
68
102
  files:
69
103
  - .gitignore
70
104
  - Gemfile
105
+ - LICENSE
71
106
  - LICENSE.txt
72
107
  - README.md
73
108
  - Rakefile
74
109
  - lib/media_meta_hash.rb
75
110
  - lib/media_meta_hash/version.rb
76
111
  - media_meta_hash.gemspec
77
- homepage: ''
112
+ - spec/media_meta_hash_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://github.com/daltonrenaldo/meta_media_hash
78
115
  licenses:
79
116
  - MIT
80
117
  post_install_message:
@@ -90,13 +127,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
127
  required_rubygems_version: !ruby/object:Gem::Requirement
91
128
  none: false
92
129
  requirements:
93
- - - ! '>'
130
+ - - ! '>='
94
131
  - !ruby/object:Gem::Version
95
- version: 1.3.1
132
+ version: '0'
96
133
  requirements: []
97
134
  rubyforge_project:
98
135
  rubygems_version: 1.8.25
99
136
  signing_key:
100
137
  specification_version: 3
101
- summary: Given a url, you get a meta hash for the type of url
102
- test_files: []
138
+ summary: This gem takes video url (youtube, vimeo, foxnews, foxbusiness) and returns
139
+ a hash for og and twitter card
140
+ test_files:
141
+ - spec/media_meta_hash_spec.rb
142
+ - spec/spec_helper.rb