netshade-oembed_links 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -69,9 +69,16 @@ OEmbed.transform(text_to_transform) do |res, url|
69
69
  end
70
70
 
71
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.
72
+ of the form "video.html.erb" or "video.rhtml". If you are not using Rails, you may still use the
73
+ template functionality, but you must specify the full path to the template. When you are integrating
74
+ with Rails, you may use any template library that is installed for your Rails app; when you are using
75
+ the absolute filename method, you only have access to ERB, Erubis or HAML.
76
+
77
+ As of version 0.0.9, your Rails oembed templates have access to all the traditional Rails template helper methods
78
+ (such as Rails' link_to, image_tag, etc.); the templates are processed using the Rails template rendering
79
+ pipeline, and as such may even do evil things like access your Models.
80
+
81
+ See the RDocs for OEmbed::TemplateResolver for more information regarding templates and oembed_links.
75
82
 
76
83
 
77
84
 
@@ -80,6 +87,7 @@ See the rdocs for much more complete examples. The specs directory has some exa
80
87
  use, but the test to code ratio is slim atm.
81
88
 
82
89
  Thanks to the Indianapolis Star I&D department for open sourcing this; most notably Chris Vannoy for giving the okay.
90
+ Thanks to Kyle Slattery for adding Viddler to the example oembed_links.yml
83
91
 
84
92
 
85
93
  CZ - chris.zelenak!at!!indystar.com
@@ -33,12 +33,7 @@ class OEmbed
33
33
  # Resolves the template path for the given (potentially relative) path
34
34
  # If the given path is found to exist immediately, whether by itself
35
35
  # or when combined with the optionally present template_root (OEmbed::TemplateResolver.template_root),
36
- # it is returned immediately. Otherwise, a check is made to see if the
37
- # usual Rails classes (ActionView and ApplicationController) have been defined,
38
- # and if so, detect the Rails template path via the .view_paths class
39
- # method of ApplicationController. Furthermore, Haml support is detected
40
- # by presence of a Haml template handler in the ActionView::Template
41
- # class, and if it's found, given default precedence over the other template forms.
36
+ # it is returned immediately.
42
37
  #
43
38
  # If no path is found for the supplied template path, an exception
44
39
  # is raised.
@@ -47,21 +42,6 @@ class OEmbed
47
42
  found_path = nil
48
43
  if File.exists?(tmp_path)
49
44
  found_path = tmp_path
50
- else
51
- # Rails like templates
52
- if defined?(ApplicationController) && defined?(ActionView)
53
- exts = ["html.erb", "erb", "rhtml"]
54
- exts = (["haml"] + exts) if ActionView::Template.class_eval("@@template_handlers").keys.include?("haml")
55
- unless exts.any? { |e| path =~ /\.#{e}/ }
56
- for ext in exts
57
- if found_path = resolve_path_in_view_paths("#{path}.#{ext}", ApplicationController.view_paths)
58
- break
59
- end
60
- end
61
- end
62
- found_path ||= resolve_path_in_view_paths(path, ApplicationController.view_paths)
63
-
64
- end
65
45
  end
66
46
  unless found_path
67
47
  raise StandardError.new("File not found: #{path}")
@@ -72,48 +52,60 @@ class OEmbed
72
52
 
73
53
  # Evaluate the template at the given (possibly relative) path,
74
54
  # assigning the template the local variables url, data and response.
55
+ # If ApplicationController and ActionController have been defined, then
56
+ # it is assumed that you are specifying a Rails template path, and an instance
57
+ # ApplicationController will be used to render the results. You may use
58
+ # Rails-style helpers / models inside your template, as the ActionView rendering
59
+ # pipeline will be used. NOTE that to accomplish this, the Rails TestRequest
60
+ # and TestResponse classes will be loaded and used, if they have not been loaded already.
61
+ #
62
+ # If no Rails style template was found or you are not using rails, the following actions take place:
75
63
  # If you specify a template processor (via OEmbed::TemplateResolver.template_processor=)
76
- # then it will be used to process the template at the given path. Otherwise
77
- # a processor will be selected on the following criterion:
64
+ # then it will be used to process the template at the given path (taking any configured template_root
65
+ # into account. Otherwise a processor will be selected on the following criterion:
78
66
  #
79
67
  # If the file extension is haml, then the Haml library will be required and used
80
68
  # If the Erubis library has been loaded or the file extension is erubis, the Erubis library will be used
81
69
  # In all other cases, the ERB library will be used
70
+ #
71
+
82
72
  #
83
73
  # The evaluated result of the template will be returned.
84
74
  def self.eval_template_for_path(path, url, data, response)
85
- if actual_path = resolve_template_path(path)
75
+ if defined?(ApplicationController) && defined?(ActionController)
76
+ if !defined?(ActionController::TestRequest) ||
77
+ !defined?(ActionController::TestResponse)
78
+ require 'action_controller/test_process'
79
+ end
80
+ @app_c ||= ApplicationController.new
81
+ rendered_response = @app_c.process(ActionController::TestRequest.new,
82
+ ActionController::TestResponse.new,
83
+ :render_for_file,
84
+ path,
85
+ 200,
86
+ true,
87
+ { :data => data, :url => url, :response => response }).body
88
+ end
89
+ if rendered_response.nil? && actual_path = resolve_template_path(path)
86
90
  contents = File.read(actual_path)
87
91
  processor = (@template_processor || File.extname(actual_path)[1..4]).to_s
88
92
  has_erubis = defined?(Erubis)
89
93
  if processor == "haml"
90
94
  require 'haml' unless defined?(Haml)
91
- Haml::Engine.new(contents).render({ }, :data => data, :url => url, :response => response)
95
+ rendered_response = Haml::Engine.new(contents).render({ }, :data => data, :url => url, :response => response)
92
96
  elsif processor == "erubis" || has_erubis
93
97
  require 'erubis' unless has_erubis
94
- Erubis::Eruby.new(contents).result(:data => data, :url => url, :response => response)
98
+ rendered_response = Erubis::Eruby.new(contents).result(:data => data, :url => url, :response => response)
95
99
  else
96
100
  require 'erb' unless defined?(ERB)
97
- ERBTemplate.new(url, data, response).evaluate(contents)
98
- end
101
+ rendered_response = ERBTemplate.new(url, data, response).evaluate(contents)
102
+ end
99
103
  end
104
+ return rendered_response
100
105
  end
101
106
 
102
107
  private
103
108
 
104
- # Resolve a relative path among an array of potential base
105
- # paths, returning the found path if it exists or nil if
106
- # none were found.
107
- def self.resolve_path_in_view_paths(desired, view_paths)
108
- view_paths.each do |p|
109
- prop = File.join(p, desired)
110
- if File.exists?(prop)
111
- return prop
112
- end
113
- end
114
- return nil
115
- end
116
-
117
109
  class ERBTemplate
118
110
  attr_reader :url, :data, :response
119
111
 
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.8"
3
+ s.version = "0.0.9"
4
4
  s.author = "Indianapolis Star"
5
5
  s.email = "bugs at indystar dot com"
6
6
  s.homepage = "http://www.indystar.com"
@@ -6,6 +6,7 @@
6
6
  :vimeo: "http://www.vimeo.com/api/oembed.{format}"
7
7
  :hulu: "http://www.hulu.com/api/oembed.{format}"
8
8
  :flickr: "http://www.flickr.com/services/oembed"
9
+ :viddler: "http://lab.viddler.com/services/oembed/"
9
10
 
10
11
  :oohembed:
11
12
  :format: "json"
@@ -37,4 +38,9 @@
37
38
  :flickr:
38
39
  :format: "xml"
39
40
  :schemes:
40
- - "http://*.flickr.com/*"
41
+ - "http://*.flickr.com/*"
42
+
43
+ :viddler:
44
+ :format: "xml"
45
+ :schemes:
46
+ - "http://*.viddler.com/*"
@@ -234,31 +234,20 @@ describe OEmbed, "Rails template resolving functions" do
234
234
  OEmbed::TemplateResolver.template_processor = nil
235
235
  @current_path = File.dirname(__FILE__)
236
236
  @template_path = File.join(@current_path, "templates")
237
+
238
+ require 'actionpack'
239
+ require 'action_controller'
240
+ require 'action_controller/test_process'
237
241
 
238
- class ::ActionView
239
- class Template
240
- @@template_handlers = { }
241
-
242
- def self.support_haml!
243
- @@template_handlers = {"haml" => "nothing"}
244
- end
245
-
246
- def self.do_not_support_haml!
247
- @@template_handlers = { }
248
- end
249
-
250
- end
251
- end
252
-
253
- class ::ApplicationController
254
- def self.view_paths
255
- [File.dirname(__FILE__)]
256
- end
242
+ class ::ApplicationController < ActionController::Base
243
+
257
244
  end
258
-
245
+ ApplicationController.view_paths += [File.dirname(__FILE__)]
246
+ Dependencies.mark_for_unload(ApplicationController)
259
247
  end
260
248
 
261
249
  it "should support Rails-like template paths for template selection" do
250
+
262
251
  OEmbed.transform("http://test1.net/foo") do |r, url|
263
252
  r.any?(:template => "templates/test")
264
253
  end.should == "rails erb\n"
@@ -269,17 +258,6 @@ describe OEmbed, "Rails template resolving functions" do
269
258
  r.any?(:template => "templates/test.rhtml")
270
259
  end.should == "rails rhtml"
271
260
  end
272
-
273
- it "should allow haml support in the Rails app" do
274
- ActionView::Template.support_haml!
275
- OEmbed.transform("http://test1.net/foo") do |r, url|
276
- r.any?(:template => "templates/test")
277
- end.should == "rails haml\n"
278
- ActionView::Template.do_not_support_haml!
279
- OEmbed.transform("http://test1.net/foo") do |r, url|
280
- r.any?(:template => "templates/test")
281
- end.should == "rails erb\n"
282
- end
283
261
 
284
262
  end
285
263
 
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.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indianapolis Star
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-08 00:00:00 -07:00
12
+ date: 2008-08-13 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency