lib/onebox/engine.rb in onebox-1.1.0 vs lib/onebox/engine.rb in onebox-1.2.0

- old
+ new

@@ -11,94 +11,104 @@ end attr_reader :url attr_reader :cache attr_reader :timeout - attr_reader :view def initialize(link, cache = nil, timeout = nil) @url = link - @cache = cache || Onebox.defaults.cache - @timeout = timeout || Onebox.defaults.timeout - @view = View.new(self.class.template_name, true) + @cache = cache || Onebox.options.cache + @timeout = timeout || Onebox.options.timeout end + # raises error if not defined in onebox engine. This is the output method for + # an engine. def to_html - view.to_html(record) + fail NoMethodError, "Engines need to implement this method" end + # Some oneboxes create iframes or other complicated controls. If you're using + # a live editor with HTML preview, rendering those complicated controls can + # be slow or cause flickering. + # + # This method allows engines to produce a placeholder such as static image + # frame of a video. + # + # By default it just calls `to_html` unless implemented. + def placeholder_html + to_html + end + private def record - if cache.key?(url) - cache.fetch(url) - else - cache.store(url, data) - end + result = cache.fetch(url) { data } + cache[url] = result if cache.respond_to?(:key?) + result end # raises error if not defined in onebox engine - # in each onebox, uses either Nokogiri or OpenGraph to get raw HTML from url + # in each onebox, uses either Nokogiri or StandardEmbed to get raw HTML from url def raw - raise NoMethodError, "Engines need to implement this method" + fail NoMethodError, "Engines need to implement this method" end # raises error if not defined in onebox engine # in each onebox, returns hash of desired onebox content def data - raise NoMethodError, "Engines need this method defined" + fail NoMethodError, "Engines need this method defined" end + def link + @url.gsub(/['\"<>]/, { + "'" => '&#39;', + '&' => '&amp;', + '"' => '&quot;', + '<' => '&lt;', + '>' => '&gt;', + }) + end + module ClassMethods - def ===(object) - if object.kind_of?(String) - !!(object =~ class_variable_get(:@@matcher)) + def ===(other) + if other.kind_of?(URI) + !!(other.to_s =~ class_variable_get(:@@matcher)) else super end end def matches(&block) - class_variable_set :@@matcher, Hexpress.new(&block).to_r + matches_regexp(Hexpress.new(&block).to_r) end - # calculates handlebars template name for onebox using name of engine - def template_name + def matches_regexp(r) + class_variable_set :@@matcher, r + end + + # calculates a name for onebox using the class name of engine + def onebox_name name.split("::").last.downcase.gsub(/onebox/, "") end + end end end -require_relative "engine/open_graph" +require_relative "helpers" +require_relative "layout_support" +require_relative "engine/standard_embed" require_relative "engine/html" require_relative "engine/json" -require_relative "engine/example_onebox" require_relative "engine/amazon_onebox" -require_relative "engine/bliptv_onebox" -require_relative "engine/clikthrough_onebox" -require_relative "engine/college_humor_onebox" -require_relative "engine/dailymotion_onebox" -require_relative "engine/dotsub_onebox" -require_relative "engine/flickr_onebox" -require_relative "engine/funny_or_die_onebox" +require_relative "engine/classic_google_maps_onebox" require_relative "engine/github_blob_onebox" require_relative "engine/github_commit_onebox" require_relative "engine/github_gist_onebox" require_relative "engine/github_pullrequest_onebox" -require_relative "engine/hulu_onebox" -require_relative "engine/imgur_image_onebox" -require_relative "engine/itunes_onebox" -require_relative "engine/kinomap_onebox" -require_relative "engine/nfb_onebox" -require_relative "engine/qik_onebox" -require_relative "engine/revision3_onebox" -require_relative "engine/slideshare_onebox" -require_relative "engine/sound_cloud_onebox" -require_relative "engine/spotify_onebox" +require_relative "engine/google_play_app_onebox" +require_relative "engine/image_onebox" require_relative "engine/stack_exchange_onebox" -require_relative "engine/ted_onebox" -require_relative "engine/twitter_onebox" -require_relative "engine/viddler_onebox" -require_relative "engine/vimeo_onebox" +require_relative "engine/twitter_status_onebox" require_relative "engine/wikipedia_onebox" -require_relative "engine/yfrog_onebox" +require_relative "engine/youtube_onebox" +require_relative "engine/whitelisted_generic_onebox"