link_thumbnailer 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.0.6
2
+
3
+ - Fix: Issue when setting `strict` option. Always returning OG representation.
4
+
1
5
  # 1.0.5
2
6
 
3
7
  - Thanks to [phlegx](https://github.com/phlegx), support for timeout http connection through configurations.
@@ -5,7 +5,7 @@ module LinkThumbnailer
5
5
 
6
6
  def create
7
7
  @preview = LinkThumbnailer.generate(params[:url])
8
- render :json => @preview.to_json
8
+ respond_with @preview
9
9
  end
10
10
  end
11
11
  end
@@ -1,5 +1,5 @@
1
1
  require 'link_thumbnailer/engine' if defined? Rails
2
- require 'link_thumbnailer/config'
2
+ require 'link_thumbnailer/configuration'
3
3
  require 'link_thumbnailer/object'
4
4
  require 'link_thumbnailer/fetcher'
5
5
  require 'link_thumbnailer/doc_parser'
@@ -24,21 +24,21 @@ module LinkThumbnailer
24
24
 
25
25
  def config
26
26
  self.configuration ||= Configuration.new(
27
- :mandatory_attributes => %w(url title images),
28
- :strict => true,
29
- :redirect_limit => 3,
30
- :blacklist_urls => [
27
+ mandatory_attributes: %w(url title images),
28
+ strict: true,
29
+ redirect_limit: 3,
30
+ blacklist_urls: [
31
31
  %r{^http://ad\.doubleclick\.net/},
32
32
  %r{^http://b\.scorecardresearch\.com/},
33
33
  %r{^http://pixel\.quantserve\.com/},
34
34
  %r{^http://s7\.addthis\.com/}
35
35
  ],
36
- :rmagick_attributes => %w(source_url mime_type colums rows filesize number_colors),
37
- :limit => 10,
38
- :top => 5,
39
- :user_agent => 'linkthumbnailer',
40
- :verify_ssl => true,
41
- :http_timeout => 5
36
+ rmagick_attributes: %w(source_url mime_type colums rows filesize number_colors),
37
+ limit: 10,
38
+ top: 5,
39
+ user_agent: 'linkthumbnailer',
40
+ verify_ssl: true,
41
+ http_timeout: 5
42
42
  )
43
43
  end
44
44
 
@@ -72,19 +72,24 @@ module LinkThumbnailer
72
72
  end
73
73
 
74
74
  def opengraph(doc)
75
+ return nil unless opengraph?(doc)
75
76
  self.object = LinkThumbnailer::Opengraph.parse(self.object, doc)
76
77
  return self.object if self.object.valid?
77
78
  nil
78
79
  end
79
80
 
80
81
  def custom(doc)
81
- self.object[:title] = doc.title
82
+ self.object[:title] = doc.title
82
83
  self.object[:description] = doc.description
83
- self.object[:images] = self.img_parser.parse(doc.img_abs_urls.dup)
84
+ self.object[:images] = self.img_parser.parse(doc.img_abs_urls.dup)
84
85
  return self.object if self.object.valid?
85
86
  nil
86
87
  end
87
88
 
89
+ def opengraph?(doc)
90
+ !doc.xpath('//meta[starts-with(@property, "og:") and @content]').empty?
91
+ end
92
+
88
93
  end
89
94
 
90
95
  end
@@ -24,10 +24,10 @@ module LinkThumbnailer
24
24
  end
25
25
 
26
26
  result << if u.is_a?(URI::HTTP)
27
- u
28
- else
29
- URI.join(base_url || doc_base_href || source_url, i)
30
- end
27
+ u
28
+ else
29
+ URI.join(base_url || doc_base_href || source_url, i)
30
+ end
31
31
  end
32
32
 
33
33
  result
@@ -8,22 +8,21 @@ module LinkThumbnailer
8
8
 
9
9
  def fetch(url, redirect_count = 0)
10
10
  if redirect_count > LinkThumbnailer.configuration.redirect_limit
11
- raise ArgumentError,
12
- "too many redirects (#{redirect_count})"
11
+ raise ArgumentError, "too many redirects (#{redirect_count})"
13
12
  end
14
13
 
15
14
  self.url = url.is_a?(URI) ? url : URI(url)
16
15
 
17
16
  if self.url.is_a?(URI::HTTP)
18
- http = Net::HTTP::Persistent.new('linkthumbnailer')
19
- http.headers['User-Agent'] = LinkThumbnailer.configuration.user_agent
20
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless LinkThumbnailer.configuration.verify_ssl
21
- http.open_timeout = LinkThumbnailer.configuration.http_timeout
22
- resp = http.request(self.url)
17
+ http = Net::HTTP::Persistent.new('linkthumbnailer')
18
+ http.headers['User-Agent'] = LinkThumbnailer.configuration.user_agent
19
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless LinkThumbnailer.configuration.verify_ssl
20
+ http.open_timeout = LinkThumbnailer.configuration.http_timeout
21
+ resp = http.request(self.url)
23
22
  case resp
24
- when Net::HTTPSuccess; resp.body
25
- when Net::HTTPRedirection; fetch(resp['location'], redirect_count + 1)
26
- else resp.error!
23
+ when Net::HTTPSuccess then resp.body
24
+ when Net::HTTPRedirection then fetch(resp['location'], redirect_count + 1)
25
+ else resp.error!
27
26
  end
28
27
  end
29
28
  end
@@ -5,8 +5,8 @@ module LinkThumbnailer
5
5
  class ImgParser
6
6
 
7
7
  def initialize(fetcher, img_url_filter)
8
- @fetcher = fetcher
9
- @img_url_filters = [*img_url_filter]
8
+ @fetcher = fetcher
9
+ @img_url_filters = [*img_url_filter]
10
10
  end
11
11
 
12
12
  def parse(img_urls)
@@ -14,7 +14,7 @@ module LinkThumbnailer
14
14
  img_urls.delete_if { |i| filter.reject?(i) }
15
15
  end
16
16
 
17
- imgs = []
17
+ imgs = []
18
18
  count = 0
19
19
  img_urls.each { |i|
20
20
  break if count >= LinkThumbnailer.configuration.limit
@@ -37,7 +37,7 @@ module LinkThumbnailer
37
37
  )
38
38
  img.source_url = img_url
39
39
  img
40
- rescue Exception
40
+ rescue StandardError
41
41
  nil
42
42
  end
43
43
 
@@ -10,7 +10,7 @@ module LinkThumbnailer
10
10
 
11
11
  object[:images] = []
12
12
  if object[:image]
13
- object[:images] << { :source_url => object[:image] }
13
+ object[:images] << { source_url: object[:image] }
14
14
  end
15
15
 
16
16
  object
@@ -12,7 +12,7 @@ module LinkThumbnailer
12
12
  end
13
13
 
14
14
  def self.install!
15
- ActionDispatch::Routing::Mapper.send :include, LinkThumbnailer::Rails::Routes::Helper
15
+ ActionDispatch::Routing::Mapper.send(:include, LinkThumbnailer::Rails::Routes::Helper)
16
16
  end
17
17
 
18
18
  attr_accessor :routes
@@ -23,7 +23,7 @@ module LinkThumbnailer
23
23
 
24
24
  def generate_routes!(options)
25
25
  @mapping = Mapper.new.map(&@options)
26
- routes.scope 'link', :as => 'link' do
26
+ routes.scope 'link', as: 'link' do
27
27
  map_route(:previews, :preview_routes)
28
28
  end
29
29
  end
@@ -37,8 +37,8 @@ module LinkThumbnailer
37
37
  end
38
38
 
39
39
  def preview_routes(mapping)
40
- routes.scope :controller => mapping[:controllers] do
41
- routes.match 'preview', :via => :post, :action => :create, :as => mapping[:as], :defaults => { :format => 'json' }
40
+ routes.scope controller: mapping[:controllers] do
41
+ routes.match 'preview', via: :post, action: :create, as: mapping[:as], defaults: { format: 'json' }
42
42
  end
43
43
  end
44
44
 
@@ -7,11 +7,11 @@ module LinkThumbnailer
7
7
 
8
8
  def initialize
9
9
  @controllers = {
10
- :previews => 'link_thumbnailer/previews'
10
+ previews: 'link_thumbnailer/previews'
11
11
  }
12
12
 
13
13
  @as = {
14
- :previews => :preview
14
+ previews: :preview
15
15
  }
16
16
 
17
17
  @skips = []
@@ -19,13 +19,13 @@ module LinkThumbnailer
19
19
 
20
20
  def [](routes)
21
21
  {
22
- :controllers => @controllers[routes],
23
- :as => @as[routes]
22
+ controllers: @controllers[routes],
23
+ as: @as[routes]
24
24
  }
25
25
  end
26
26
 
27
27
  def skipped?(controller)
28
- return @skips.include?(controller)
28
+ @skips.include?(controller)
29
29
  end
30
30
  end
31
31
  end
@@ -1,3 +1,3 @@
1
1
  module LinkThumbnailer
2
- VERSION = "1.0.5"
2
+ VERSION = "1.0.6"
3
3
  end
@@ -6,9 +6,9 @@ module LinkThumbnailer
6
6
  def to_hash
7
7
  result = {}
8
8
  LinkThumbnailer.configuration.rmagick_attributes.each {|m|
9
- k = m.to_sym
10
- result[k] = self.send(m) if self.respond_to?(m)
11
- result[k] = result[k].to_s if result[k].is_a?(URI)
9
+ k = m.to_sym
10
+ result[k] = self.send(m) if self.respond_to?(m)
11
+ result[k] = result[k].to_s if result[k].is_a?(URI)
12
12
  }
13
13
 
14
14
  result
@@ -2,6 +2,24 @@ require 'spec_helper'
2
2
 
3
3
  describe LinkThumbnailer::DocParser do
4
4
 
5
- it { should respond_to :parse }
5
+ it { should respond_to(:parse).with(1).arguments }
6
+
7
+ let(:instance) { LinkThumbnailer::DocParser.new }
8
+
9
+ describe "#parse" do
10
+
11
+ let(:source_url) { 'http://foo.com' }
12
+
13
+ subject { instance.parse('', source_url) }
14
+
15
+ it { expect(subject.source_url).to eq(source_url) }
16
+ it { expect(subject).to respond_to(:doc_base_href) }
17
+ it { expect(subject).to respond_to(:img_srcs) }
18
+ it { expect(subject).to respond_to(:img_abs_urls) }
19
+ it { expect(subject).to respond_to(:title) }
20
+ it { expect(subject).to respond_to(:description) }
21
+ it { expect(subject).to respond_to(:source_url) }
22
+
23
+ end
6
24
 
7
25
  end
data/spec/fetcher_spec.rb CHANGED
@@ -17,14 +17,14 @@ describe LinkThumbnailer::Fetcher do
17
17
 
18
18
  context "when redirect_count is more than config" do
19
19
 
20
- it { lambda { fetcher.fetch(url, 10) }.should raise_exception(ArgumentError) }
20
+ it { expect { fetcher.fetch(url, 10) }.to raise_exception(ArgumentError) }
21
21
 
22
22
  end
23
23
 
24
24
  context "when no http error" do
25
25
 
26
26
  before do
27
- stub_request(:get, url).to_return(:status => 200, :body => 'foo', :headers => {})
27
+ stub_request(:get, url).to_return(status: 200, body: 'foo', headers: {})
28
28
  end
29
29
 
30
30
  it "returns body response" do
@@ -43,8 +43,8 @@ describe LinkThumbnailer::Fetcher do
43
43
  let(:another_url) { 'http://bar.com' }
44
44
 
45
45
  before do
46
- stub_request(:get, url).to_return(:status => 300, :body => 'foo', :headers => { 'Location' => another_url})
47
- stub_request(:get, another_url).to_return(:status => 200, :body => 'bar', :headers => {})
46
+ stub_request(:get, url).to_return(status: 300, body: 'foo', headers: { 'Location' => another_url })
47
+ stub_request(:get, another_url).to_return(status: 200, body: 'bar', headers: {})
48
48
  end
49
49
 
50
50
  it "returns body response" do
@@ -61,10 +61,10 @@ describe LinkThumbnailer::Fetcher do
61
61
  context "when http error" do
62
62
 
63
63
  before do
64
- stub_request(:get, url).to_return(:status => 500, :body => 'foo', :headers => {})
64
+ stub_request(:get, url).to_return(status: 500, body: 'foo', headers: {})
65
65
  end
66
66
 
67
- it { lambda { fetcher.fetch(url) }.should raise_exception(Net::HTTPFatalError) }
67
+ it { expect { fetcher.fetch(url) }.to raise_exception(Net::HTTPFatalError) }
68
68
 
69
69
  end
70
70
 
@@ -16,13 +16,13 @@ describe LinkThumbnailer::ImgUrlFilter do
16
16
 
17
17
  context "when img_url does not contain any blacklisted urls" do
18
18
 
19
- it { img_url_filter.reject?('http://valid.com/foo/bar.png').should be_false }
19
+ it { expect(img_url_filter.reject?('http://valid.com/foo/bar.png')).to be_false }
20
20
 
21
21
  end
22
22
 
23
23
  context "when img_url does contain any blacklisted urls" do
24
24
 
25
- it { img_url_filter.reject?('http://not_valid.net/foo/bar.png').should be_true }
25
+ it { expect(img_url_filter.reject?('http://not_valid.net/foo/bar.png')).to be_true }
26
26
 
27
27
  end
28
28
 
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe LinkThumbnailer::Opengraph do
4
4
 
5
- it { LinkThumbnailer::Opengraph.should respond_to :parse }
5
+ it { LinkThumbnailer::Opengraph.should respond_to(:parse).with(2).arguments }
6
6
 
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: link_thumbnailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
12
+ date: 2013-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -142,7 +142,7 @@ files:
142
142
  - lib/generators/link_thumbnailer/install_generator.rb
143
143
  - lib/generators/templates/initializer.rb
144
144
  - lib/link_thumbnailer.rb
145
- - lib/link_thumbnailer/config.rb
145
+ - lib/link_thumbnailer/configuration.rb
146
146
  - lib/link_thumbnailer/doc.rb
147
147
  - lib/link_thumbnailer/doc_parser.rb
148
148
  - lib/link_thumbnailer/engine.rb