netshade-oembed_links 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README +19 -1
- data/lib/oembed_links.rb +46 -8
- data/oembed_links.gemspec +1 -2
- data/spec/oembed_links_spec.rb +43 -0
- metadata +1 -10
- data/lib/oembed_links/formatters/xml.rb +0 -36
data/README
CHANGED
@@ -4,6 +4,11 @@ inside the text. A sample configuration file for configuring the
|
|
4
4
|
library has been included (oembed_links_example.yml), though you
|
5
5
|
may also configure the library programmatically (see rdocs).
|
6
6
|
|
7
|
+
You must have the JSON gem installed to use oembed_links.
|
8
|
+
If you have the libxml-ruby gem installed, oembed_links will use that;
|
9
|
+
it will fall back to hpricot if that is installed, and finally REXML
|
10
|
+
if you have nothing else.
|
11
|
+
|
7
12
|
|
8
13
|
|
9
14
|
To get started quickly (in irb):
|
@@ -53,7 +58,20 @@ To get started quickly in Rails:
|
|
53
58
|
|
54
59
|
Copy the included oembed_links_example.yml file to RAILS_ROOT/config/oembed_links.yml,
|
55
60
|
add a dependency to the gem in your environment.rb ( config.gem "oembed_links" )
|
56
|
-
and start your server. That's it.
|
61
|
+
and start your server. That's it. If you'd like to transform the oembedded content via
|
62
|
+
templates, you can do so using the following syntax:
|
63
|
+
|
64
|
+
OEmbed.transform(text_to_transform) do |res, url|
|
65
|
+
res.video?(:template => "oembed/video")
|
66
|
+
res.from?(:a_provider, :template => "a_provider/oembed")
|
67
|
+
res.matches?(/some_regex/, :template => "shared/oembed_link")
|
68
|
+
res.any?(:template => "shared/oembed_link")
|
69
|
+
end
|
70
|
+
|
71
|
+
This presumes you have a directory in your Rails views directory called "oembed", and a file
|
72
|
+
of the form "video.html.erb" or "video.rhtml". Haml templates are also supported (Erubis too). If you are
|
73
|
+
not using Rails, you may still use the template functionality, but you must specify the full path
|
74
|
+
to the template. See the RDocs for OEmbed::TemplateResolver for more information.
|
57
75
|
|
58
76
|
|
59
77
|
|
data/lib/oembed_links.rb
CHANGED
@@ -149,15 +149,22 @@ class OEmbed
|
|
149
149
|
end
|
150
150
|
|
151
151
|
# Clear all registration information; really only valuable in testing
|
152
|
-
def self.clear_registrations
|
152
|
+
def self.clear_registrations()
|
153
153
|
@schemes = []
|
154
154
|
@urls = { }
|
155
155
|
@formats = { }
|
156
156
|
@formatters = { }
|
157
157
|
@fetchers = { }
|
158
|
-
|
158
|
+
end
|
159
|
+
|
160
|
+
# Load the default JSON and XML formatters, autodetecting
|
161
|
+
# formatters when possible; load the default fetcher as well
|
162
|
+
def self.load_default_libs(*ignore_formats)
|
163
|
+
self.autodetect_xml_formatters(*ignore_formats)
|
164
|
+
require 'oembed_links/formatters/json'
|
159
165
|
self.register_formatter(OEmbed::Formatters::JSON)
|
160
|
-
|
166
|
+
require 'oembed_links/fetchers/net_http'
|
167
|
+
self.register_fetcher(OEmbed::Fetchers::NetHTTP)
|
161
168
|
end
|
162
169
|
|
163
170
|
# Register a new formatter. klass is the class object of the desired formatter.
|
@@ -286,6 +293,38 @@ class OEmbed
|
|
286
293
|
return ret
|
287
294
|
end
|
288
295
|
|
296
|
+
# Determine the XML formatter that can be loaded for
|
297
|
+
# this system based on what libraries are present
|
298
|
+
def self.autodetect_xml_formatters(*ignore)
|
299
|
+
loaded_lib = false
|
300
|
+
unless ignore.include? "libxml"
|
301
|
+
begin
|
302
|
+
require 'libxml'
|
303
|
+
require 'oembed_links/formatters/lib_xml'
|
304
|
+
self.register_formatter(OEmbed::Formatters::LibXML)
|
305
|
+
loaded_lib = true
|
306
|
+
rescue LoadError
|
307
|
+
puts "Error loading LibXML XML formatter"
|
308
|
+
end
|
309
|
+
end
|
310
|
+
unless loaded_lib || ignore.include?("hpricot")
|
311
|
+
begin
|
312
|
+
require 'hpricot'
|
313
|
+
require 'oembed_links/formatters/hpricot_xml'
|
314
|
+
self.register_formatter(OEmbed::Formatters::HpricotXML)
|
315
|
+
loaded_lib = true
|
316
|
+
rescue LoadError
|
317
|
+
puts "Error loading Hpricot XML formatter"
|
318
|
+
end
|
319
|
+
end
|
320
|
+
unless loaded_lib || ignore.include?("rexml")
|
321
|
+
require 'oembed_links/formatters/ruby_xml'
|
322
|
+
self.register_formatter(OEmbed::Formatters::RubyXML)
|
323
|
+
loaded_lib = true
|
324
|
+
end
|
325
|
+
raise StandardError.new("No XML formatter could be autodetected") unless loaded_lib
|
326
|
+
end
|
327
|
+
|
289
328
|
private
|
290
329
|
|
291
330
|
# stupid simple copy of URI.extract to allow for looser URI detection
|
@@ -316,9 +355,8 @@ class OEmbed
|
|
316
355
|
end
|
317
356
|
end
|
318
357
|
end
|
319
|
-
|
320
|
-
|
321
358
|
end
|
322
|
-
|
323
|
-
|
324
|
-
|
359
|
+
|
360
|
+
|
361
|
+
OEmbed.load_default_libs
|
362
|
+
|
data/oembed_links.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "oembed_links"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.6"
|
4
4
|
s.author = "Indianapolis Star"
|
5
5
|
s.email = "bugs at indystar dot com"
|
6
6
|
s.homepage = "http://www.indystar.com"
|
@@ -11,5 +11,4 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.has_rdoc = true
|
12
12
|
s.extra_rdoc_files = ['README']
|
13
13
|
s.add_dependency(%q<json>)
|
14
|
-
s.add_dependency(%q<libxml-ruby>)
|
15
14
|
end
|
data/spec/oembed_links_spec.rb
CHANGED
@@ -5,6 +5,7 @@ describe OEmbed, "registration tasks" do
|
|
5
5
|
|
6
6
|
before(:each) do
|
7
7
|
OEmbed.clear_registrations
|
8
|
+
OEmbed.load_default_libs
|
8
9
|
clear_urls
|
9
10
|
end
|
10
11
|
|
@@ -62,12 +63,53 @@ describe OEmbed, "registration tasks" do
|
|
62
63
|
OEmbed.transform("http://fake/bar/baz").should == "http://fakesville"
|
63
64
|
end
|
64
65
|
|
66
|
+
it "should allow selective loading of formatters through the load_default_libs function" do
|
67
|
+
OEmbed.clear_registrations
|
68
|
+
OEmbed.load_default_libs
|
69
|
+
OEmbed.instance_variable_get("@formatters")["xml"].class.should == OEmbed::Formatters::LibXML
|
70
|
+
OEmbed.clear_registrations
|
71
|
+
OEmbed.load_default_libs("libxml")
|
72
|
+
OEmbed.instance_variable_get("@formatters")["xml"].class.should == OEmbed::Formatters::HpricotXML
|
73
|
+
OEmbed.clear_registrations
|
74
|
+
OEmbed.load_default_libs("libxml", "hpricot")
|
75
|
+
OEmbed.instance_variable_get("@formatters")["xml"].class.should == OEmbed::Formatters::RubyXML
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should support the hpricot formatter" do
|
79
|
+
OEmbed.clear_registrations
|
80
|
+
OEmbed.load_default_libs("libxml")
|
81
|
+
url_provides(<<-EOXML)
|
82
|
+
<?xml version="1.0"?>
|
83
|
+
<oembed>
|
84
|
+
<html>bar</html>
|
85
|
+
</oembed>
|
86
|
+
EOXML
|
87
|
+
OEmbed.register_provider(:test, "http://test4/oembed.{format}", "xml", "http://test.*/*")
|
88
|
+
OEmbed.transform("http://test.com/bar/baz").should == "bar"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should support the rexml formatter" do
|
92
|
+
OEmbed.clear_registrations
|
93
|
+
OEmbed.load_default_libs("libxml", "hpricot")
|
94
|
+
url_provides(<<-EOXML)
|
95
|
+
<?xml version="1.0"?>
|
96
|
+
<oembed>
|
97
|
+
<html>barxml</html>
|
98
|
+
</oembed>
|
99
|
+
EOXML
|
100
|
+
OEmbed.register_provider(:test, "http://test4/oembed.{format}", "xml", "http://test.*/*")
|
101
|
+
OEmbed.transform("http://test.com/bar/baz").should == "barxml"
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
|
65
106
|
end
|
66
107
|
|
67
108
|
describe OEmbed, "transforming functions" do
|
68
109
|
include SpecHelper
|
69
110
|
before(:each) do
|
70
111
|
OEmbed.clear_registrations
|
112
|
+
OEmbed.load_default_libs
|
71
113
|
clear_urls
|
72
114
|
url_provides({
|
73
115
|
"html" => "foo",
|
@@ -181,6 +223,7 @@ describe OEmbed, "Rails template resolving functions" do
|
|
181
223
|
include SpecHelper
|
182
224
|
before(:each) do
|
183
225
|
OEmbed.clear_registrations
|
226
|
+
OEmbed.load_default_libs
|
184
227
|
clear_urls
|
185
228
|
url_provides({
|
186
229
|
"html" => "foo",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: netshade-oembed_links
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Indianapolis Star
|
@@ -21,15 +21,6 @@ dependencies:
|
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: "0"
|
23
23
|
version:
|
24
|
-
- !ruby/object:Gem::Dependency
|
25
|
-
name: libxml-ruby
|
26
|
-
version_requirement:
|
27
|
-
version_requirements: !ruby/object:Gem::Requirement
|
28
|
-
requirements:
|
29
|
-
- - ">="
|
30
|
-
- !ruby/object:Gem::Version
|
31
|
-
version: "0"
|
32
|
-
version:
|
33
24
|
description:
|
34
25
|
email: bugs at indystar dot com
|
35
26
|
executables: []
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'libxml'
|
2
|
-
|
3
|
-
class OEmbed
|
4
|
-
module Formatters
|
5
|
-
class LibXML
|
6
|
-
|
7
|
-
def name
|
8
|
-
"xml"
|
9
|
-
end
|
10
|
-
|
11
|
-
# This is an extremely naive XML doc to hash
|
12
|
-
# formatter. Cases like arrays represented in
|
13
|
-
# XML will not work; only strings, ints and
|
14
|
-
# floats will be converted.
|
15
|
-
def format(txt)
|
16
|
-
parser = ::LibXML::XML::Parser.string(txt)
|
17
|
-
doc = parser.parse
|
18
|
-
h = { }
|
19
|
-
doc.root.children.each do |node|
|
20
|
-
unless node.name.strip.empty?
|
21
|
-
c = node.content
|
22
|
-
if c =~ /^[0-9]+$/
|
23
|
-
c = c.to_i
|
24
|
-
elsif c=~ /^[0-9]+\.[0-9]+$/
|
25
|
-
c = c.to_f
|
26
|
-
end
|
27
|
-
h[node.name.strip] = c
|
28
|
-
end
|
29
|
-
end
|
30
|
-
return h
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
OEmbed.register_formatter(OEmbed::Formatters::LibXML)
|