Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching support for preview #97

Merged
merged 48 commits into from
Sep 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d286ed4
starting tests for the preview method default options
vykster Aug 22, 2013
c46d57e
using OpenStruct to wrap options and setting up default caching system
vykster Aug 22, 2013
444754e
define fetch method on preview class #55
vykster Aug 22, 2013
f40bb9f
create test for fetch method in engine_spec
vykster Aug 27, 2013
009e7d9
refine and make fetch test pass for if cache exists
vykster Aug 27, 2013
3d0d97b
create fetch test for when cache key doesn't exist
vykster Aug 27, 2013
7fda729
edit description for cache expiration test
vykster Aug 29, 2013
99dfa9c
change extracted data to return empty hash in engine spec
vykster Aug 29, 2013
aff39d1
require and use moneta for cache #55
vykster Aug 29, 2013
9cd0271
make cache default options into a singleton method to dry code #55
vykster Aug 29, 2013
d2e2cfc
change has_key? to key? in spec to use moneta method
vykster Aug 30, 2013
0f90283
switch to caching extracted data instead of entire html response #55
vykster Aug 30, 2013
0c5ca4d
re-evaluating tests for checking cache
vykster Aug 30, 2013
cb66506
add tests for cache storage and expired cache replacement #55
jzeta Sep 4, 2013
f604541
start refactoring preview and engine modules b/c logic not using cach…
jzeta Sep 4, 2013
4212198
refactor amazon onebox - data method #55
jzeta Sep 4, 2013
a65ad03
refactor opengraph module - raw method #55
jzeta Sep 4, 2013
1ad1ad7
refactor oneboxes - change @body to raw in extracted_data method #55
jzeta Sep 4, 2013
f4682b2
extracted_data method renamed to record #55
jzeta Sep 4, 2013
a7bbed6
use raw instead of @body in rest of oneboxes #55
jzeta Sep 4, 2013
b17ac63
create defaults singleton method in Preview module for Engine module #55
jzeta Sep 4, 2013
86493a7
fix engine spec to use renamed record method #55
jzeta Sep 4, 2013
13bc0ce
refactor sound cloud extracted data method
vykster Sep 5, 2013
d2fd84b
change @body to raw in sound cloud data method to use refactored method
vykster Sep 5, 2013
a286ebd
remove html from example onebox template
vykster Sep 5, 2013
99478c3
remove html from expected result in example onebox
vykster Sep 5, 2013
8dfc5be
switch to new defaults reader interface
vykster Sep 5, 2013
916bdde
switch to preview to new defaults reader interface #55
vykster Sep 5, 2013
0dfc853
remove html from expected result in preview test #55
vykster Sep 5, 2013
6c0b665
use to_s method in onebox for test b/c record was not being accessed …
vykster Sep 5, 2013
332ebd7
add test to check if oneboxes behave like engines
vykster Sep 5, 2013
2633b78
add test to check if onebox methods work
vykster Sep 5, 2013
41239f5
rename record method in oneboxes to data #55
jzeta Sep 5, 2013
86e40bc
restore correct link urls in onebox specs #55
jzeta Sep 5, 2013
186ed3f
move fake web to global scope in onebox specs so tests use fixtures #55
jzeta Sep 5, 2013
ce24a3a
make raw method in opengraph module private b/c engine specifies priv…
jzeta Sep 5, 2013
3f898ee
change images selector in data method to use array#first as it is mor…
vykster Sep 6, 2013
559abf3
change image selector in data method to use array#first as it is more…
vykster Sep 6, 2013
61015f4
change images selector in data method to use array#first as it is mor…
vykster Sep 6, 2013
f2b2269
change image selector in data method to use array#first as it is more…
vykster Sep 6, 2013
d775c13
document template_name method #55
vykster Sep 6, 2013
b3f71f0
add documentation to #data in engine module #55
jzeta Sep 6, 2013
183b4c1
move raw method from Engine module to new HTML module to mimic OpenGr…
jzeta Sep 6, 2013
9ec66d5
include HTML module in oneboxes that use Nokogiri #55
jzeta Sep 6, 2013
542e5c0
require HTML module in Engine module #55
jzeta Sep 7, 2013
d9d3946
HTML module should be using Nokogiri, not OpenGraph, duh #55
jzeta Sep 7, 2013
60c6287
add documentation for #raw in Engine module #55
jzeta Sep 7, 2013
0e515a8
fix dotsub spec test for url. fixes #55
jzeta Sep 10, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/onebox.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write some documentation above this method.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the way we want to get to this data?

}
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/onebox/engine/clikthrough_onebox.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be how we get to this data?

}
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/onebox/engine/dailymotion_onebox.rb
Original file line number Diff line number Diff line change
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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the way to do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

video is just the url for the video; since we haven't established video embedding in oneboxes yet (or even if we want to), this is temporary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so it's a link to the video directly? And good point, make a new issue for that and mark it for Milestone 1.2. We'll need a template and a module that people can use to start using that template. Should also lead into the idea of having a default template.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a direct video link. Ok got it

}
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/onebox/engine/dotsub_onebox.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading