reddavis-embedit 0.0.1 → 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/lib/embedit/oembed.rb +61 -0
- data/lib/embedit/youtube.rb +47 -0
- metadata +4 -2
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
module Embedit
|
|
2
|
+
|
|
3
|
+
class Oembed
|
|
4
|
+
|
|
5
|
+
attr_reader :title, :url, :format, :html
|
|
6
|
+
|
|
7
|
+
def initialize(url)
|
|
8
|
+
@input_url = url
|
|
9
|
+
@sites = Providers.new.sites
|
|
10
|
+
get_info
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def html(size = {})
|
|
14
|
+
if @format == 'photo'
|
|
15
|
+
@html.insert(-2, " height=#{size[:height]} ") unless size[:height].nil?
|
|
16
|
+
@html.insert(-2, " width=#{size[:width]}") unless size[:width].nil?
|
|
17
|
+
else
|
|
18
|
+
@html.gsub!(/height="\d+"/, %{height="#{size[:height].to_s}"}) unless size[:height].nil?
|
|
19
|
+
@html.gsub!(/width="\d+"/, %{width="#{size[:width].to_s}"}) unless size[:width].nil?
|
|
20
|
+
end
|
|
21
|
+
@html
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
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)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def set_attributes(att)
|
|
33
|
+
parsed_data = JSON.parse(att)
|
|
34
|
+
@title = parsed_data['title']
|
|
35
|
+
@url = parsed_data['url'] ||= @input_url
|
|
36
|
+
@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
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
#some urls contain format in the middle of the url
|
|
51
|
+
def prepare_url(url)
|
|
52
|
+
if url.match(/format/)
|
|
53
|
+
@base_url = "#{url.gsub(/\{format\}/, 'json')}" + '?url='
|
|
54
|
+
else
|
|
55
|
+
@input_url = @input_url + '&format=json'
|
|
56
|
+
@base_url = url + '?url='
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Embedit
|
|
2
|
+
|
|
3
|
+
class YouTube
|
|
4
|
+
|
|
5
|
+
attr_reader :title, :url, :format
|
|
6
|
+
|
|
7
|
+
def initialize(url)
|
|
8
|
+
@format = 'video'
|
|
9
|
+
@url = url
|
|
10
|
+
get_info
|
|
11
|
+
end
|
|
12
|
+
|
|
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
|
+
@html = %{
|
|
21
|
+
<object width="425" height="350">
|
|
22
|
+
<param name="movie" value="http://www.youtube.com/v/#{video_id}"></param>
|
|
23
|
+
<param name="wmode" value="transparent"></param>
|
|
24
|
+
<embed src="http://www.youtube.com/v/#{video_id}"
|
|
25
|
+
type="application/x-shockwave-flash" wmode="transparent"
|
|
26
|
+
width="425" height="350">
|
|
27
|
+
</embed>
|
|
28
|
+
</object>
|
|
29
|
+
}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def get_info
|
|
35
|
+
video_id = extract_id(@url)
|
|
36
|
+
data = REXML::Document.new(open("http://gdata.youtube.com/feeds/videos/#{video_id}"))
|
|
37
|
+
@title = REXML::XPath.first(data, "//title").text
|
|
38
|
+
self.html = video_id
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def extract_id(url)
|
|
42
|
+
url.scan(/v=([\w\d]+)/)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
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.
|
|
4
|
+
version: 0.0.2
|
|
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:
|
|
12
|
+
date: 2008-08-26 08:42:40 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -27,6 +27,8 @@ files:
|
|
|
27
27
|
- lib/providers.yaml
|
|
28
28
|
- lib/embedit/media.rb
|
|
29
29
|
- lib/embedit/providers.rb
|
|
30
|
+
- lib/embedit/youtube.rb
|
|
31
|
+
- lib/embedit/oembed.rb
|
|
30
32
|
has_rdoc: false
|
|
31
33
|
homepage: http://github.com/reddavis/embedit/
|
|
32
34
|
post_install_message:
|