reddavis-embedit 0.0.9 → 0.1.0

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/README CHANGED
@@ -5,6 +5,8 @@ Embedit is an embedding tool written in Ruby.
5
5
 
6
6
  We are currently re-writing dothegreenthing.com and allow users to add media from pretty much anywhere. Handling all the different embed codes/api's validations and everything else that comes with this functionality can be/is a pain.
7
7
 
8
+ Embedit is the backbone to the web service http://embedit.me. Embedit.me also has its only ruby lib over at http://github.com/reddavis/embedit_ruby/tree/master
9
+
8
10
  Embedit is extremely simple to use. You have the url? Just plonk it in here =>
9
11
 
10
12
  media = Embedit::Media.new('here')
@@ -26,10 +28,20 @@ At the moment Embedit supports:
26
28
  - oEmbed family (http://www.oembed.com/)
27
29
  - YouTube (http://youtube.com)
28
30
  - OVI (http://share.ovi.com/)
29
- - Local files, I've had some problems with Rails (files not found), but using http://pandastream.com it works perfectly fine
31
+ - Google Video
32
+ - Sevenload Video
33
+ - .[jpg|gif|png] URL
34
+ - Twitter (twictur.es)
35
+ - Myspace Images
36
+ - FLV video stored where ever
30
37
 
31
38
  Sites To Support:
32
39
  - Facebook
33
40
  - Image shack
34
41
  - Screen shot of a website (user puts in url, if nothing matches take screen shot)
35
- - Email me any other suggestions with links to the site at reddavis [[+@+]] gmail *.* com
42
+ - Email me any other suggestions with links to the site at reddavis [[+@+]] gmail *.* com
43
+
44
+ Credits:
45
+
46
+ Red Davis (http://redwriteshere.com/) (http://github.com/reddavis/embedit/tree/master)
47
+ Michael Bumann - railslove.com
data/lib/embedit/media.rb CHANGED
@@ -2,56 +2,41 @@ module Embedit
2
2
 
3
3
  class Media
4
4
 
5
- attr_reader :title, :url, :format, :html
5
+ attr_accessor :title, :url, :format, :html, :provider
6
+
7
+
8
+ #delegate :title, :html, :format, :to => :provider
6
9
 
7
10
  def initialize(url)
8
- @valid = true #Innocent until proven guilty
9
- @oembed_providers = Providers.new.sites
10
- find_provider(url)
11
- #rescue #Horrible hack, but flickrs poor status headers == :(
12
- # @valid = false #if it breaks, its gotta be invalid, I suggest removing when debugging
11
+ @valid = true
12
+ @url = url
13
+ @provider = Provider.find(url)
14
+ # The validation does a expensive GET request. needs to be fixed. (request only the headers?!)
15
+ if !@provider || !Validate.new(@url).valid?
16
+ @valid = false
17
+ return
18
+ end
19
+
20
+ rescue #Horrible hack, but flickrs poor status headers == :(
21
+ @valid = false #if it breaks, its gotta be invalid, I suggest removing when debugging
13
22
  end
14
23
 
15
24
  def title
16
- @media_data.title
25
+ self.provider.title if self.provider
17
26
  end
18
27
 
19
- def html(size = {})
20
- @media_data.html(size)
28
+ def html(args={})
29
+ args = {} if args.nil?
30
+ self.provider.html(args) if self.provider
21
31
  end
22
32
 
23
33
  def format
24
- @media_data.format
25
- end
26
-
27
- def url
28
- @media_data.url
34
+ self.provider.format if self.provider
29
35
  end
30
36
 
31
37
  def valid?
32
38
  @valid
33
39
  end
34
40
 
35
-
36
- private
37
-
38
- #Find a provider
39
- def find_provider(url)
40
- return @valid = false unless Validate.new(url).valid?
41
-
42
- @oembed_providers.keys.each do |key| #First search oembed providers for a match
43
- if url.match(/(\.|\/)#{key}\./) #URL can be www.vimeo.com || http://vimeo.com
44
- return @media_data = Oembed.new(url, key)
45
- end
46
- end
47
- if url.match(/(\.|\/)youtube\./) #Next up is YouTube
48
- return @media_data = YouTube.new(url)
49
- elsif url.match(/share\.ovi\.com/)
50
- return @media_data = Ovi.new(url)
51
- elsif File.extname(url) != "" || nil
52
- return @media_data = Player.new(url)
53
- end
54
- @valid = false
55
- end
56
41
  end
57
42
  end
@@ -0,0 +1,32 @@
1
+ require "active_support"
2
+
3
+ module Embedit
4
+ class Provider
5
+ attr_reader :title, :url, :format, :html
6
+
7
+ # require all providers and collect the names
8
+ @@list = Dir["#{File.dirname(__FILE__)}/providers/*.rb"].collect {|file| require(file); "Embedit::#{File.basename(file,'.rb').classify}".constantize }
9
+
10
+ class << self
11
+ def all
12
+ @@list
13
+ end
14
+
15
+ def each
16
+ all.each do |provider|
17
+ yield provider
18
+ end
19
+ end
20
+
21
+ def find(url)
22
+ each do |provider|
23
+ return provider.new(url) if provider.match(url)
24
+ end
25
+ nil # if no provider found
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -0,0 +1,30 @@
1
+ module Embedit
2
+
3
+ class Flv
4
+
5
+ attr_reader :title, :url, :format
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @format = "video"
10
+ self.html = url
11
+ end
12
+
13
+ def html(options = {})
14
+ @html.gsub!(/height="\d+"/, %{height="#{options[:height].to_s}"}) unless options[:height].nil?
15
+ @html.gsub!(/width="\d+"/, %{width="#{options[:width].to_s}"}) unless options[:width].nil?
16
+ @html
17
+ end
18
+
19
+ def html=(url)
20
+ @html = %{<embed src="http://s3.amazonaws.com/panda-test/player.swf" width="400" height="300"
21
+ allowfullscreen="true" allowscriptaccess="always"
22
+ flashvars="&displayheight=300&file=#{url}&width=400&height=300" />}
23
+ end
24
+
25
+ def self.match(url)
26
+ url.match(/(.+\.flv$)/)
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,32 @@
1
+ module Embedit
2
+
3
+ class GoogleVideo
4
+
5
+ attr_reader :title, :url, :format
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @format = "video"
10
+ end
11
+
12
+ def html(options={})
13
+ options.reverse_merge!(:width=>400, :height=>326)
14
+ %{<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=#{doc_id}&hl=en&fs=true" style="width:#{options[:width]}px;height:#{options[:height]}px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>}
15
+ end
16
+
17
+ def doc_id
18
+ @url[/video\.google\.com\/videoplay\?docid=(\w+)/,1]
19
+ end
20
+
21
+ def title
22
+ return @title if @title
23
+ @page = Hpricot(open(@url))
24
+ @title = @page.at("title").inner_html if @page.at("title")
25
+ end
26
+
27
+ def self.match(url)
28
+ url.match(/video\.google\.com\/videoplay\?docid=(\w+)/)
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,23 @@
1
+ module Embedit
2
+
3
+ class Image
4
+
5
+ attr_reader :title, :url, :format
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ end
10
+
11
+ def html(options = {})
12
+ attributes = options.collect {|k,v| "#{k}=\"#{v}\" "}.join(" ")
13
+ %{
14
+ <a href="#{@url}"><img src="#{@url}" #{attributes}/></a>
15
+ }
16
+ end
17
+
18
+ def self.match(url)
19
+ url.match(/(.+\.(gif|jpg|png)$)/)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,15 @@
1
+ module Embedit
2
+
3
+ class Ipernity
4
+
5
+ attr_reader :title, :url, :format
6
+
7
+ def initialize(url)
8
+ @url = url
9
+
10
+ end
11
+ def self.match(url)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,35 @@
1
+ module Embedit
2
+
3
+ class Myspace
4
+
5
+ attr_reader :url, :title, :format
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @format = 'picture'
10
+ end
11
+
12
+ def title
13
+ @title ||= page.at("div#caption span").inner_html
14
+ end
15
+
16
+ def html(size = {})
17
+ attributes = size.collect {|k,v| "#{k}=\"#{v}\" "}.join(" ")
18
+ puts src = page.at("img#userImage").to_s.slice(/src=.+\.\w+/)
19
+ %{
20
+ <a href="#{@url}"><img src="#{src}" #{attributes}/></a>
21
+ }
22
+ end
23
+
24
+ def self.match(url)
25
+ url.match(/viewmorepics\.myspace\.com/)
26
+ end
27
+
28
+ private
29
+
30
+ def page
31
+ @page ||= Hpricot(open(@url))
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,79 @@
1
+ module Embedit
2
+
3
+ class Oembed
4
+
5
+ @@list = {
6
+ /flickr\.com\/photos\/(\S+)\/([0-9A-Za-z]+)/xi => { :name => "flickr", :api_url => "http://www.flickr.com/services/oembed" },
7
+ /pownce/ => { :name => "pownce", :api_url => "http://api.pownce.com/2.1/oembed.{format}" },
8
+ /revision3/ => { :name => "pownce", :api_url => "http://revision3.com/api/oembed/" },
9
+ /qik\.com\/video\/(\d+)/xi => { :name => "qik", :api_url => "http://qik.com/api/oembed.{format}" },
10
+ /viddler\.com(\/explore)?\/([^\/,\s]+)\/videos\/([^\/,\s]+)/xi => { :name => "viddler", :api_url => "http://lab.viddler.com/services/oembed/" },
11
+ /vimeo\.com\/(\d+)/xi => { :name => "vimeo", :api_url => "http://www.vimeo.com/api/oembed.{format}" },
12
+ /slideshare/ => {:name => "slideshare", :api_url => "http://oohembed.com/oohembed/"},
13
+ /amazon\.(com|co.uk|de|ca|jp)\/(.*)(gp\/product|o\/ASIN|obidos\/ASIN|dp)\/(.*)/ => {:name => "amazon", :api_url => "http://oohembed.com/oohembed/"}
14
+ }
15
+
16
+ attr_reader :title, :url, :format, :html
17
+
18
+ def initialize(url)
19
+ @input_url = url
20
+
21
+ get_provider_for(url)
22
+ get_info
23
+ end
24
+
25
+ def html(size = {})
26
+ if @format == 'photo' #Photos use image tags
27
+ @html.insert(-2, " height=#{size[:height]} ") unless size[:height].nil?
28
+ @html.insert(-2, " width=#{size[:width]}") unless size[:width].nil?
29
+ else
30
+ @html.gsub!(/height="\d+"/, %{height="#{size[:height].to_s}"}) unless size[:height].nil?
31
+ @html.gsub!(/width="\d+"/, %{width="#{size[:width].to_s}"}) unless size[:width].nil?
32
+ end
33
+ @html
34
+ end
35
+
36
+ def self.match(url)
37
+ @@list.keys.each do |regex|
38
+ m = url.match(regex)
39
+ return m if m
40
+ end
41
+ nil # if none matches
42
+ end
43
+
44
+ private
45
+
46
+ def get_provider_for(url)
47
+ @@list.each do |regex, provider|
48
+ @provider = provider and break if url.match(regex)
49
+ end
50
+ @provider
51
+ end
52
+
53
+ def get_info
54
+ base_url = prepare_url(@provider[:api_url]) #Prepare the base_url
55
+ url = URI.parse(base_url + @input_url)
56
+ api_data = Net::HTTP.get(url) #Get the data
57
+ set_attributes(api_data)
58
+ end
59
+
60
+ def set_attributes(att)
61
+ parsed_data = JSON.parse(att)
62
+ @title = parsed_data['title']
63
+ @url = parsed_data['url'] ||= @input_url
64
+ @format = parsed_data['type']
65
+ @html = @format == 'video' ? parsed_data['html'] : %{<img src='#{@url}' alt='#{@title}'>} #Image tags
66
+ end
67
+
68
+ #some urls contain format in the middle of the url
69
+ def prepare_url(url)
70
+ if url.match(/format/)
71
+ return "#{url.gsub(/\{format\}/, 'json')}" + '?url='
72
+ else
73
+ @input_url = @input_url + '&format=json'
74
+ return url + '?url='
75
+ end
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,49 @@
1
+ #http://share.ovi.com - They have no API so screen scrape probably best solution here
2
+ module Embedit
3
+
4
+ class Ovi
5
+
6
+ attr_reader :title, :url, :format
7
+
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def html(size = {})
13
+ @html = original_html
14
+ @html.gsub!(/height="\d+"/, %{height="#{size[:height]}"}) if size[:height]
15
+ @html.gsub!(/width="\d+"/, %{width="#{size[:width]}"}) if size[:width]
16
+ @html
17
+ end
18
+
19
+ def title
20
+ @title ||= page.at("h2.pagetitle").inner_html.strip
21
+ end
22
+
23
+ def format
24
+ @format ||= case self.original_html
25
+ when /flash\/player\.aspx\?media/ then 'video'
26
+ when /flash\/audioplayer\.aspx\?media/ then 'audio'
27
+ when /<img src/ then 'photo'
28
+ end
29
+ end
30
+
31
+ def self.match(url)
32
+ url.match(/share\.ovi\.com/)
33
+ end
34
+
35
+ def page
36
+ @page ||= Hpricot(open(@url))
37
+ end
38
+
39
+ def original_html
40
+ return @original_html if @original_html
41
+ @original_html = page.search("tr#M_sidebar_uimediaembed_uifp1 td input").first.attributes['value'] rescue nil #We first search for video or audio, if not its got to be image (hopfully)
42
+ if @original_html.nil?
43
+ @original_html = page.search("div#M_sidebar_uimediaembed_uiip td input#M_sidebar_uimediaembed_uihtml2").first.attributes['value'].gsub(/<a \S+>/, '').gsub(/<\/a>/, '') #Follow Embedit convention, images should not be surrounded a a <a href></a>
44
+ end
45
+ @original_html
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,47 @@
1
+ module Embedit
2
+
3
+ class SevenloadVideo
4
+
5
+ attr_reader :title, :url, :format, :video_id, :lang
6
+
7
+ def initialize(url)
8
+ @format = 'video'
9
+ @url = url
10
+ get_info
11
+ end
12
+
13
+ def html(options = {})
14
+ options.reverse_merge!({:width=>445,:height=>364})
15
+ %{
16
+ <object type="application/x-shockwave-flash" data="http://#{lang}.sevenload.com/pl/#{video_id}/#{options[:width]}x#{options[:height]}/swf" width="#{options[:width]}" height="#{options[:height]}">
17
+ <param name="allowFullscreen" value="true" />
18
+ <param name="allowScriptAccess" value="always" />
19
+ <param name="movie" value="http://#{lang}.sevenload.com/pl/#{video_id}/#{options[:width]}x#{options[:height]}/swf" />
20
+ </object>
21
+ }
22
+ end
23
+
24
+ def title
25
+ page.at("#itemTitle").inner_html
26
+ end
27
+
28
+ def self.match(url)
29
+ url.match(/(\w+)\.sevenload\.\S{2,}\/(videos|videolar|filmy|video)?\/(\w+)[\/]?([\S]+)/xi) || url.match(/(\w+)\.sevenload\.\S{2,}\/(shows|sendungen|yayinlar|emissions|trasmissione|audycje)?\/(\S+)\/(episodes|folgen|bolumler|puntate)?\/(\w+)[\/]?([\S]+)/xi)
30
+ end
31
+
32
+ private
33
+
34
+ def page
35
+ @page ||= Hpricot(open(@url))
36
+ end
37
+
38
+ def get_info
39
+ match = self.class.match(@url)
40
+ @lang = match[1]
41
+ # the video id is always the second last match
42
+ @video_id = match[-2]
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,36 @@
1
+ module Embedit
2
+
3
+ class Twitter
4
+
5
+ attr_reader :title, :url, :format
6
+
7
+ def initialize(url)
8
+ @format = 'photo'
9
+ @url = url
10
+ end
11
+
12
+ def html(options = {})
13
+ attributes = options.collect {|k,v| "#{k}=\"#{v}\" "}.join(" ")
14
+ %{<img src="http://twictur.es/i/#{status_id}.gif" #{attributes} />}
15
+ end
16
+
17
+ def status_id
18
+ @status_id ||= url[/twitter\.com\/(\S+)\/statuses\/(\d+)/,2]
19
+ end
20
+
21
+ def title
22
+ @title ||= page.at("title").inner_html
23
+ end
24
+
25
+ def self.match(url)
26
+ url.match(/twitter\.com\/(\S+)\/statuses\/(\d+)/)
27
+ end
28
+
29
+ private
30
+
31
+ def page
32
+ @page ||= Hpricot(open(@url))
33
+ end
34
+ end
35
+
36
+ end
@@ -1,46 +1,44 @@
1
1
  module Embedit
2
2
 
3
- class YouTube
3
+ class Youtube
4
4
 
5
5
  attr_reader :title, :url, :format
6
6
 
7
7
  def initialize(url)
8
8
  @format = 'video'
9
9
  @url = url
10
- get_info
11
10
  end
12
11
 
13
- def html(size = {})
14
- @html.gsub!(/height="\d+"/, %{height="#{size[:height].to_s}"}) unless size[:height].nil?
15
- @html.gsub!(/width="\d+"/, %{width="#{size[:width].to_s}"}) unless size[:width].nil?
16
- @html
17
- end
18
-
19
- def html=(video_id)
20
- #Add &ap=%2526fmt%3D18 to end of YouTube embed url to gain access to higher quality videos
12
+ def html(options = {})
13
+ options.reverse_merge!({:width=>425,:height=>350})
21
14
  @html = %{
22
- <object width="425" height="350">
15
+ <object width="#{options[:width]}" height="#{options[:height]}">
23
16
  <param name="movie" value="http://www.youtube.com/v/#{video_id}"></param>
24
17
  <param name="wmode" value="transparent"></param>
25
18
  <embed src="http://www.youtube.com/v/#{video_id}"
26
19
  type="application/x-shockwave-flash" wmode="transparent"
27
- width="425" height="350">
20
+ width="#{options[:width]}" height="#{options[:height]}">
28
21
  </embed>
29
22
  </object>
30
23
  }
31
24
  end
32
-
33
- private
34
25
 
35
- def get_info
36
- video_id = extract_id(@url)
26
+ def video_id
27
+ @video_id ||= url[/v=([\w\d\-]+)/,1]
28
+ end
29
+
30
+ def title
31
+ return @title if @title
37
32
  data = REXML::Document.new(open("http://gdata.youtube.com/feeds/videos/#{video_id}"))
38
33
  @title = REXML::XPath.first(data, "//title").text
39
- self.html = video_id
40
34
  end
41
-
42
- def extract_id(url)
43
- url.scan(/v=([\w\d]+)/)
35
+
36
+ def self.match(url)
37
+ url.match(/youtube\.com\/watch\?v=(\S+)/xi)
38
+ end
39
+
40
+ def reverse_merge!(other_hash)
41
+ replace(reverse_merge(other_hash))
44
42
  end
45
43
 
46
44
  end
@@ -1,44 +1,22 @@
1
- class Validate
1
+ module Embedit
2
+ class Validate
2
3
 
3
- def initialize(url)
4
- @url = url
5
- @valid = check_url == true ? true : false
6
- end
7
-
8
- def valid?
9
- @valid
10
- end
11
-
12
- private
4
+ def initialize(url)
5
+ @url = url
6
+ @valid = accessible? # && add more tests here if needed
7
+ end
13
8
 
14
- def check_url
15
- if (check_url_supported == true && check_response == true) || (File.extname(@url) != "" || nil && check_url_supported) #We first check that the url is one actually supported by Embedit
16
- return true
9
+ def valid?
10
+ @valid
17
11
  end
18
- end
12
+
13
+ private
19
14
 
20
- def check_response
21
- true if open(@url) #Header codes are annoying, just check that the page works, the check with Embed::Media will narrow down more
22
- rescue
15
+ def accessible?
16
+ true if @url.match(/(flv|jpg|png|gif|mp3)$/) || open(@url) #Header codes are annoying, just check that the page works, the check with Embed::Media will narrow down more
17
+ rescue
23
18
  false
24
- end
25
-
26
- def check_url_supported
27
- oembed_providers = Embedit::Providers.new.sites
28
- oembed_providers.keys.each do |key| #First search oembed providers for a match
29
- if @url.match(/(\.|\/)#{key}\./) #URL can be www.vimeo.com || http://vimeo.com
30
- return true
31
- end
32
19
  end
33
- # Now we go through all services not linked with oEmbed
34
- if @url.match(/(\.|\/)youtube\./) #All youtube links should end with a .com (Please correct if I'm wrong) they get redirected to jp.youtube.com or whatever
35
- return true
36
- elsif @url.match(/share\.ovi\.com/)
37
- return true
38
- elsif File.extname(@url) != "" || nil
39
- return true
40
- end
41
- return false #Return false if all else fail
42
- end
43
20
 
21
+ end
44
22
  end
data/lib/embedit.rb CHANGED
@@ -3,20 +3,21 @@ require 'json'
3
3
  require 'rexml/document'
4
4
  require 'net/http'
5
5
  require 'open-uri'
6
+ require 'hpricot'
6
7
  require 'yaml'
7
8
 
8
9
  #Files
9
- require 'embedit/oembed/providers'
10
+ require 'embedit/provider'
10
11
  require 'embedit/media'
11
- require 'embedit/oembed/oembed'
12
- require 'embedit/youtube/youtube'
13
- require 'embedit/ovi/ovi'
14
12
  require 'embedit/exceptions'
15
13
  require 'embedit/validate'
16
- require 'embedit/player/player'
14
+
15
+ module Embedit
16
+ end
17
17
 
18
18
  # Oembed
19
- #puts a = Embedit::Media.new('http://www.vimeo.com/1260077').title
19
+
20
+ #puts a = Embedit::Media.new('http://www.vimeo.com/1263763').html
20
21
 
21
22
  #puts b = Embedit::Media.new('http://www.flickr.com/photos/reddavis999/2692043113/').html #valid? #.valid #.html(:height => 200)
22
23
 
@@ -42,6 +43,8 @@ require 'embedit/player/player'
42
43
 
43
44
  # Flash Player
44
45
 
45
- #puts a = Embedit::Media.new('../test.flv').html
46
+ #puts a = Embedit::Media.new('http://twitter.com/kastner/statuses/939873832').html
47
+
48
+ # MySpace Pictures
46
49
 
47
- #puts File.exists?(File.expand_path('test.flv'))
50
+ #puts a = Embedit::Media.new('http://viewmorepics.myspace.com/index.cfm?fuseaction=viewImage&friendID=152417109&albumID=0&imageID=8797852').html(:height => 20)
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Google Video" do
4
+
5
+ it "should show true on valid url" do
6
+ Embedit::Media.new("http://video.google.com/videoplay?docid=9190313294638554619&hl=en").should be_valid
7
+ end
8
+
9
+ it "should have the title of 'Full Democratic Debate - Austin TX - Feb 21, 2008'" do
10
+ Embedit::Media.new("http://video.google.com/videoplay?docid=9190313294638554619&hl=en").title.should eql("Full Democratic Debate - Austin TX - Feb 21, 2008")
11
+ end
12
+
13
+ it "should show format as video" do
14
+ Embedit::Media.new("http://video.google.com/videoplay?docid=9190313294638554619&hl=en").format.should eql("video")
15
+ end
16
+
17
+ it "should width and height options for embeded code" do
18
+ embed = Embedit::Media.new("http://video.google.com/videoplay?docid=9190313294638554619&hl=en").html(:width=>200, :height => 100)
19
+ embed.match(/width:200px/).should_not be_nil
20
+ embed.match(/height:100px/).should_not be_nil
21
+ end
22
+
23
+ it "should return correct embeded code" do
24
+ embed = Embedit::Media.new("http://video.google.com/videoplay?docid=9190313294638554619&hl=en").html
25
+ embed.should eql(%{<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=9190313294638554619&hl=en&fs=true" style="width:400px;height:326px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>})
26
+ end
27
+
28
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Sevenload Video" do
4
+
5
+ def videos
6
+ [
7
+ { :url => "http://de.sevenload.com/videos/NWwrZzk-Flugzeug-Rennen", :title => "Flugzeug Rennen" },
8
+ { :url => "http://de.sevenload.com/sendungen/Pferdeverstehen/folgen/LDvQYeZ-Vorfuehrung-Freiheitsdressur", :title => "Vorführung Freiheitsdressur" },
9
+ { :url => "http://en.sevenload.com/videos/06Ae3BE-No-risk-no-fun-3", :title => "No risk, no fun 3" },
10
+ { :url => "http://en.sevenload.com/shows/Wine-Library-TV/episodes/1sHBywY-Sippin-Some-Tequila-With-a-Guest-Episode-529", :title => "Sippin’ Some Tequila With a Guest - Episode #529" },
11
+ { :url => "http://fr.sevenload.com/videos/Z8EMpxo-The-Bounce-Effect", :title => "The Bounce Effect" } ,
12
+ { :url => "http://fr.sevenload.com/emissions/huberbuam/episodes/f5ZpMoT-Am-Limit-Trailer-Film-Trailer-On-the-Limit", :title => 'Am Limit Trailer/Film Trailer &quot;On the Limit&quot;' }
13
+ ]
14
+ end
15
+
16
+ it "should show true on valid url" do
17
+ videos.each do |v|
18
+ Embedit::Media.new(v[:url]).should be_valid
19
+ end
20
+ end
21
+
22
+ it "should match the different urls" do
23
+ videos.each do |v|
24
+ Embedit::SevenloadVideo.match(v[:url]).should_not be_nil
25
+ end
26
+ end
27
+
28
+ it "should return the correct titles of the different urls" do
29
+ videos.each do |v|
30
+ Embedit::Media.new(v[:url]).title.should eql(v[:title])
31
+ end
32
+ end
33
+
34
+ it "should show format as video" do
35
+ videos.each do |v|
36
+ Embedit::Media.new(v[:url]).format.should eql("video")
37
+ end
38
+ end
39
+
40
+ it "should accept width and height options for embeded code" do
41
+ embed = Embedit::Media.new("http://de.sevenload.com/videos/vNAluQj-MacBookAirPl0gbariPhone").html(:width=>200, :height => 100)
42
+ embed.match(/200x100\/swf/).should_not be_nil
43
+ embed.match(/width="200"/).should_not be_nil
44
+ embed.match(/height="100"/).should_not be_nil
45
+ end
46
+
47
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Twitter" do
4
+
5
+ it "should show true on valid url" do
6
+ Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").should be_valid
7
+ end
8
+
9
+ it "should return the correct title" do
10
+ Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").title.should eql("Twitter / Michael Bumann: I love github!")
11
+ end
12
+
13
+ it "should show format as photo" do
14
+ Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").format.should eql("photo")
15
+ end
16
+
17
+ it "should accept width and height options for embeded code" do
18
+ embed = Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").html(:width=>200, :height => 100)
19
+ embed.match(/width="200"/).should_not be_nil
20
+ embed.match(/height="100"/).should_not be_nil
21
+ end
22
+
23
+ it "should return correct the embed code" do
24
+ Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").html.should eql('<img src="http://twictur.es/i/937196371.gif" />')
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reddavis-embedit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Red Davis
8
+ - Michael Bumann
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-09-28 07:22:17 -07:00
13
+ date: 2008-10-02 17:19:11 -07:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
@@ -22,24 +23,31 @@ extensions: []
22
23
  extra_rdoc_files: []
23
24
 
24
25
  files:
25
- - README
26
- - lib/embedit.rb
27
- - lib/providers.yaml
28
- - lib/embedit/media.rb
29
- - lib/embedit/oembed/providers.rb
30
- - lib/embedit/youtube/youtube.rb
31
- - lib/embedit/oembed/oembed.rb
32
26
  - lib/embedit/exceptions.rb
27
+ - lib/embedit/media.rb
28
+ - lib/embedit/provider.rb
29
+ - lib/embedit/providers/flv.rb
30
+ - lib/embedit/providers/google_video.rb
31
+ - lib/embedit/providers/image.rb
32
+ - lib/embedit/providers/ipernity.rb
33
+ - lib/embedit/providers/myspace.rb
34
+ - lib/embedit/providers/oembed.rb
35
+ - lib/embedit/providers/ovi.rb
36
+ - lib/embedit/providers/sevenload_video.rb
37
+ - lib/embedit/providers/twitter.rb
38
+ - lib/embedit/providers/youtube.rb
33
39
  - lib/embedit/validate.rb
34
- - lib/embedit/ovi/ovi.rb
35
- - lib/embedit/player/player.rb
36
- - spec/spec_helper.rb
40
+ - lib/embedit.rb
41
+ - README
42
+ - spec/google_video_spec.rb
37
43
  - spec/oembed_spec.rb
38
- - spec/youtube_spec.rb
39
44
  - spec/ovi_spec.rb
40
45
  - spec/player_spec.rb
46
+ - spec/sevenload_video_spec.rb
47
+ - spec/twitter_spec.rb
48
+ - spec/youtube_spec.rb
41
49
  has_rdoc: false
42
- homepage: http://github.com/reddavis/embedit/
50
+ homepage: http://github.com/reddavis/embedit
43
51
  post_install_message:
44
52
  rdoc_options: []
45
53
 
@@ -63,6 +71,12 @@ rubyforge_project:
63
71
  rubygems_version: 1.2.0
64
72
  signing_key:
65
73
  specification_version: 2
66
- summary: Ruby interface for embedding a range of media
67
- test_files: []
68
-
74
+ summary: Ruby interface for embedding a range of media.
75
+ test_files:
76
+ - spec/google_video_spec.rb
77
+ - spec/oembed_spec.rb
78
+ - spec/ovi_spec.rb
79
+ - spec/player_spec.rb
80
+ - spec/sevenload_video_spec.rb
81
+ - spec/twitter_spec.rb
82
+ - spec/youtube_spec.rb
@@ -1,52 +0,0 @@
1
- module Embedit
2
-
3
- class Oembed
4
-
5
- attr_reader :title, :url, :format, :html
6
-
7
- def initialize(url, provider)
8
- @input_url = url
9
- get_info(provider)
10
- end
11
-
12
- def html(size = {})
13
- if @format == 'photo' #Photos use image tags
14
- @html.insert(-2, " height=#{size[:height]} ") unless size[:height].nil?
15
- @html.insert(-2, " width=#{size[:width]}") unless size[:width].nil?
16
- else
17
- @html.gsub!(/height="\d+"/, %{height="#{size[:height].to_s}"}) unless size[:height].nil?
18
- @html.gsub!(/width="\d+"/, %{width="#{size[:width].to_s}"}) unless size[:width].nil?
19
- end
20
- @html
21
- end
22
-
23
- private
24
-
25
- def get_info(provider)
26
- oembed_services = Providers.new.sites #Load the oEmbed providers - stored in ../providers/yaml
27
- base_url = prepare_url(oembed_services[provider]) #Prepare the base_url
28
- url = URI.parse(base_url + @input_url)
29
- api_data = Net::HTTP.get(url) #Get the data
30
- set_attributes(api_data)
31
- end
32
-
33
- def set_attributes(att)
34
- parsed_data = JSON.parse(att)
35
- @title = parsed_data['title']
36
- @url = parsed_data['url'] ||= @input_url
37
- @format = parsed_data['type']
38
- @html = @format == 'video' ? parsed_data['html'] : %{<img src='#{@url}' alt='#{@title}'>} #Image tags
39
- end
40
-
41
- #some urls contain format in the middle of the url
42
- def prepare_url(url)
43
- if url.match(/format/)
44
- return "#{url.gsub(/\{format\}/, 'json')}" + '?url='
45
- else
46
- @input_url = @input_url + '&format=json'
47
- return url + '?url='
48
- end
49
- end
50
- end
51
-
52
- end
@@ -1,21 +0,0 @@
1
- module Embedit
2
- class Providers
3
-
4
- attr_accessor :sites
5
-
6
- def initialize
7
- @sites = {}
8
- add_default_providers
9
- end
10
-
11
- private
12
- def add_default_providers
13
- load_providers
14
- end
15
-
16
- def load_providers
17
- providers = YAML.load_file("#{File.dirname(__FILE__)}/../../providers.yaml")
18
- providers.each {|d| @sites.merge!(d[0] => d[1])}
19
- end
20
- end
21
- end
@@ -1,42 +0,0 @@
1
- #http://share.ovi.com - They have no API so screen scrape probably best solution here
2
-
3
- module Embedit
4
-
5
- class Ovi
6
- require 'hpricot'
7
-
8
- attr_reader :title, :url, :format
9
-
10
- def initialize(url)
11
- page = Hpricot(open(url))
12
- @url= url
13
- work_out_html(page)
14
- work_out_format(@html)
15
- @title = page.search("h2.pagetitle").inner_html.strip
16
- end
17
-
18
- def html(size = {})
19
- @html.gsub!(/height="\d+"/, %{height="#{size[:height]}"}) if size[:height]
20
- @html.gsub!(/width="\d+"/, %{width="#{size[:width]}"}) if size[:width]
21
- @html
22
- end
23
-
24
- private
25
-
26
- def work_out_html(page)
27
- @html = page.search("tr#M_sidebar_uimediaembed_uifp1 td input").first.attributes['value'] rescue nil #We first search for video or audio, if not its got to be image (hopfully)
28
- if @html.nil?
29
- @html = page.search("div#M_sidebar_uimediaembed_uiip td input#M_sidebar_uimediaembed_uihtml2").first.attributes['value'].gsub(/<a \S+>/, '').gsub(/<\/a>/, '') #Follow Embedit convention, images should not be surrounded a a <a href></a>
30
- end
31
- end
32
-
33
- def work_out_format(html)
34
- case html
35
- when /flash\/player\.aspx\?media/ then @format = 'video'
36
- when /flash\/audioplayer\.aspx\?media/ then @format = 'audio'
37
- when /<img src/ then @format = 'photo'
38
- end
39
- end
40
-
41
- end
42
- end
@@ -1,27 +0,0 @@
1
- # TODO Having problems with using in rails (files not being found), need to look into. But works perfectly find with www.pandastream.com
2
- module Embedit
3
-
4
- class Player
5
-
6
- attr_reader :title, :url, :format
7
-
8
- def initialize(url)
9
- @url = url
10
- end
11
-
12
- def html(size = {})
13
- self.html = @url # Reset measurements, incase if hmtl is called twice on the same object
14
- @html.gsub!(/400/, size[:width].to_s) unless size[:width].nil?
15
- @html.gsub!(/300/, size[:height].to_s) unless size[:height].nil?
16
- @html
17
- end
18
-
19
- def html=(url)
20
- @html = %(<embed src="http://s3.amazonaws.com/panda-test/player.swf" width="400" height="300"
21
- allowfullscreen="true" allowscriptaccess="always"
22
- flashvars="&displayheight=300&file=#{url}&width=400&height=300" />)
23
- end
24
-
25
- end
26
-
27
- end
data/lib/providers.yaml DELETED
@@ -1,13 +0,0 @@
1
- flickr: http://www.flickr.com/services/oembed
2
-
3
- vimeo: http://www.vimeo.com/api/oembed.{format}
4
-
5
- viddler: http://lab.viddler.com/services/oembed/
6
-
7
- qik: http://qik.com/api/oembed.{format}
8
-
9
- pownce: http://api.pownce.com/2.1/oembed.{format}
10
-
11
- revision3: http://revision3.com/api/oembed/
12
-
13
- # hulu: http://www.hulu.com/api/oembed.{format} - No UK service
data/spec/spec_helper.rb DELETED
@@ -1,6 +0,0 @@
1
- require 'rubygems'
2
- require 'spec'
3
-
4
- $:.unshift(File.dirname(__FILE__) + '/../lib')
5
-
6
- require 'embedit'