diff --git a/lib/onebox.rb b/lib/onebox.rb index c44f44f9..3d0292b0 100644 --- a/lib/onebox.rb +++ b/lib/onebox.rb @@ -4,6 +4,8 @@ require "mustache" require "opengraph_parser" require "verbal_expressions" +require "ostruct" +require "moneta" require_relative "onebox/version" require_relative "onebox/preview" @@ -11,7 +13,21 @@ require_relative "onebox/engine" module Onebox - def self.preview(url, args={}) - Preview.new(url, args) + DEFAULTS = { + cache: Moneta.new(:Memory, expires: true, serializer: :json) + } + + @@defaults = DEFAULTS + + def self.preview(url, options = Onebox.defaults) + Preview.new(url, options) + end + + def self.defaults + @@defaults + end + + def self.defaults=(options) + @@defaults = DEFAULTS.merge(options) end end diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb index 9c0f01d1..a0438b23 100644 --- a/lib/onebox/engine.rb +++ b/lib/onebox/engine.rb @@ -10,30 +10,48 @@ def self.engines end.map(&method(:const_get)) end - def initialize(link) + attr_reader :cache + + def initialize(link, cache = Onebox.defaults) @url = link - @body = read - @data = extracted_data + @cache = cache end def to_html - Mustache.render(template, @data) + Mustache.render(template, record) end private - def read - Nokogiri::HTML(open(@url)) + def record + if cache.key?(@url) + cache.fetch(@url) + else + cache.store(@url, data) + end + end + + # raises error if not defined in onebox engine + # in each onebox, uses either Nokogiri or OpenGraph to get raw HTML from url + def raw + raise NoMethodError, "Engines need to implement this method" end def template File.read(File.join("templates", "#{template_name}.handlebars")) end + # calculates handlebars template name for onebox using name of engine def template_name self.class.name.split("::").last.downcase.gsub(/onebox/, "") 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" + end + module ClassMethods def ===(object) if object.kind_of?(String) @@ -51,6 +69,7 @@ def matches(&block) end require_relative "engine/open_graph" +require_relative "engine/html" require_relative "engine/example_onebox" require_relative "engine/amazon_onebox" require_relative "engine/bliptv_onebox" diff --git a/lib/onebox/engine/amazon_onebox.rb b/lib/onebox/engine/amazon_onebox.rb index 1667fa4c..338e8090 100644 --- a/lib/onebox/engine/amazon_onebox.rb +++ b/lib/onebox/engine/amazon_onebox.rb @@ -2,6 +2,7 @@ module Onebox module Engine class AmazonOnebox include Engine + include HTML matches do #/^https?:\/\/(?:www\.)?amazon.(com|ca)\/.*$/ @@ -10,13 +11,13 @@ class AmazonOnebox private - def extracted_data + def data { url: @url, - name: @body.css("html body h1").inner_text, - image: @body.css("html body #main-image").first["src"], - description: @body.css("html body #postBodyPS").inner_text, - price: @body.css("html body .priceLarge").inner_text + name: raw.css("h1").inner_text, + image: raw.css("#main-image").first["src"], + description: raw.css("#postBodyPS").inner_text, + price: raw.css(".priceLarge").inner_text } end end diff --git a/lib/onebox/engine/bliptv_onebox.rb b/lib/onebox/engine/bliptv_onebox.rb index bee111a5..ae5e8af2 100644 --- a/lib/onebox/engine/bliptv_onebox.rb +++ b/lib/onebox/engine/bliptv_onebox.rb @@ -11,13 +11,13 @@ class BliptvOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images[0], - description: @body.description, - video: @body.metadata[:video].first[:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/clikthrough_onebox.rb b/lib/onebox/engine/clikthrough_onebox.rb index 974e1068..f2c9d450 100644 --- a/lib/onebox/engine/clikthrough_onebox.rb +++ b/lib/onebox/engine/clikthrough_onebox.rb @@ -11,11 +11,11 @@ class ClikThroughOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - description: @body.description + title: raw.title, + description: raw.description } end end diff --git a/lib/onebox/engine/college_humor_onebox.rb b/lib/onebox/engine/college_humor_onebox.rb index 77678710..c8d0baa1 100644 --- a/lib/onebox/engine/college_humor_onebox.rb +++ b/lib/onebox/engine/college_humor_onebox.rb @@ -11,13 +11,13 @@ class CollegeHumorOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images[0], - description: @body.description, - video: @body.metadata[:video].first[:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/dailymotion_onebox.rb b/lib/onebox/engine/dailymotion_onebox.rb index c134e3e5..bfed1993 100644 --- a/lib/onebox/engine/dailymotion_onebox.rb +++ b/lib/onebox/engine/dailymotion_onebox.rb @@ -11,13 +11,13 @@ class DailymotionOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description, - video: @body.metadata[:video][1][:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video][1][:_value] } end end diff --git a/lib/onebox/engine/dotsub_onebox.rb b/lib/onebox/engine/dotsub_onebox.rb index 68cbeb81..a1a1d7af 100644 --- a/lib/onebox/engine/dotsub_onebox.rb +++ b/lib/onebox/engine/dotsub_onebox.rb @@ -11,13 +11,13 @@ class DotsubOnebox private - def extracted_data + def data { - url: @body.metadata[:url].first[:_value], - title: @body.title, - image: @body.images.first, - description: @body.description, - video: @body.metadata[:video].first[:_value] + url: @url, + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/example_onebox.rb b/lib/onebox/engine/example_onebox.rb index 0846dad7..539faad5 100644 --- a/lib/onebox/engine/example_onebox.rb +++ b/lib/onebox/engine/example_onebox.rb @@ -2,6 +2,7 @@ module Onebox module Engine class ExampleOnebox include Engine + include HTML matches do find "example.com" @@ -9,14 +10,14 @@ class ExampleOnebox private - def extracted_data + def data { - header: @body.css("html body h1") + header: raw.css("h1").inner_text } end def template - %|
{{{header}}}
| + %|
{{header}}
| end end end diff --git a/lib/onebox/engine/flickr_onebox.rb b/lib/onebox/engine/flickr_onebox.rb index 0dfa3a4e..3c426673 100644 --- a/lib/onebox/engine/flickr_onebox.rb +++ b/lib/onebox/engine/flickr_onebox.rb @@ -11,12 +11,12 @@ class FlickrOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images[0], - description: @body.description + title: raw.title, + image: raw.images.first, + description: raw.description } end end diff --git a/lib/onebox/engine/funny_or_die_onebox.rb b/lib/onebox/engine/funny_or_die_onebox.rb index 3c9b07ae..67e3e31e 100644 --- a/lib/onebox/engine/funny_or_die_onebox.rb +++ b/lib/onebox/engine/funny_or_die_onebox.rb @@ -11,13 +11,13 @@ class FunnyOrDieOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description, - video: @body.metadata[:video].first[:url].first[:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:url].first[:_value] } end end diff --git a/lib/onebox/engine/html.rb b/lib/onebox/engine/html.rb new file mode 100644 index 00000000..ec1bc9e4 --- /dev/null +++ b/lib/onebox/engine/html.rb @@ -0,0 +1,11 @@ +module Onebox + module Engine + module HTML + private + + def raw + @raw ||= Nokogiri::HTML(open(@url)) + end + end + end +end diff --git a/lib/onebox/engine/hulu_onebox.rb b/lib/onebox/engine/hulu_onebox.rb index 33c1a23f..a9c49fd3 100644 --- a/lib/onebox/engine/hulu_onebox.rb +++ b/lib/onebox/engine/hulu_onebox.rb @@ -11,13 +11,13 @@ class HuluOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images[0], - description: @body.description, - video: @body.metadata[:video][1][:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video][1][:_value] } end end diff --git a/lib/onebox/engine/nfb_onebox.rb b/lib/onebox/engine/nfb_onebox.rb index 2a89540a..94b0714a 100644 --- a/lib/onebox/engine/nfb_onebox.rb +++ b/lib/onebox/engine/nfb_onebox.rb @@ -11,12 +11,12 @@ class NFBOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - description: @body.description, - video: @body.metadata[:video].first[:_value] + title: raw.title, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/open_graph.rb b/lib/onebox/engine/open_graph.rb index c8d0785e..5f9c7f8e 100644 --- a/lib/onebox/engine/open_graph.rb +++ b/lib/onebox/engine/open_graph.rb @@ -1,8 +1,10 @@ module Onebox module Engine module OpenGraph - def read - ::OpenGraph.new(@url) + private + + def raw + @raw ||= ::OpenGraph.new(@url) end end end diff --git a/lib/onebox/engine/qik_onebox.rb b/lib/onebox/engine/qik_onebox.rb index e15449e0..746705fc 100644 --- a/lib/onebox/engine/qik_onebox.rb +++ b/lib/onebox/engine/qik_onebox.rb @@ -2,6 +2,7 @@ module Onebox module Engine class QikOnebox include Engine + include HTML matches do # /^https?\:\/\/qik\.com\/video\/.*$/ @@ -10,11 +11,11 @@ class QikOnebox private - def extracted_data + def data { url: @url, - title: @body.css(".info h2").inner_text, - image: @body.css(".userphoto").first["src"] + title: raw.css(".info h2").inner_text, + image: raw.css(".userphoto").first["src"] } end end diff --git a/lib/onebox/engine/revision3_onebox.rb b/lib/onebox/engine/revision3_onebox.rb index 4ca96d02..809b5ce8 100644 --- a/lib/onebox/engine/revision3_onebox.rb +++ b/lib/onebox/engine/revision3_onebox.rb @@ -11,13 +11,13 @@ class Revision3Onebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description, - video: @body.metadata[:video].first[:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/slideshare_onebox.rb b/lib/onebox/engine/slideshare_onebox.rb index 7e08feae..0aea8b9d 100644 --- a/lib/onebox/engine/slideshare_onebox.rb +++ b/lib/onebox/engine/slideshare_onebox.rb @@ -11,12 +11,12 @@ class SlideshareOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description + title: raw.title, + image: raw.images.first, + description: raw.description } end end diff --git a/lib/onebox/engine/sound_cloud_onebox.rb b/lib/onebox/engine/sound_cloud_onebox.rb index 500be7fc..18a9e5e0 100644 --- a/lib/onebox/engine/sound_cloud_onebox.rb +++ b/lib/onebox/engine/sound_cloud_onebox.rb @@ -11,13 +11,13 @@ class SoundCloudOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description, - video: @body.metadata[:video][1][:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video][1][:_value] } end end diff --git a/lib/onebox/engine/stack_exchange_onebox.rb b/lib/onebox/engine/stack_exchange_onebox.rb index 31d9be24..f4827bea 100644 --- a/lib/onebox/engine/stack_exchange_onebox.rb +++ b/lib/onebox/engine/stack_exchange_onebox.rb @@ -2,6 +2,7 @@ module Onebox module Engine class StackExchangeOnebox include Engine + include HTML matches do # /^http:\/\/(?:(?:(?\w*)\.)?(?\w*)\.)?(?#{DOMAINS.join('|')})\.com\/(?:questions|q)\/(?\d*)/ @@ -10,11 +11,11 @@ class StackExchangeOnebox private - def extracted_data + def data { url: @url, - title: @body.css(".question-hyperlink").inner_text, - question: @body.css(".question .post-text p").first.inner_text + title: raw.css(".question-hyperlink").inner_text, + question: raw.css(".question .post-text p").first.inner_text } end end diff --git a/lib/onebox/engine/ted_onebox.rb b/lib/onebox/engine/ted_onebox.rb index b33be375..eed69a50 100644 --- a/lib/onebox/engine/ted_onebox.rb +++ b/lib/onebox/engine/ted_onebox.rb @@ -11,12 +11,12 @@ class TedOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description + title: raw.title, + image: raw.images.first, + description: raw.description } end end diff --git a/lib/onebox/engine/viddler_onebox.rb b/lib/onebox/engine/viddler_onebox.rb index 7097f468..b15cd021 100644 --- a/lib/onebox/engine/viddler_onebox.rb +++ b/lib/onebox/engine/viddler_onebox.rb @@ -11,13 +11,13 @@ class ViddlerOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description, - video: @body.metadata[:video].first[:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/vimeo_onebox.rb b/lib/onebox/engine/vimeo_onebox.rb index ba3f339b..0ee24809 100644 --- a/lib/onebox/engine/vimeo_onebox.rb +++ b/lib/onebox/engine/vimeo_onebox.rb @@ -11,13 +11,13 @@ class VimeoOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images[0], - description: @body.description, - video: @body.metadata[:video].first[:_value] + title: raw.title, + image: raw.images.first, + description: raw.description, + video: raw.metadata[:video].first[:_value] } end end diff --git a/lib/onebox/engine/wikipedia_onebox.rb b/lib/onebox/engine/wikipedia_onebox.rb index c3a8a67c..b0cbfbe7 100644 --- a/lib/onebox/engine/wikipedia_onebox.rb +++ b/lib/onebox/engine/wikipedia_onebox.rb @@ -2,6 +2,7 @@ module Onebox module Engine class WikipediaOnebox include Engine + include HTML matches do # /^https?:\/\/.*wikipedia\.(com|org)\/.*$/ @@ -10,12 +11,12 @@ class WikipediaOnebox private - def extracted_data + def data { url: @url, - name: @body.css("html body h1").inner_text, - image: @body.css(".infobox .image img").first["src"], - description: @body.css("html body p").inner_text + name: raw.css("html body h1").inner_text, + image: raw.css(".infobox .image img").first["src"], + description: raw.css("html body p").inner_text } end end diff --git a/lib/onebox/engine/yfrog_onebox.rb b/lib/onebox/engine/yfrog_onebox.rb index e4c4902f..207e125c 100644 --- a/lib/onebox/engine/yfrog_onebox.rb +++ b/lib/onebox/engine/yfrog_onebox.rb @@ -11,12 +11,12 @@ class YfrogOnebox private - def extracted_data + def data { url: @url, - title: @body.title, - image: @body.images.first, - description: @body.description + title: raw.title, + image: raw.images.first, + description: raw.description } end end diff --git a/lib/onebox/preview.rb b/lib/onebox/preview.rb index cba8224e..a497fab0 100644 --- a/lib/onebox/preview.rb +++ b/lib/onebox/preview.rb @@ -1,7 +1,11 @@ module Onebox class Preview - def initialize(link) + attr_reader :cache + + def initialize(link, parameters = Onebox.defaults) @url = link + @options = parameters + @cache = options.cache @engine = Matcher.new(@url).oneboxed end @@ -9,10 +13,14 @@ def to_s engine.to_html end + def options + OpenStruct.new(@options) + end + private def engine - @engine.new(@url) + @engine.new(@url, cache) end class InvalidURI < StandardError diff --git a/onebox.gemspec b/onebox.gemspec index 19403f08..3133f2c2 100644 --- a/onebox.gemspec +++ b/onebox.gemspec @@ -23,6 +23,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "nokogiri", "~> 1.4" spec.add_runtime_dependency "opengraph_parser", "~> 0.2" spec.add_runtime_dependency "verbal_expressions", "~> 0.1" + spec.add_runtime_dependency "moneta", "~> 0.7" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 2.14" diff --git a/spec/lib/onebox/engine/amazon_spec.rb b/spec/lib/onebox/engine/amazon_spec.rb index ef00f68c..55f471eb 100644 --- a/spec/lib/onebox/engine/amazon_spec.rb +++ b/spec/lib/onebox/engine/amazon_spec.rb @@ -1,14 +1,16 @@ require "spec_helper" describe Onebox::Engine::AmazonOnebox do + let(:link) { "http://amazon.com" } + before do + fake(link, response("amazon.response")) + end + + it_behaves_like "engines" + describe "#to_html" do - let(:link) { "http://example.com" } let(:html) { described_class.new(link).to_html } - before do - fake(link, response("amazon.response")) - end - it "returns the product title" do expect(html).to include("Knit Noro: Accessories: 30 Colorful Little Knits [Hardcover]") end @@ -28,6 +30,5 @@ it "returns the product URL" do expect(html).to include(link) end - end end diff --git a/spec/lib/onebox/engine/bliptv_spec.rb b/spec/lib/onebox/engine/bliptv_spec.rb index 9c8abddb..4254d3bd 100644 --- a/spec/lib/onebox/engine/bliptv_spec.rb +++ b/spec/lib/onebox/engine/bliptv_spec.rb @@ -2,29 +2,33 @@ describe Onebox::Engine::BliptvOnebox do let(:link) { "http://blip.tv" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("bliptv.response")) end - it "returns video title" do - expect(html).to include("SC2L Week 3 - Axiom vs Acer G6") - end + it_behaves_like "engines" - it "returns image" do - expect(html).to include("Striderdoom-SC2LWeek3AxiomVsAcerG6178-416.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Acer and Axiom go head to head in week 3!") - end + it "returns video title" do + expect(html).to include("SC2L Week 3 - Axiom vs Acer G6") + end - it "returns video" do - expect(html).to include("http://blip.tv/day9tv/sc2l-week-3-axiom-vs-acer-g6-6623829") - end + it "returns image" do + expect(html).to include("Striderdoom-SC2LWeek3AxiomVsAcerG6178-416.jpg") + end + + it "returns video description" do + expect(html).to include("Acer and Axiom go head to head in week 3!") + end + + it "returns video" do + expect(html).to include("http://blip.tv/day9tv/sc2l-week-3-axiom-vs-acer-g6-6623829") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/clikthrough_spec.rb b/spec/lib/onebox/engine/clikthrough_spec.rb index aadabe4a..2c72dd10 100644 --- a/spec/lib/onebox/engine/clikthrough_spec.rb +++ b/spec/lib/onebox/engine/clikthrough_spec.rb @@ -2,21 +2,25 @@ describe Onebox::Engine::ClikThroughOnebox do let(:link) { "http://www.clickthough.com"} - let(:html) { described_class.new(link).to_html } - before do fake(link, response("clikthrough.response")) end - it "returns video title" do - expect(html).to include("Keri Hilson - Knock You Down") - end + it_behaves_like "engines" - it "returns video description" do - expect(html).to include("Keri Hilson gets taken down by love once again") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } + + it "returns video title" do + expect(html).to include("Keri Hilson - Knock You Down") + end + + it "returns video description" do + expect(html).to include("Keri Hilson gets taken down by love once again") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/college_humor_spec.rb b/spec/lib/onebox/engine/college_humor_spec.rb index 7d449d3e..d3810678 100644 --- a/spec/lib/onebox/engine/college_humor_spec.rb +++ b/spec/lib/onebox/engine/college_humor_spec.rb @@ -2,29 +2,33 @@ describe Onebox::Engine::CollegeHumorOnebox do let(:link) { "http://collegehumor.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("collegehumor.response")) end - it "returns video title" do - expect(html).to include("Mitt Romney Style (Gangnam Style Parody)") - end + it_behaves_like "engines" - it "returns video image" do - expect(html).to include("a9febe641d5beb264bbab0de49272e5a-mitt-romney-style-gangnam-style-parody.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Heyyy wealthy ladies!"Mitt Romney Style" is now available on iTunes") - end + it "returns video title" do + expect(html).to include("Mitt Romney Style (Gangnam Style Parody)") + end - it "returns video URL" do - expect(html).to include("moogaloop.1.0.31.swf?clip_id=6830834&use_node_id=true&og=1&auto=true") - end + it "returns video image" do + expect(html).to include("a9febe641d5beb264bbab0de49272e5a-mitt-romney-style-gangnam-style-parody.jpg") + end + + it "returns video description" do + expect(html).to include("Heyyy wealthy ladies!"Mitt Romney Style" is now available on iTunes") + end + + it "returns video URL" do + expect(html).to include("moogaloop.1.0.31.swf?clip_id=6830834&use_node_id=true&og=1&auto=true") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/dailymotion_spec.rb b/spec/lib/onebox/engine/dailymotion_spec.rb index 22924260..2d7ba193 100644 --- a/spec/lib/onebox/engine/dailymotion_spec.rb +++ b/spec/lib/onebox/engine/dailymotion_spec.rb @@ -2,31 +2,35 @@ describe Onebox::Engine::DailymotionOnebox do let(:link) { "http://dailymotion.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("dailymotion.response")) end - it "returns video title" do - expect(html).to include("Two Door Cinema Club - Les Vielles Charrues 2013.") - end + it_behaves_like "engines" - it "returns video image" do - expect(html).to include("526x297-bxE.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Vibrez au son de l'electro-pop des Irlandais de Two Door Cinema Club,") - end + it "returns video title" do + expect(html).to include("Two Door Cinema Club - Les Vielles Charrues 2013.") + end - it "returns video URL" do - expect(html).to include("http://www.dailymotion.com/swf/video/x12h020?autoPlay=1") - end + it "returns video image" do + expect(html).to include("526x297-bxE.jpg") + end + + it "returns video description" do + expect(html).to include("Vibrez au son de l'electro-pop des Irlandais de Two Door Cinema Club,") + end + + it "returns video URL" do + expect(html).to include("http://www.dailymotion.com/swf/video/x12h020?autoPlay=1") + end - it "returns video embed code" + it "returns video embed code" - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/dotsub_spec.rb b/spec/lib/onebox/engine/dotsub_spec.rb index 91a41d1e..3542a4b2 100644 --- a/spec/lib/onebox/engine/dotsub_spec.rb +++ b/spec/lib/onebox/engine/dotsub_spec.rb @@ -1,32 +1,36 @@ require "spec_helper" describe Onebox::Engine::DotsubOnebox do - let(:link) { "http://funnyordie.com" } - let(:html) { described_class.new(link).to_html } - + let(:link) { "http://dotsub.com/view/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78" } before do fake(link, response("dotsub.response")) end - it "returns video title" do - expect(html).to include("Twitter in Plain English") - end + it_behaves_like "engines" - it "returns video description" do - # og:decription tag spelled wrong in http response - pending - expect(html).to include("A short explanation of the micro-blogging service, Twitter.") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video photo" do - expect(html).to include("/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78/p") - end + it "returns video title" do + expect(html).to include("Twitter in Plain English") + end - it "returns video URL" do - expect(html).to include("dotsub.com/media/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78/m/flv/") - end + it "returns video description" do + # og:decription tag spelled wrong in http response + pending + expect(html).to include("A short explanation of the micro-blogging service, Twitter.") + end + + it "returns video photo" do + expect(html).to include("/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78/p") + end + + it "returns video URL" do + expect(html).to include("dotsub.com/media/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78/m/flv/") + end - it "returns URL" do - expect(html).to include("http://dotsub.com/view/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78") + it "returns URL" do + expect(html).to include("http://dotsub.com/view/665bd0d5-a9f4-4a07-9d9e-b31ba926ca78") + end end end diff --git a/spec/lib/onebox/engine/example_spec.rb b/spec/lib/onebox/engine/example_spec.rb index f6263643..29fd7b4e 100644 --- a/spec/lib/onebox/engine/example_spec.rb +++ b/spec/lib/onebox/engine/example_spec.rb @@ -1,16 +1,18 @@ require "spec_helper" describe Onebox::Engine::ExampleOnebox do + let(:link) { "http://example.com" } + before do + fake(link, response("example.response")) + end + + it_behaves_like "engines" + describe "#to_html" do - let(:link) { "http://example.com" } let(:html) { described_class.new(link).to_html } - before do - fake(link, response("example.response")) - end - it "returns template if given valid data" do - expect(html).to include(onebox_view("

Example Domain 1

")) + expect(html).to include(onebox_view("Example Domain 1")) end end end diff --git a/spec/lib/onebox/engine/flickr_spec.rb b/spec/lib/onebox/engine/flickr_spec.rb index 72d35f95..4c526108 100644 --- a/spec/lib/onebox/engine/flickr_spec.rb +++ b/spec/lib/onebox/engine/flickr_spec.rb @@ -2,25 +2,29 @@ describe Onebox::Engine::FlickrOnebox do let(:link) { "http://flickr.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("flickr.response")) end - it "returns photo title" do - expect(html).to include("Los Angeles View 2011") - end + it_behaves_like "engines" - it "returns photo" do - expect(html).to include("6038315155_2875860c4b_z.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns photo description" do - expect(html).to include("The view from the Griffith Observatory, Los Angeles; July 2011") - end + it "returns photo title" do + expect(html).to include("Los Angeles View 2011") + end + + it "returns photo" do + expect(html).to include("6038315155_2875860c4b_z.jpg") + end + + it "returns photo description" do + expect(html).to include("The view from the Griffith Observatory, Los Angeles; July 2011") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/funny_or_die_spec.rb b/spec/lib/onebox/engine/funny_or_die_spec.rb index 8a5894bc..31a816c5 100644 --- a/spec/lib/onebox/engine/funny_or_die_spec.rb +++ b/spec/lib/onebox/engine/funny_or_die_spec.rb @@ -2,29 +2,33 @@ describe Onebox::Engine::FunnyOrDieOnebox do let(:link) { "http://funnyordie.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("funnyordie.response")) end - it "returns video title" do - expect(html).to include("The Landlord") - end + it_behaves_like "engines" - it "returns video photo" do - expect(html).to include("c480x270_18.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Will Ferrell meets his landlord.") - end + it "returns video title" do + expect(html).to include("The Landlord") + end - it "returns video URL" do - expect(html).to include("http://www.funnyordie.com/videos/74/the-landlord-from-will-ferrell-and-adam-ghost-panther-mckay") - end + it "returns video photo" do + expect(html).to include("c480x270_18.jpg") + end + + it "returns video description" do + expect(html).to include("Will Ferrell meets his landlord.") + end + + it "returns video URL" do + expect(html).to include("http://www.funnyordie.com/videos/74/the-landlord-from-will-ferrell-and-adam-ghost-panther-mckay") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/hulu_spec.rb b/spec/lib/onebox/engine/hulu_spec.rb index 1673aad3..16402659 100644 --- a/spec/lib/onebox/engine/hulu_spec.rb +++ b/spec/lib/onebox/engine/hulu_spec.rb @@ -2,29 +2,33 @@ describe Onebox::Engine::HuluOnebox do let(:link) { "http://hulu.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("hulu.response")) end - it "returns video title" do - expect(html).to include("The Awesomes: Pilot, Part 1") - end + it_behaves_like "engines" - it "returns photo" do - expect(html).to include("http://ib3.huluim.com/video/60245466?region=US&size=600x400") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("After Mr. Awesome decides to retire and disband The Awesomes") - end + it "returns video title" do + expect(html).to include("The Awesomes: Pilot, Part 1") + end - it "returns video URL" do - expect(html).to include("https://secure.hulu.com/embed/0-us7uHJgevua5TeiGwCxQ") - end + it "returns photo" do + expect(html).to include("http://ib3.huluim.com/video/60245466?region=US&size=600x400") + end + + it "returns video description" do + expect(html).to include("After Mr. Awesome decides to retire and disband The Awesomes") + end + + it "returns video URL" do + expect(html).to include("https://secure.hulu.com/embed/0-us7uHJgevua5TeiGwCxQ") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/nfb_spec.rb b/spec/lib/onebox/engine/nfb_spec.rb index 285889ec..5b35bd07 100644 --- a/spec/lib/onebox/engine/nfb_spec.rb +++ b/spec/lib/onebox/engine/nfb_spec.rb @@ -2,30 +2,34 @@ describe Onebox::Engine::NFBOnebox do let(:link) { "http://nfb.ca" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("nfb.response")) end - it "returns video title" do - expect(html).to include("Overdose") - end + it_behaves_like "engines" - it "returns video description" do - expect(html).to include("With school, tennis lessons, swimming lessons, art classes,") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video URL" do - expect(html).to include("http://www.nfb.ca/film/overdose_en/") - end + it "returns video title" do + expect(html).to include("Overdose") + end - it "returns the video embed code" do - pending - expect(html).to include("") - end + it "returns video description" do + expect(html).to include("With school, tennis lessons, swimming lessons, art classes,") + end + + it "returns video URL" do + expect(html).to include("http://www.nfb.ca/film/overdose_en/") + end + + it "returns the video embed code" do + pending + expect(html).to include("") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/qik_spec.rb b/spec/lib/onebox/engine/qik_spec.rb index 755c6a27..460896ba 100644 --- a/spec/lib/onebox/engine/qik_spec.rb +++ b/spec/lib/onebox/engine/qik_spec.rb @@ -1,14 +1,16 @@ require "spec_helper" describe Onebox::Engine::QikOnebox do + let(:link) { "http://qik.com" } + before do + fake(link, response("qik.response")) + end + + it_behaves_like "engines" + describe "#to_html" do - let(:link) { "http://qik.com" } let(:html) { described_class.new(link).to_html } - before do - fake(link, response("qik.response")) - end - it "returns the video title" do expect(html).to include("20910") end diff --git a/spec/lib/onebox/engine/revision3_spec.rb b/spec/lib/onebox/engine/revision3_spec.rb index 0eb868b0..4fefbe73 100644 --- a/spec/lib/onebox/engine/revision3_spec.rb +++ b/spec/lib/onebox/engine/revision3_spec.rb @@ -1,32 +1,36 @@ require "spec_helper" describe Onebox::Engine::Revision3Onebox do - let(:link) { "http://collegehumor.com" } - let(:html) { described_class.new(link).to_html } - + let(:link) { "http://revision3.com" } before do fake(link, response("revision3.response")) end - it "returns video title" do - expect(html).to include("Blue Shark Bites Diver's Arm") - end + it_behaves_like "engines" - it "returns video image" do - expect(html).to include("discoverysharks--0029--blue-sharks--medium.thumb.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Blue Sharks swimming and eating in the open ocean.") - end + it "returns video title" do + expect(html).to include("Blue Shark Bites Diver's Arm") + end - it "returns video URL" do - expect(html).to include("https://revision3.com/player-v22668") - end + it "returns video image" do + expect(html).to include("discoverysharks--0029--blue-sharks--medium.thumb.jpg") + end + + it "returns video description" do + expect(html).to include("Blue Sharks swimming and eating in the open ocean.") + end + + it "returns video URL" do + expect(html).to include("https://revision3.com/player-v22668") + end - it "returns video embed code" + it "returns video embed code" - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/slideshare_spec.rb b/spec/lib/onebox/engine/slideshare_spec.rb index e33f1b6b..312cad51 100644 --- a/spec/lib/onebox/engine/slideshare_spec.rb +++ b/spec/lib/onebox/engine/slideshare_spec.rb @@ -2,25 +2,29 @@ describe Onebox::Engine::SlideshareOnebox do let(:link) { "http://slideshare.net" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("slideshare.response")) end - it "returns presentation title" do - expect(html).to include("12 Local Traditions That") - end + it_behaves_like "engines" - it "returns presentation description" do - expect(html).to include("12 Local traditions that will make") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns presentation image" do - expect(html).to include("12localtraditions-130729070157-phpapp01-thumbnail-4") - end + it "returns presentation title" do + expect(html).to include("12 Local Traditions That") + end + + it "returns presentation description" do + expect(html).to include("12 Local traditions that will make") + end + + it "returns presentation image" do + expect(html).to include("12localtraditions-130729070157-phpapp01-thumbnail-4") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/sound_cloud_spec.rb b/spec/lib/onebox/engine/sound_cloud_spec.rb index 95cf9f30..6863509d 100644 --- a/spec/lib/onebox/engine/sound_cloud_spec.rb +++ b/spec/lib/onebox/engine/sound_cloud_spec.rb @@ -1,32 +1,36 @@ require "spec_helper" describe Onebox::Engine::SoundCloudOnebox do - let(:link) { "http://dailymotion.com" } - let(:html) { described_class.new(link).to_html } - + let(:link) { "http://soundcloud.com" } before do fake(link, response("soundcloud.response")) end - it "returns video title" do - expect(html).to include("Penguin Prison - Worse It Gets (RAC Mix)") - end + it_behaves_like "engines" - it "returns video image" do - expect(html).to include("artworks-000033643332-vpuznu-t500x500.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Remix by André Allen Anjos.") - end + it "returns video title" do + expect(html).to include("Penguin Prison - Worse It Gets (RAC Mix)") + end - it "returns video URL" do - expect(html).to include("Remix by André Allen Anjos.") - end + it "returns video image" do + expect(html).to include("artworks-000033643332-vpuznu-t500x500.jpg") + end + + it "returns video description" do + expect(html).to include("Remix by André Allen Anjos.") + end + + it "returns video URL" do + expect(html).to include("Remix by André Allen Anjos.") + end - it "returns video embed code" + it "returns video embed code" - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/stack_exchange_spec.rb b/spec/lib/onebox/engine/stack_exchange_spec.rb index d52a7e5c..edcab6fb 100644 --- a/spec/lib/onebox/engine/stack_exchange_spec.rb +++ b/spec/lib/onebox/engine/stack_exchange_spec.rb @@ -1,14 +1,16 @@ require "spec_helper" describe Onebox::Engine::StackExchangeOnebox do + let(:link) { "http://stackexchange.com" } + before do + fake(link, response("stackexchange.response")) + end + + it_behaves_like "engines" + describe "#to_html" do - let(:link) { "http://stackexchange.com" } let(:html) { described_class.new(link).to_html } - before do - fake(link, response("stackexchange.response")) - end - it "returns the question title" do expect(html).to include("Concept behind these 4 lines of tricky C++ code") end diff --git a/spec/lib/onebox/engine/ted_spec.rb b/spec/lib/onebox/engine/ted_spec.rb index d4d3813c..33941e95 100644 --- a/spec/lib/onebox/engine/ted_spec.rb +++ b/spec/lib/onebox/engine/ted_spec.rb @@ -2,25 +2,29 @@ describe Onebox::Engine::TedOnebox do let(:link) { "http://ted.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("ted.response")) end - it "returns video title" do - expect(html).to include("Eli Beer: The fastest ambulance? A motorcycle") - end + it_behaves_like "engines" - it "returns video photo" do - expect(html).to include("aa8d0403aec3466d031e3e1c1605637d84d6a07d_389x292.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("As a young EMT on a Jerusalem ambulance") - end + it "returns video title" do + expect(html).to include("Eli Beer: The fastest ambulance? A motorcycle") + end + + it "returns video photo" do + expect(html).to include("aa8d0403aec3466d031e3e1c1605637d84d6a07d_389x292.jpg") + end + + it "returns video description" do + expect(html).to include("As a young EMT on a Jerusalem ambulance") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/viddler_spec.rb b/spec/lib/onebox/engine/viddler_spec.rb index f01c95a1..b87b4631 100644 --- a/spec/lib/onebox/engine/viddler_spec.rb +++ b/spec/lib/onebox/engine/viddler_spec.rb @@ -2,31 +2,35 @@ describe Onebox::Engine::ViddlerOnebox do let(:link) { "http://viddler.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("viddler.response")) end - it "returns video title" do - expect(html).to include("Viddler Demo") - end + it_behaves_like "engines" - it "returns video image" do - expect(html).to include("thumbnail_2_7164f749_v2.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("Get familiar with your Viddler account.") - end + it "returns video title" do + expect(html).to include("Viddler Demo") + end - it "returns video URL" do - expect(html).to include("http://www.viddler.com/player/7164f749") - end + it "returns video image" do + expect(html).to include("thumbnail_2_7164f749_v2.jpg") + end + + it "returns video description" do + expect(html).to include("Get familiar with your Viddler account.") + end + + it "returns video URL" do + expect(html).to include("http://www.viddler.com/player/7164f749") + end - it "returns video embed code" + it "returns video embed code" - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/vimeo_spec.rb b/spec/lib/onebox/engine/vimeo_spec.rb index 7edd6ad1..20b34c5c 100644 --- a/spec/lib/onebox/engine/vimeo_spec.rb +++ b/spec/lib/onebox/engine/vimeo_spec.rb @@ -2,29 +2,33 @@ describe Onebox::Engine::VimeoOnebox do let(:link) { "http://vimeo.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("vimeo.response")) end - it "returns video title" do - expect(html).to include("108 years of Herman Miller in 108 seconds") - end + it_behaves_like "engines" - it "returns video photo" do - expect(html).to include("443673159_1280.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns video description" do - expect(html).to include("To mark the launch of a new website for Hermann Miller furniture") - end + it "returns video title" do + expect(html).to include("108 years of Herman Miller in 108 seconds") + end - it "returns video URL" do - expect(html).to include("http://vimeo.com/moogaloop.swf?clip_id=70437049") - end + it "returns video photo" do + expect(html).to include("443673159_1280.jpg") + end + + it "returns video description" do + expect(html).to include("To mark the launch of a new website for Hermann Miller furniture") + end + + it "returns video URL" do + expect(html).to include("http://vimeo.com/moogaloop.swf?clip_id=70437049") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine/wikipedia_spec.rb b/spec/lib/onebox/engine/wikipedia_spec.rb index 7eee1616..cd29df3c 100644 --- a/spec/lib/onebox/engine/wikipedia_spec.rb +++ b/spec/lib/onebox/engine/wikipedia_spec.rb @@ -1,13 +1,15 @@ require "spec_helper" describe Onebox::Engine::WikipediaOnebox do - describe "to_html" do - let(:link) { "http://example.com" } - let(:html) { described_class.new(link).to_html } + let(:link) { "http://wikipedia.com" } + before do + fake(link, response("wikipedia.response")) + end - before do - fake(link, response("wikipedia.response")) - end + it_behaves_like "engines" + + describe "#to_html" do + let(:html) { described_class.new(link).to_html } it "returns the product URL" do expect(html).to include(link) diff --git a/spec/lib/onebox/engine/yfrog_spec.rb b/spec/lib/onebox/engine/yfrog_spec.rb index 6a815953..3bbf8153 100644 --- a/spec/lib/onebox/engine/yfrog_spec.rb +++ b/spec/lib/onebox/engine/yfrog_spec.rb @@ -2,25 +2,29 @@ describe Onebox::Engine::YfrogOnebox do let(:link) { "http://yfrog.com" } - let(:html) { described_class.new(link).to_html } - before do fake(link, response("yfrog.response")) end - it "returns photo title" do - expect(html).to include("Celebrating @questlove Cook4Quest w/WyattCenac") - end + it_behaves_like "engines" - it "returns photo" do - expect(html).to include("jjdob.jpg") - end + describe "#to_html" do + let(:html) { described_class.new(link).to_html } - it "returns photo description" do - expect(html).to include("Click on the photo to comment, share or view other great photos") - end + it "returns photo title" do + expect(html).to include("Celebrating @questlove Cook4Quest w/WyattCenac") + end + + it "returns photo" do + expect(html).to include("jjdob.jpg") + end + + it "returns photo description" do + expect(html).to include("Click on the photo to comment, share or view other great photos") + end - it "returns URL" do - expect(html).to include(link) + it "returns URL" do + expect(html).to include(link) + end end end diff --git a/spec/lib/onebox/engine_spec.rb b/spec/lib/onebox/engine_spec.rb index 55a52330..2866ea19 100644 --- a/spec/lib/onebox/engine_spec.rb +++ b/spec/lib/onebox/engine_spec.rb @@ -2,6 +2,10 @@ class Onebox::Engine::Foo include Onebox::Engine + + def record + "foo" + end end describe Onebox::Engine do @@ -9,6 +13,20 @@ class Onebox::Engine::Foo it "returns formatted html" end + describe "#record" do + it "returns cache value for given url if cache exists" do + cache = { "http://example.com" => "foo" } + result = Onebox::Engine::Foo.new("http://example.com", cache).send(:record) + expect(result).to eq("foo") + end + + it "stores cache value for given url if cache key doesn't exist" do + cache = { "http://example.com1" => "foo" } + result = Onebox::Engine::Foo.new("http://example.com").send(:record) + expect(result).to eq("foo") + end + end + describe ".===" do it "returns true if argument matches the matcher" do class Onebox::Engine::Foo diff --git a/spec/lib/onebox/preview_spec.rb b/spec/lib/onebox/preview_spec.rb index d952325d..415a850c 100644 --- a/spec/lib/onebox/preview_spec.rb +++ b/spec/lib/onebox/preview_spec.rb @@ -5,7 +5,7 @@ it "returns some html if given a valid url" do fake("http://www.example.com", response("example.response")) preview = described_class.new("http://www.example.com") - expect(preview.to_s).to include("

Example Domain 1

") + expect(preview.to_s).to include("Example Domain 1") end it "returns an empty string if the resource is not found" it "returns an empty string if the resource fails to load" diff --git a/spec/lib/onebox_spec.rb b/spec/lib/onebox_spec.rb index e69de29b..ad4feb6c 100644 --- a/spec/lib/onebox_spec.rb +++ b/spec/lib/onebox_spec.rb @@ -0,0 +1,28 @@ +require "spec_helper" + +describe Onebox do + describe ".preview" do + it "creates a cache that responds as expected" do + url = "http://www.example.com" + preview = Onebox.preview(url) + cache = preview.cache + expect(cache).to respond_to(:key?, :store, :fetch) + end + + it "stores the value in cache if it doesn't exist" do + url = "http://www.example.com" + preview = Onebox.preview(url) + preview.to_s + cache = preview.cache + expect(cache.key?(url)).to eq(true) + end + + it "replaces the cache if the cache is expired" do + url = "http://www.example.com" + preview = Onebox.preview(url, cache: Moneta.new(:Memory, expires: 100000, serializer: :json)) + cache = preview.cache + expect(cache.fetch(url)).to be(nil) + end + + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9390fd01..623b49a2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,4 +13,14 @@ RSpec.configure do |config| config.include HTMLSpecHelper -end \ No newline at end of file +end + +shared_examples_for "engines" do + it "should behave like an engine" do + expect(described_class.private_instance_methods).to include(:data, :record, :raw) + end + + it "should have implemented a data method" do + expect { described_class.new(link).send(:data) }.not_to raise_error + end +end