Skip to content

Commit

Permalink
Merge pull request #97 from dysania/caching-support-for-preview
Browse files Browse the repository at this point in the history
Caching support for preview
  • Loading branch information
jzeta committed Sep 10, 2013
2 parents fa61d5d + 0e515a8 commit 3f20142
Show file tree
Hide file tree
Showing 52 changed files with 573 additions and 382 deletions.
20 changes: 18 additions & 2 deletions lib/onebox.rb
Expand Up @@ -4,14 +4,30 @@
require "mustache"
require "opengraph_parser"
require "verbal_expressions"
require "ostruct"
require "moneta"

require_relative "onebox/version"
require_relative "onebox/preview"
require_relative "onebox/matcher"
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
31 changes: 25 additions & 6 deletions lib/onebox/engine.rb
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions lib/onebox/engine/amazon_onebox.rb
Expand Up @@ -2,6 +2,7 @@ module Onebox
module Engine
class AmazonOnebox
include Engine
include HTML

matches do
#/^https?:\/\/(?:www\.)?amazon.(com|ca)\/.*$/
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/onebox/engine/bliptv_onebox.rb
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/onebox/engine/clikthrough_onebox.rb
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/onebox/engine/college_humor_onebox.rb
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/onebox/engine/dailymotion_onebox.rb
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/onebox/engine/dotsub_onebox.rb
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/onebox/engine/example_onebox.rb
Expand Up @@ -2,21 +2,22 @@ module Onebox
module Engine
class ExampleOnebox
include Engine
include HTML

matches do
find "example.com"
end

private

def extracted_data
def data
{
header: @body.css("html body h1")
header: raw.css("h1").inner_text
}
end

def template
%|<div class="onebox">{{{header}}}</div>|
%|<div class="onebox">{{header}}</div>|
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/onebox/engine/flickr_onebox.rb
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/onebox/engine/funny_or_die_onebox.rb
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions 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
10 changes: 5 additions & 5 deletions lib/onebox/engine/hulu_onebox.rb
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/onebox/engine/nfb_onebox.rb
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions 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
Expand Down
7 changes: 4 additions & 3 deletions lib/onebox/engine/qik_onebox.rb
Expand Up @@ -2,6 +2,7 @@ module Onebox
module Engine
class QikOnebox
include Engine
include HTML

matches do
# /^https?\:\/\/qik\.com\/video\/.*$/
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/onebox/engine/revision3_onebox.rb
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/onebox/engine/slideshare_onebox.rb
Expand Up @@ -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
Expand Down

0 comments on commit 3f20142

Please sign in to comment.