reddavis-embedit 0.0.3 → 0.0.4

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.
@@ -1,3 +1,6 @@
1
1
  module Embedit
2
2
 
3
+ class BadUrl < Exception
4
+ end
5
+
3
6
  end
data/lib/embedit/media.rb CHANGED
@@ -2,11 +2,14 @@ module Embedit
2
2
 
3
3
  class Media
4
4
 
5
- attr_accessor :title, :url, :format, :html
5
+ attr_reader :title, :url, :format, :html
6
6
 
7
7
  def initialize(url)
8
+ @valid = true #Innocent until proven guilty
8
9
  @oembed_providers = Providers.new.sites
9
10
  find_provider(url)
11
+ rescue #Horrible hack, but flickrs poor status headers == :( if it breaks, its gotta be invalid
12
+ @valid = false
10
13
  end
11
14
 
12
15
  def title
@@ -24,19 +27,25 @@ module Embedit
24
27
  def url
25
28
  @media_data.url
26
29
  end
27
-
30
+
31
+ def valid?
32
+ @valid
33
+ end
34
+
35
+
28
36
  private
29
37
 
30
38
  #Find a provider
31
39
  def find_provider(url)
32
- @oembed_providers.keys.each do |key| #First search oembed providers for a match
33
- if url.match(/#{key}/)
34
- return @media_data = Oembed.new(url)
40
+ @oembed_providers.keys.each do |key| #First search oembed providers for a match
41
+ if url.match(/(\.|\/)#{key}\./) && Validate.new(url).valid? #URL can be www.vimeo.com || http://vimeo.com
42
+ return @media_data = Oembed.new(url, key)
35
43
  end
36
44
  end
37
- if url.match(/youtube/) #Next up is YouTube
45
+ if url.match(/(\.|\/)youtube\./) && Validate.new(url).valid? #Next up is YouTube
38
46
  return @media_data = YouTube.new(url)
39
47
  end
48
+ @valid = false
40
49
  end
41
50
 
42
51
  end
@@ -4,14 +4,13 @@ module Embedit
4
4
 
5
5
  attr_reader :title, :url, :format, :html
6
6
 
7
- def initialize(url)
7
+ def initialize(url, provider)
8
8
  @input_url = url
9
- @sites = Providers.new.sites
10
- get_info
9
+ get_info(provider)
11
10
  end
12
11
 
13
12
  def html(size = {})
14
- if @format == 'photo'
13
+ if @format == 'photo' #Photos use image tags
15
14
  @html.insert(-2, " height=#{size[:height]} ") unless size[:height].nil?
16
15
  @html.insert(-2, " width=#{size[:width]}") unless size[:width].nil?
17
16
  else
@@ -22,38 +21,30 @@ module Embedit
22
21
  end
23
22
 
24
23
  private
25
- def get_info
26
- find_provider
27
- url = URI.parse(@base_url + @input_url)
28
- http_get = Net::HTTP.get(url)
29
- set_attributes(http_get)
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)
30
31
  end
31
-
32
+
32
33
  def set_attributes(att)
33
- parsed_data = JSON.parse(att)
34
+ parsed_data = JSON.parse(att)
34
35
  @title = parsed_data['title']
35
36
  @url = parsed_data['url'] ||= @input_url
36
37
  @format = parsed_data['type']
37
- @html = @format == 'video' ? parsed_data['html'] : %{<img src='#{@url}' alt='#{@title}'>}
38
- end
39
-
40
- #find Oembed provider - set in ../providers.yaml
41
- def find_provider
42
- @sites.keys.each do |key|
43
- if @input_url.match(/#{key}/)
44
- @base_url = prepare_url(@sites[key])
45
- break
46
- end
47
- end
38
+ @html = @format == 'video' ? parsed_data['html'] : %{<img src='#{@url}' alt='#{@title}'>} #Image tags
48
39
  end
49
-
40
+
50
41
  #some urls contain format in the middle of the url
51
42
  def prepare_url(url)
52
43
  if url.match(/format/)
53
- @base_url = "#{url.gsub(/\{format\}/, 'json')}" + '?url='
44
+ return "#{url.gsub(/\{format\}/, 'json')}" + '?url='
54
45
  else
55
46
  @input_url = @input_url + '&format=json'
56
- @base_url = url + '?url='
47
+ return url + '?url='
57
48
  end
58
49
  end
59
50
  end
@@ -0,0 +1,40 @@
1
+ class Validate
2
+
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
13
+
14
+ def check_url
15
+ if check_url_supported == true && check_response == true #We first check that the url is one actually supported by Embedit
16
+ return true
17
+ end
18
+ end
19
+
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
23
+ 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
+ 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
+ end
37
+ return false #Return false if all else fail
38
+ end
39
+
40
+ end
data/lib/embedit.rb CHANGED
@@ -10,13 +10,15 @@ require 'embedit/providers'
10
10
  require 'embedit/media'
11
11
  require 'embedit/oembed'
12
12
  require 'embedit/youtube'
13
+ require 'embedit/exceptions'
14
+ require 'embedit/validate'
13
15
 
14
16
 
15
- #puts a = Embedit::Media.new('http://www.vimeo.com/1263763').format
17
+ #puts a = Embedit::Media.new('http://www.vimeo.com/1263763').valid?
16
18
 
17
- #puts b = Embedit::Media.new('http://www.flickr.com/photos/davidgutierrez/2135724493/').html(:height => 200)
19
+ #puts b = Embedit::Media.new('http://www.flickr.com/photos/kentfield/2735062540/').valid? #.valid #.html(:height => 200)
18
20
 
19
- #puts c = Embedit::Media.new('http://www.viddler.com/explore/winelibrarytv/videos/142/').html(:height => 200, :width => 500)
21
+ #puts c = Embedit::Media.new('http://www.viddler.com/explore/winelibrarytv/videos/14212300/').html(:height => 200, :width => 500)
20
22
 
21
23
  #puts d = Embedit::Media.new('http://qik.com/video/141977').html(:height => 50)
22
24
 
@@ -28,4 +30,5 @@ require 'embedit/youtube'
28
30
 
29
31
  #puts g = Embedit::Media.new("http://www.youtube.com/watch?v=j3TOT1lnVTA")
30
32
 
31
- #puts Embedit::Media.new("http://www.youtube.com/watch?v=j3TOT1lnVTA")
33
+ #puts Validate.new('http://www.flickr.com/photos/kentfield/2735062540/').valid?
34
+
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "Vimeo oEmbed tests" do
4
+
5
+ it "should properly validate a valid Vimeo url" do
6
+ media = Embedit::Media.new('http://www.vimeo.com/1263763').valid?
7
+ media.should == true
8
+ end
9
+
10
+ it "should validate an invalid (with only numbers) Vimeo url" do
11
+ media = Embedit::Media.new('http://www.vimeo.com/126007722').valid?
12
+ media.should == false
13
+ end
14
+
15
+ it "should validate an invalid (with some letters) Vimeo url" do
16
+ media = Embedit::Media.new('http://www.vimeo.com/126badurl').valid?
17
+ media.should == false
18
+ end
19
+
20
+ end
21
+
22
+ describe "Flickr oEmbed tests" do
23
+
24
+ it "should properly validate a valid Flickr url" do
25
+ media = Embedit::Media.new('http://www.flickr.com/photos/asianmack/2781811902/in/set-72157606856535809/').valid?
26
+ media.should == true
27
+ end
28
+
29
+ it "should properly validate an invalid (with only numbers) Flickr url" do
30
+ media = Embedit::Media.new('http://www.flickr.com/photos/banuelos_ismael/280428728023/').valid?
31
+ media.should == false
32
+ end
33
+
34
+ it "should properly validate an invalid (with some letters) Flickr url" do
35
+ media = Embedit::Media.new('http://www.flickr.com/photos/banuelos_ismael/2804287asd23/').valid?
36
+ media.should == false
37
+ end
38
+
39
+ it "should properly validate an invalid (with some letters) Flickr url" do
40
+ media = Embedit::Media.new('http://www.flickr.com/photos/banuelos_ismael/2804287asd23/').valid?
41
+ media.should == false
42
+ end
43
+
44
+ it "should properly validate an invalid Flickr url photos/url-here" do
45
+ media = Embedit::Media.new('http://www.flickr.com/photos/as').valid?
46
+ media.should == false
47
+ end
48
+
49
+ end
50
+
51
+ describe "Viddler oEmbed tests" do
52
+ it "should properly validate a valid Viddler url" do
53
+ media = Embedit::Media.new('http://www.viddler.com/explore/winelibrarytv/videos/635/').valid?
54
+ media.should == true
55
+ end
56
+
57
+ it "should properly validate an invalid (with only numbers) Viddler url" do
58
+ media = Embedit::Media.new('http://www.viddler.com/explore/winelibrarytv/videos/635222/').valid?
59
+ media.should == false
60
+ end
61
+
62
+ end
63
+
64
+
65
+
66
+
67
+
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'embedit'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reddavis-embedit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Red Davis
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-26 08:59:49 -07:00
12
+ date: 2008-08-27 14:41:48 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,6 +30,9 @@ files:
30
30
  - lib/embedit/youtube.rb
31
31
  - lib/embedit/oembed.rb
32
32
  - lib/embedit/exceptions.rb
33
+ - lib/embedit/validate.rb
34
+ - spec/spec_helper.rb
35
+ - spec/oembed_spec.rb
33
36
  has_rdoc: false
34
37
  homepage: http://github.com/reddavis/embedit/
35
38
  post_install_message:
@@ -55,6 +58,6 @@ rubyforge_project:
55
58
  rubygems_version: 1.2.0
56
59
  signing_key:
57
60
  specification_version: 2
58
- summary: Ruby interface for embedding anymedia
61
+ summary: Ruby interface for embedding any media
59
62
  test_files: []
60
63