link_thumbnailer 1.1.1 → 1.1.2

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.
@@ -1,19 +1,19 @@
1
- module LinkThumbnailer
2
- module WebImage
3
-
4
- attr_accessor :doc
5
-
6
- def source_url
7
- instance_variable_get(:@uri)
8
- end
9
-
10
- def to_hash
11
- LinkThumbnailer.configuration.image_attributes.each_with_object({}) { |m, result|
12
- k = m.to_sym
13
- result[k] = self.send(m) if self.respond_to?(m)
14
- result[k] = result[k].to_s if result[k].is_a?(URI) || result[k].respond_to?(:to_str)
15
- }
16
- end
17
-
18
- end
19
- end
1
+ module LinkThumbnailer
2
+ module WebImage
3
+
4
+ attr_accessor :doc
5
+
6
+ def source_url
7
+ instance_variable_get(:@uri)
8
+ end
9
+
10
+ def to_hash
11
+ LinkThumbnailer.configuration.image_attributes.each_with_object({}) { |m, result|
12
+ k = m.to_sym
13
+ result[k] = self.send(m) if self.respond_to?(m)
14
+ result[k] = result[k].to_s if result[k].is_a?(URI) || result[k].respond_to?(:to_str)
15
+ }
16
+ end
17
+
18
+ end
19
+ end
@@ -1,119 +1,119 @@
1
- require 'link_thumbnailer/configuration'
2
- require 'link_thumbnailer/object'
3
- require 'link_thumbnailer/fetcher'
4
- require 'link_thumbnailer/doc_parser'
5
- require 'link_thumbnailer/doc'
6
- require 'link_thumbnailer/img_url_filter'
7
- require 'link_thumbnailer/img_parser'
8
- require 'link_thumbnailer/img_comparator'
9
- require 'link_thumbnailer/web_image'
10
- require 'link_thumbnailer/opengraph'
11
- require 'link_thumbnailer/version'
12
-
13
- module LinkThumbnailer
14
-
15
- module Rails
16
- autoload :Routes, 'link_thumbnailer/rails/routes'
17
- end
18
-
19
- class << self
20
-
21
- attr_accessor :configuration, :object, :fetcher, :doc_parser,
22
- :img_url_filters, :img_parser, :logger
23
-
24
- def logger
25
- @logger ||= ::Rails.logger
26
- end
27
-
28
- def config
29
- self.configuration ||= Configuration.new(
30
- mandatory_attributes: %w(url title images),
31
- strict: true,
32
- redirect_limit: 3,
33
- blacklist_urls: [
34
- %r{^http://ad\.doubleclick\.net/},
35
- %r{^http://b\.scorecardresearch\.com/},
36
- %r{^http://pixel\.quantserve\.com/},
37
- %r{^http://s7\.addthis\.com/}
38
- ],
39
- image_attributes: %w(source_url size type),
40
- limit: 10,
41
- top: 5,
42
- user_agent: 'linkthumbnailer',
43
- verify_ssl: true,
44
- http_timeout: 5
45
- )
46
- end
47
-
48
- def configure
49
- yield config
50
- end
51
-
52
- def generate(url, options = {})
53
- set_options(options)
54
- instantiate_classes
55
-
56
- doc = doc_parser.parse(fetcher.fetch(url), url)
57
-
58
- self.object[:url] = fetcher.url.to_s
59
- opengraph(doc) || custom(doc)
60
- end
61
-
62
- private
63
-
64
- def set_options(options)
65
- config
66
- options.each {|k, v| config[k] = v }
67
- end
68
-
69
- def instantiate_classes
70
- self.object = LinkThumbnailer::Object.new
71
- self.fetcher = LinkThumbnailer::Fetcher.new
72
- self.doc_parser = LinkThumbnailer::DocParser.new
73
- self.img_url_filters = [LinkThumbnailer::ImgUrlFilter.new]
74
- self.img_parser = LinkThumbnailer::ImgParser.new(fetcher, img_url_filters)
75
- end
76
-
77
- def opengraph(doc)
78
- return unless opengraph?(doc)
79
- self.object = LinkThumbnailer::Opengraph.parse(object, doc)
80
- return object if object.valid?
81
- nil
82
- end
83
-
84
- def custom(doc)
85
- self.object[:title] = doc.title
86
- self.object[:description] = doc.description
87
- self.object[:images] = img_parser.parse(doc.img_abs_urls.dup)
88
- self.object[:url] = doc.canonical_url || object[:url]
89
- return object if object.valid?
90
- nil
91
- end
92
-
93
- def opengraph?(doc)
94
- !doc.xpath('//meta[starts-with(@property, "og:") and @content]').empty?
95
- end
96
-
97
- end
98
-
99
- end
100
-
101
- begin
102
- require 'rails'
103
- rescue LoadError
104
- end
105
-
106
- $stderr.puts <<-EOC if !defined?(Rails)
107
- warning: no framework detected.
108
-
109
- Your Gemfile might not be configured properly.
110
- ---- e.g. ----
111
- Rails:
112
- gem 'link_thumbnailer'
113
-
114
- EOC
115
-
116
- if defined?(Rails)
117
- require 'link_thumbnailer/engine'
118
- require 'link_thumbnailer/railtie'
119
- end
1
+ require 'link_thumbnailer/configuration'
2
+ require 'link_thumbnailer/object'
3
+ require 'link_thumbnailer/fetcher'
4
+ require 'link_thumbnailer/doc_parser'
5
+ require 'link_thumbnailer/doc'
6
+ require 'link_thumbnailer/img_url_filter'
7
+ require 'link_thumbnailer/img_parser'
8
+ require 'link_thumbnailer/img_comparator'
9
+ require 'link_thumbnailer/web_image'
10
+ require 'link_thumbnailer/opengraph'
11
+ require 'link_thumbnailer/version'
12
+
13
+ module LinkThumbnailer
14
+
15
+ module Rails
16
+ autoload :Routes, 'link_thumbnailer/rails/routes'
17
+ end
18
+
19
+ class << self
20
+
21
+ attr_accessor :configuration, :object, :fetcher, :doc_parser,
22
+ :img_url_filters, :img_parser, :logger
23
+
24
+ def logger
25
+ @logger ||= ::Rails.logger
26
+ end
27
+
28
+ def config
29
+ self.configuration ||= Configuration.new(
30
+ mandatory_attributes: %w(url title images),
31
+ strict: true,
32
+ redirect_limit: 3,
33
+ blacklist_urls: [
34
+ %r{^http://ad\.doubleclick\.net/},
35
+ %r{^http://b\.scorecardresearch\.com/},
36
+ %r{^http://pixel\.quantserve\.com/},
37
+ %r{^http://s7\.addthis\.com/}
38
+ ],
39
+ image_attributes: %w(source_url size type),
40
+ limit: 10,
41
+ top: 5,
42
+ user_agent: 'linkthumbnailer',
43
+ verify_ssl: true,
44
+ http_timeout: 5
45
+ )
46
+ end
47
+
48
+ def configure
49
+ yield config
50
+ end
51
+
52
+ def generate(url, options = {})
53
+ set_options(options)
54
+ instantiate_classes
55
+
56
+ doc = doc_parser.parse(fetcher.fetch(url), url)
57
+
58
+ self.object[:url] = fetcher.url.to_s
59
+ opengraph(doc) || custom(doc)
60
+ end
61
+
62
+ private
63
+
64
+ def set_options(options)
65
+ config
66
+ options.each {|k, v| config[k] = v }
67
+ end
68
+
69
+ def instantiate_classes
70
+ self.object = LinkThumbnailer::Object.new
71
+ self.fetcher = LinkThumbnailer::Fetcher.new
72
+ self.doc_parser = LinkThumbnailer::DocParser.new
73
+ self.img_url_filters = [LinkThumbnailer::ImgUrlFilter.new]
74
+ self.img_parser = LinkThumbnailer::ImgParser.new(fetcher, img_url_filters)
75
+ end
76
+
77
+ def opengraph(doc)
78
+ return unless opengraph?(doc)
79
+ self.object = LinkThumbnailer::Opengraph.parse(object, doc)
80
+ return object if object.valid?
81
+ nil
82
+ end
83
+
84
+ def custom(doc)
85
+ self.object[:title] = doc.title
86
+ self.object[:description] = doc.description
87
+ self.object[:images] = img_parser.parse(doc.img_abs_urls.dup)
88
+ self.object[:url] = doc.canonical_url || object[:url]
89
+ return object if object.valid?
90
+ nil
91
+ end
92
+
93
+ def opengraph?(doc)
94
+ !doc.xpath('//meta[starts-with(@property, "og:") and @content]').empty?
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ begin
102
+ require 'rails'
103
+ rescue LoadError
104
+ end
105
+
106
+ $stderr.puts <<-EOC if !defined?(Rails)
107
+ warning: no framework detected.
108
+
109
+ Your Gemfile might not be configured properly.
110
+ ---- e.g. ----
111
+ Rails:
112
+ gem 'link_thumbnailer'
113
+
114
+ EOC
115
+
116
+ if defined?(Rails)
117
+ require 'link_thumbnailer/engine'
118
+ require 'link_thumbnailer/railtie'
119
+ end
@@ -1,28 +1,28 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/link_thumbnailer/version', __FILE__)
3
-
4
- Gem::Specification.new do |gem|
5
- gem.authors = ["Pierre-Louis Gottfrois"]
6
- gem.email = ["pierrelouis.gottfrois@gmail.com"]
7
- gem.description = %q{Ruby gem generating thumbnail images from a given URL.}
8
- gem.summary = %q{Ruby gem ranking images from a given URL returning an object containing images and website informations.}
9
- gem.homepage = "https://github.com/gottfrois/link_thumbnailer"
10
-
11
- gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "link_thumbnailer"
15
- gem.require_paths = ["lib"]
16
- gem.version = LinkThumbnailer::VERSION
17
-
18
- gem.add_dependency 'rake', '>= 0.9'
19
- gem.add_dependency 'nokogiri', '>= 1.5.5', '< 1.7'
20
- gem.add_dependency 'hashie', '>= 1.2.0'
21
- gem.add_dependency 'net-http-persistent', '~> 2.7'
22
- gem.add_dependency 'fastimage', '~> 1.5.5'
23
- gem.add_dependency 'json', '>= 1.7.6', '< 1.9'
24
-
25
- gem.add_development_dependency 'bundler', '~> 1.3'
26
- gem.add_development_dependency 'rspec', '~> 2.14'
27
- gem.add_development_dependency 'pry', '~> 0.9'
28
- end
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/link_thumbnailer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Pierre-Louis Gottfrois"]
6
+ gem.email = ["pierrelouis.gottfrois@gmail.com"]
7
+ gem.description = %q{Ruby gem generating thumbnail images from a given URL.}
8
+ gem.summary = %q{Ruby gem ranking images from a given URL returning an object containing images and website informations.}
9
+ gem.homepage = "https://github.com/gottfrois/link_thumbnailer"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "link_thumbnailer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = LinkThumbnailer::VERSION
17
+
18
+ gem.add_dependency 'rake', '>= 0.9'
19
+ gem.add_dependency 'nokogiri', '>= 1.5.5', '< 1.7'
20
+ gem.add_dependency 'hashie', '>= 1.2.0'
21
+ gem.add_dependency 'net-http-persistent', '~> 2.7'
22
+ gem.add_dependency 'fastimage', '~> 1.5.5'
23
+ gem.add_dependency 'json', '>= 1.7.6', '< 1.9'
24
+
25
+ gem.add_development_dependency 'bundler', '~> 1.3'
26
+ gem.add_development_dependency 'rspec', '~> 2.14'
27
+ gem.add_development_dependency 'pry', '~> 0.9'
28
+ end