netshade-oembed_links 0.0.4 → 0.0.5
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/oembed_links.rb +19 -2
- data/lib/oembed_links/response.rb +13 -9
- data/oembed_links.gemspec +1 -1
- data/spec/oembed_links_spec.rb +118 -0
- metadata +1 -1
data/lib/oembed_links.rb
CHANGED
@@ -48,6 +48,10 @@ require 'oembed_links/response'
|
|
48
48
|
# r.from?(:youtube) { |vid| vid["html"] }
|
49
49
|
# end
|
50
50
|
#
|
51
|
+
# OEmbed.transform("Some more text from http://en.wikipedia.com/wiki/Dinosaurs!") do |r, url|
|
52
|
+
# r.from?(:wikipedia, :template => "links/wiki_links")
|
53
|
+
# end
|
54
|
+
#
|
51
55
|
# See the OEmbed.transform function for more details.
|
52
56
|
#
|
53
57
|
#
|
@@ -225,9 +229,9 @@ class OEmbed
|
|
225
229
|
# OEmbed.transform(some_string) do |r, url|
|
226
230
|
# r.from?(:provider_name) { |content| content["html"] }
|
227
231
|
# r.matches?(/some_regex_against_the_url/) { |content| content["title"] }
|
228
|
-
# r.video?
|
232
|
+
# r.video?(:template => "videos/oembed_link")
|
229
233
|
# r.audio? { |audio| content["html"] }
|
230
|
-
# r.hedgehog?
|
234
|
+
# r.hedgehog?(:template => File.join(File.dirname(__FILE__), "templates", "hedgehogs.haml"))
|
231
235
|
# r.photo? { |photo| "<img src='#{photo["url"]}' title='#{photo['title']} />" }
|
232
236
|
# r.any? { |anythingelse| content["title"] }
|
233
237
|
# end
|
@@ -248,6 +252,19 @@ class OEmbed
|
|
248
252
|
# The value passed to these conditional blocks is a hash representing the data returned
|
249
253
|
# by the server. The keys of all the attributes will be strings.
|
250
254
|
#
|
255
|
+
# If you specify the :template option, a template will be found for you based on your current engironment.
|
256
|
+
# Currently there is support for Haml, Erubis and ERB templates. Each template will have the following
|
257
|
+
# local variables available to it:
|
258
|
+
#
|
259
|
+
# url : The URL for which OEmbed data exists
|
260
|
+
# data : A hash of the actual OEmbed data for that URL
|
261
|
+
# response : The OEmbed::Response object for the URL
|
262
|
+
#
|
263
|
+
#
|
264
|
+
# If you are using Rails, you may specify your template relative to your application's
|
265
|
+
# view root (eg "photos/flickr_oembed"), and your template will be found based on your application settings.
|
266
|
+
# For more options regarding template support, see the documentation for OEmbed::TemplateResolver.
|
267
|
+
#
|
251
268
|
# NOTE: The type equivalent blocks (.video?, .audio?, .hedgehog?, .photo?, etc.) perform
|
252
269
|
# an equality test between the method name and the type returned by the OEmbed provider.
|
253
270
|
# You may specify any type name you wish as the method name, and its type will be checked
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# The class used to represent data returned by the server.
|
2
2
|
#
|
3
|
+
require 'oembed_links/template_resolver'
|
3
4
|
class OEmbed
|
4
5
|
class Response
|
5
6
|
|
@@ -23,22 +24,22 @@ class OEmbed
|
|
23
24
|
|
24
25
|
# Test if this response has been returned from
|
25
26
|
# the given provider_name.
|
26
|
-
def from?(provider_name, &block)
|
27
|
+
def from?(provider_name, *args, &block)
|
27
28
|
if @provider.to_s === provider_name.to_s
|
28
29
|
if can_render_type?(:provider)
|
29
30
|
@rendered_via_provider = true
|
30
|
-
return render_content(&block)
|
31
|
+
return render_content(*args, &block)
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
# Test if this response came from a URL
|
36
37
|
# that matches the given regex.
|
37
|
-
def matches?(regex, &block)
|
38
|
+
def matches?(regex, *args, &block)
|
38
39
|
if @url =~ regex
|
39
40
|
if can_render_type?(:regex)
|
40
41
|
@rendered_via_regex = true
|
41
|
-
render_content(&block)
|
42
|
+
render_content(*args, &block)
|
42
43
|
end
|
43
44
|
end
|
44
45
|
end
|
@@ -46,9 +47,9 @@ class OEmbed
|
|
46
47
|
# Lowest priority renderer, which will execute
|
47
48
|
# a block regardless of conditions so long as
|
48
49
|
# no content has yet been rendered for this response.
|
49
|
-
def any?(&block)
|
50
|
+
def any?(*args, &block)
|
50
51
|
if can_render_type?
|
51
|
-
return render_content(&block)
|
52
|
+
return render_content(*args, &block)
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
@@ -63,7 +64,7 @@ class OEmbed
|
|
63
64
|
if @response["type"] == ts
|
64
65
|
if can_render_type?(:type)
|
65
66
|
@rendered_via_type = true
|
66
|
-
return render_content(&block)
|
67
|
+
return render_content(*args, &block)
|
67
68
|
end
|
68
69
|
end
|
69
70
|
else
|
@@ -90,8 +91,11 @@ class OEmbed
|
|
90
91
|
!@rendered.nil?
|
91
92
|
end
|
92
93
|
|
93
|
-
def render_content(&block)
|
94
|
-
|
94
|
+
def render_content(*args, &block)
|
95
|
+
options = (args.last.is_a?(Hash)) ? args.last : { }
|
96
|
+
if options[:template]
|
97
|
+
@rendered = TemplateResolver.eval_template_for_path(options[:template], @url, @response, self)
|
98
|
+
elsif block_given?
|
95
99
|
@rendered = yield(@response)
|
96
100
|
else
|
97
101
|
@rendered = self.to_s
|
data/oembed_links.gemspec
CHANGED
data/spec/oembed_links_spec.rb
CHANGED
@@ -74,6 +74,8 @@ describe OEmbed, "transforming functions" do
|
|
74
74
|
"type" => "video"
|
75
75
|
}.to_json)
|
76
76
|
OEmbed.register_yaml_file(File.join(File.dirname(__FILE__), "oembed_links_test.yml"))
|
77
|
+
@current_path = File.dirname(__FILE__)
|
78
|
+
@template_path = File.join(@current_path, "templates")
|
77
79
|
end
|
78
80
|
|
79
81
|
it "should always give priority to provider conditional blocks" do
|
@@ -121,8 +123,124 @@ describe OEmbed, "transforming functions" do
|
|
121
123
|
r.matches?(/baz/) { |m| "regex" }
|
122
124
|
end.should == "foo"
|
123
125
|
end
|
126
|
+
|
127
|
+
it "should allow templates to be used to process the output" do
|
128
|
+
url_provides({
|
129
|
+
"url" => "template!"
|
130
|
+
}.to_json)
|
131
|
+
|
132
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
133
|
+
r.any?(:template => File.join(@template_path, "test.rhtml"))
|
134
|
+
end.should == "template! rhtml"
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
it "should allow a template root to be set programmatically to process the output" do
|
139
|
+
url_provides({
|
140
|
+
"url" => "template!"
|
141
|
+
}.to_json)
|
142
|
+
OEmbed::TemplateResolver.template_root = @template_path
|
143
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
144
|
+
r.any?(:template => "test.html.erb")
|
145
|
+
end.should == "template! erb\n"
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should allow for selection of different template renderers" do
|
149
|
+
url_provides({
|
150
|
+
"url" => "template!"
|
151
|
+
}.to_json)
|
152
|
+
defined?(Erubis).should_not == true
|
153
|
+
defined?(Haml).should_not == true
|
154
|
+
OEmbed::TemplateResolver.template_root = @template_path
|
155
|
+
OEmbed::TemplateResolver.template_processor = :erubis
|
156
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
157
|
+
r.any?(:template => "test.html.erb")
|
158
|
+
end.should == "template! erb\n"
|
159
|
+
defined?(Erubis).should == "constant"
|
160
|
+
OEmbed::TemplateResolver.template_processor = :haml
|
161
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
162
|
+
r.any?(:template => "test.haml")
|
163
|
+
end.should == "template! haml\n"
|
164
|
+
defined?(Haml).should == "constant"
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should throw an exception if a path to a template specified does not exist" do
|
168
|
+
url_provides({
|
169
|
+
"url" => "template!"
|
170
|
+
}.to_json)
|
171
|
+
lambda { OEmbed.transform("http://test1.net/foo") do |r, url|
|
172
|
+
r.any?(:template => "does.not.exist")
|
173
|
+
end }.should raise_error
|
174
|
+
end
|
124
175
|
|
125
176
|
|
126
177
|
end
|
127
178
|
|
128
179
|
|
180
|
+
describe OEmbed, "Rails template resolving functions" do
|
181
|
+
include SpecHelper
|
182
|
+
before(:each) do
|
183
|
+
OEmbed.clear_registrations
|
184
|
+
clear_urls
|
185
|
+
url_provides({
|
186
|
+
"html" => "foo",
|
187
|
+
"type" => "video",
|
188
|
+
"url" => "rails"
|
189
|
+
}.to_json)
|
190
|
+
OEmbed.register_yaml_file(File.join(File.dirname(__FILE__), "oembed_links_test.yml"))
|
191
|
+
OEmbed::TemplateResolver.template_processor = nil
|
192
|
+
@current_path = File.dirname(__FILE__)
|
193
|
+
@template_path = File.join(@current_path, "templates")
|
194
|
+
|
195
|
+
class ::ActionView
|
196
|
+
class Template
|
197
|
+
@@template_handlers = { }
|
198
|
+
|
199
|
+
def self.support_haml!
|
200
|
+
@@template_handlers = {"haml" => "nothing"}
|
201
|
+
end
|
202
|
+
|
203
|
+
def self.do_not_support_haml!
|
204
|
+
@@template_handlers = { }
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
class ::ApplicationController
|
211
|
+
def self.view_paths
|
212
|
+
[File.dirname(__FILE__)]
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should support Rails-like template paths for template selection" do
|
219
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
220
|
+
r.any?(:template => "templates/test")
|
221
|
+
end.should == "rails erb\n"
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should support Rails-like template paths with extension specified" do
|
225
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
226
|
+
r.any?(:template => "templates/test.rhtml")
|
227
|
+
end.should == "rails rhtml"
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should allow haml support in the Rails app" do
|
231
|
+
ActionView::Template.support_haml!
|
232
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
233
|
+
r.any?(:template => "templates/test")
|
234
|
+
end.should == "rails haml\n"
|
235
|
+
ActionView::Template.do_not_support_haml!
|
236
|
+
OEmbed.transform("http://test1.net/foo") do |r, url|
|
237
|
+
r.any?(:template => "templates/test")
|
238
|
+
end.should == "rails erb\n"
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|