Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'feature/metadata' into develop

* feature/metadata:
  Added link around image
  add alt-attribute support
  Textfields enabled, not disable. Elsewise selecting is not possible
  Absolute URL support
  Refactor to DRY and more consistent code.
  Title hackishly implemented.
  • Loading branch information...
commit 7881b409dfc7de2d7cfd0bdfbcb050da1d524473 2 parents 94849da + fd37069
Bèr Kessels authored
View
1  Gemfile
@@ -3,6 +3,7 @@ source "https://rubygems.org"
gem 'sinatra', '~> 1.4'
gem 'rmagick', '~> 2.13'
+gem 'video_info', '~> 1.1'
group :test do
gem 'rspec', '~> 2.13'
View
4 Gemfile.lock
@@ -2,6 +2,7 @@ GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.1)
+ multi_json (1.7.2)
rack (1.5.2)
rack-protection (1.5.0)
rack
@@ -23,6 +24,8 @@ GEM
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (1.3.6)
+ video_info (1.1.1)
+ multi_json
PLATFORMS
ruby
@@ -33,3 +36,4 @@ DEPENDENCIES
rspec (~> 2.13)
shotgun
sinatra (~> 1.4)
+ video_info (~> 1.1)
View
39 lib/youtube.rb
@@ -1,28 +1,41 @@
require 'RMagick'
+require 'video_info'
require 'net/http'
class YouTube
- def initialize id
+ def initialize id, opts = {}
@id = id
+ @meta = nil
+
+ @base_url = opts[:base_url] || ''
end
- def img_tag overlay = false
- %Q{<img src="#{thumb(overlay)}" alt=""/>}
+ def title
+ parse
+ @meta.title
end
- private
- def thumb overlay
+ def href
+ "http://www.youtube.com/watch?v=#{@id}"
+ end
+
+ def tags
parse
- thumbname({:overlay => overlay, :omit_public => true})
+ get_thumbs.map {|t| %Q{<a href="#{href}"><img src="#{t}" alt="#{title}"/></a>} }
end
+ private
def parse
- img = Net::HTTP.get("i.ytimg.com", "/vi/#{@id}/0.jpg")
+ @meta ||= VideoInfo.get(href)
+ end
+
+ def get_thumbs
+ img = Net::HTTP.get(URI(@meta.thumbnail_large))
tmpfile = Tempfile.new(["tubemp", ".jpg"])
begin
tmpfile.binmode
- tmpfile.write img
+ tmpfile.write(img)
tmpfile.close
images = Magick::ImageList.new(tmpfile.path, File.join("assets", "overlay.png"))
@@ -35,6 +48,9 @@ def parse
images = nil
tmpfile.unlink
end
+
+ [ thumbname({:overlay => false, :absolute => true}),
+ thumbname({:overlay => true, :absolute => true}) ]
end
# Creates a path to the thumbnail
@@ -47,7 +63,12 @@ def thumbname opts = {}
filename = opts[:overlay] ? "#{@id}_overlay": @id
parts = ["thumbs", "#{filename}.png"]
- parts.unshift("public") unless opts[:omit_public]
+
+ if opts[:absolute]
+ parts.unshift(@base_url)
+ else
+ parts.unshift("public")
+ end
File.join(parts)
end
end
View
1  spec/spec_helper.rb
@@ -1,5 +1,4 @@
require 'rack/test'
-
require File.expand_path File.join('../../tubemp.rb'), __FILE__
module RSpecMixin
View
56 spec/youtube_spec.rb
@@ -1,46 +1,64 @@
require File.join(File.dirname(__FILE__), "..", "lib", "youtube")
IMAGE_RE = /\<img.*src="(.*)".*alt="(.*).*\/\>/
+LINK_RE = /\<a.*href="(.*)".*\>/
+
describe YouTube do
before do
+ info = mock("video");
+ info.stub(:title).and_return("Tony Tribe , Red Red Wine")
+ info.stub(:thumbnail_large).and_return("http://i.ytimg.com/vi/D80QdsFWdcQ/hqdefault.jpg")
+ VideoInfo.stub(:get).and_return info
+
@id = "D80QdsFWdcQ"
- @yt = YouTube.new @id
+ @yt = YouTube.new @id, {:base_url => "http://example.com"}
@filename = File.join("public", "thumbs", "#{@id}.png")
end
- describe '#img_tag' do
+ describe '#tags' do
context "valid ID" do
- it 'should render a valid image tag' do
- @yt.stub(:parse)
- @yt.img_tag.should =~ IMAGE_RE
+ it 'should render a list of valid image tags' do
+ @yt.tags.each {|t| t.should match IMAGE_RE }
end
it 'should link to a thumbnail in PNG format' do
- @yt.stub(:parse)
- img = @yt.img_tag.match IMAGE_RE
+ img = @yt.tags[0].match IMAGE_RE
src = img[1]
src.should =~ /#{@id}\.png/
end
it 'should link to the overlayed thumbnail with overlay=true' do
- @yt.stub(:parse)
- img = @yt.img_tag(true).match IMAGE_RE
+ img = @yt.tags[1].match IMAGE_RE
src = img[1]
src.should =~ /#{@id}_overlay\.png/
end
+ it 'should link to an absolute URL' do
+ img = @yt.tags[0].match IMAGE_RE
+ src = img[1]
+ src.should match /^http:\/\/.*$/
+ end
+
+ it 'should have the title as alt attribute' do
+ @yt.tags[0].match(IMAGE_RE)[2].should match "Tony Tribe , Red Red Wine"
+ end
+
+ it 'should have a link pointing to the youtube video' do
+ @yt.tags[0].match(LINK_RE)[1].should match /http:\/\/www\.youtube\.com/
+ end
+
context 'local image does not exist' do
before do
File.delete @filename if File.exists? @filename
end
it 'should create its thumb in "public/thumbs" dir' do
- @yt.img_tag
+ @yt.tags
File.exists?(@filename).should be_true
end
it 'should get the large thumbnail' do
- @yt.img_tag
+ @yt.tags
img = Magick::ImageList.new(@filename)
img.rows.should >= 300
img.columns.should >= 400
@@ -54,17 +72,29 @@
end
it 'should overwrite when a newer image is found online' do
- @yt.img_tag
+ @yt.tags
@existing.should_not eq File.stat(@filename)
end
end
it 'should overlay a play-icon' do
- @yt.img_tag
+ @yt.tags
filename = File.join("public", "thumbs", "#{@id}_over.png")
File.exists?(filename)
end
end # context "valid ID"
+ end
+ describe "#title" do
+ it "should render a title" do
+ @yt.title.should eq "Tony Tribe , Red Red Wine"
+ end
end
+
+ describe "#href" do
+ it 'should render a link' do
+ @yt.href.should eq "http://www.youtube.com/watch?v=#{@id}"
+ end
+ end
+
end
View
13 tubemp.rb
@@ -4,12 +4,9 @@
include ERB::Util
get '/:id' do
- yt = YouTube.new params[:id]
+ yt = YouTube.new params[:id], {:base_url => request.base_url }
- image_tags = [yt.img_tag, yt.img_tag(true)]
- title = "title here"
-
- erb :index, :locals => {:image_tags => image_tags, :title => title}
+ erb :index, :locals => {:tags => yt.tags, :title => yt.title}
end
__END__
@@ -74,9 +71,9 @@
</html>
@@ index
-<% image_tags.each do |image_tag| %>
+<% tags.each do |tag| %>
<div class="large-6 columns">
- <%= image_tag %><br />
- <input type="text" disabled="disabled" value="<%= html_escape image_tag %>" />
+ <%= tag %><br />
+ <input type="text" value="<%= html_escape tag %>" />
</div>
<% end %>
Please sign in to comment.
Something went wrong with that request. Please try again.