-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use frozen string literal to reduce number of string allocation
- Loading branch information
Showing
76 changed files
with
398 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,71 @@ | ||
module LinkThumbnailer | ||
|
||
# Access point for the gem configurations. | ||
# | ||
# @return [LinkThumbnailer::Configuration] a configuration instance. | ||
def self.config | ||
@config ||= Configuration.new | ||
end | ||
|
||
# Configure hook used in the gem initializer. Convinient way to set all the | ||
# gem configurations. | ||
# | ||
# @example inside config/initializers/link_thumbnaler.rb | ||
# LinkThumbnailer.configure do |config| | ||
# config.user_agent = 'link_thumbnailer' | ||
# end | ||
# | ||
# @return [void] | ||
def self.configure | ||
yield config if block_given? | ||
end | ||
|
||
class Configuration | ||
|
||
attr_accessor :redirect_limit, :blacklist_urls, :user_agent, | ||
:verify_ssl, :http_open_timeout, :http_read_timeout, :attributes, | ||
:graders, :description_min_length, :positive_regex, :negative_regex, | ||
:image_limit, :image_stats, :raise_on_invalid_format, :max_concurrency, | ||
:scrapers, :http_override_headers, :encoding | ||
|
||
alias_method :http_timeout, :http_open_timeout | ||
alias_method :http_timeout=, :http_open_timeout= | ||
|
||
# Create a new instance. | ||
# | ||
# @return [LinkThumbnailer::Configuration] | ||
def initialize | ||
@redirect_limit = 3 | ||
@user_agent = 'link_thumbnailer' | ||
@verify_ssl = true | ||
@http_open_timeout = 5 | ||
@http_read_timeout = 5 | ||
@blacklist_urls = [ | ||
%r{^http://ad\.doubleclick\.net/}, | ||
%r{^http://b\.scorecardresearch\.com/}, | ||
%r{^http://pixel\.quantserve\.com/}, | ||
%r{^http://s7\.addthis\.com/} | ||
] | ||
@attributes = [:title, :images, :description, :videos, :favicon] | ||
@graders = [ | ||
->(description) { ::LinkThumbnailer::Graders::Length.new(description) }, | ||
->(description) { ::LinkThumbnailer::Graders::HtmlAttribute.new(description, :class) }, | ||
->(description) { ::LinkThumbnailer::Graders::HtmlAttribute.new(description, :id) }, | ||
->(description) { ::LinkThumbnailer::Graders::Position.new(description, weigth: 3) }, | ||
->(description) { ::LinkThumbnailer::Graders::LinkDensity.new(description) }, | ||
] | ||
@description_min_length = 50 | ||
@positive_regex = /article|body|content|entry|hentry|main|page|pagination|post|text|blog|story/i | ||
@negative_regex = /combx|comment|com-|contact|foot|footer|footnote|masthead|media|meta|outbrain|promo|related|scroll|shoutbox|sidebar|sponsor|shopping|tags|tool|widget|modal/i | ||
@image_limit = 5 | ||
@image_stats = true | ||
@raise_on_invalid_format = false | ||
@max_concurrency = 20 | ||
@scrapers = [:opengraph, :default] | ||
@http_override_headers = { 'Accept-Encoding' => 'none' } | ||
@encoding = 'utf-8' | ||
end | ||
|
||
end | ||
end | ||
# frozen_string_literal: true | ||
module LinkThumbnailer | ||
|
||
# Access point for the gem configurations. | ||
# | ||
# @return [LinkThumbnailer::Configuration] a configuration instance. | ||
def self.config | ||
@config ||= Configuration.new | ||
end | ||
|
||
# Configure hook used in the gem initializer. Convinient way to set all the | ||
# gem configurations. | ||
# | ||
# @example inside config/initializers/link_thumbnaler.rb | ||
# LinkThumbnailer.configure do |config| | ||
# config.user_agent = 'link_thumbnailer' | ||
# end | ||
# | ||
# @return [void] | ||
def self.configure | ||
yield config if block_given? | ||
end | ||
|
||
class Configuration | ||
|
||
attr_accessor :redirect_limit, :blacklist_urls, :user_agent, | ||
:verify_ssl, :http_open_timeout, :http_read_timeout, :attributes, | ||
:graders, :description_min_length, :positive_regex, :negative_regex, | ||
:image_limit, :image_stats, :raise_on_invalid_format, :max_concurrency, | ||
:scrapers, :http_override_headers, :encoding | ||
|
||
alias_method :http_timeout, :http_open_timeout | ||
alias_method :http_timeout=, :http_open_timeout= | ||
|
||
# Create a new instance. | ||
# | ||
# @return [LinkThumbnailer::Configuration] | ||
def initialize | ||
@redirect_limit = 3 | ||
@user_agent = 'link_thumbnailer' | ||
@verify_ssl = true | ||
@http_open_timeout = 5 | ||
@http_read_timeout = 5 | ||
@blacklist_urls = [ | ||
%r{^http://ad\.doubleclick\.net/}, | ||
%r{^http://b\.scorecardresearch\.com/}, | ||
%r{^http://pixel\.quantserve\.com/}, | ||
%r{^http://s7\.addthis\.com/} | ||
] | ||
@attributes = [:title, :images, :description, :videos, :favicon] | ||
@graders = [ | ||
->(description) { ::LinkThumbnailer::Graders::Length.new(description) }, | ||
->(description) { ::LinkThumbnailer::Graders::HtmlAttribute.new(description, :class) }, | ||
->(description) { ::LinkThumbnailer::Graders::HtmlAttribute.new(description, :id) }, | ||
->(description) { ::LinkThumbnailer::Graders::Position.new(description, weigth: 3) }, | ||
->(description) { ::LinkThumbnailer::Graders::LinkDensity.new(description) }, | ||
] | ||
@description_min_length = 50 | ||
@positive_regex = /article|body|content|entry|hentry|main|page|pagination|post|text|blog|story/i | ||
@negative_regex = /combx|comment|com-|contact|foot|footer|footnote|masthead|media|meta|outbrain|promo|related|scroll|shoutbox|sidebar|sponsor|shopping|tags|tool|widget|modal/i | ||
@image_limit = 5 | ||
@image_stats = true | ||
@raise_on_invalid_format = false | ||
@max_concurrency = 20 | ||
@scrapers = [:opengraph, :default] | ||
@http_override_headers = { 'Accept-Encoding' => 'none' } | ||
@encoding = 'utf-8' | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'delegate' | ||
|
||
module LinkThumbnailer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
module LinkThumbnailer | ||
module ImageComparators | ||
class Base | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'image_info' | ||
|
||
module LinkThumbnailer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'delegate' | ||
|
||
module LinkThumbnailer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
module LinkThumbnailer | ||
class Model | ||
|
||
def to_json(*args) | ||
as_json.to_json(*args) | ||
end | ||
|
||
private | ||
|
||
def sanitize(str) | ||
return unless str | ||
|
||
str.encode!("UTF-16", "UTF-8", invalid: :replace, undef: :replace, replace: "") | ||
str.encode!("UTF-8", "UTF-16").strip.gsub(/[\r\n\f]+/, "\n") | ||
end | ||
end | ||
end | ||
# frozen_string_literal: true | ||
module LinkThumbnailer | ||
class Model | ||
|
||
def to_json(*args) | ||
as_json.to_json(*args) | ||
end | ||
|
||
private | ||
|
||
def sanitize(str) | ||
return unless str | ||
|
||
str = str.dup | ||
str.encode!("UTF-16", "UTF-8", invalid: :replace, undef: :replace, replace: "") | ||
str.encode!("UTF-8", "UTF-16").strip.gsub(/[\r\n\f]+/, "\n") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'link_thumbnailer/model' | ||
require 'link_thumbnailer/grader' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'link_thumbnailer/model' | ||
|
||
module LinkThumbnailer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,55 @@ | ||
require 'link_thumbnailer/model' | ||
require 'link_thumbnailer/image_parser' | ||
require 'link_thumbnailer/image_comparator' | ||
require 'link_thumbnailer/image_validator' | ||
|
||
module LinkThumbnailer | ||
module Models | ||
class Image < ::LinkThumbnailer::Model | ||
|
||
attr_reader :src, :type, :size | ||
|
||
def initialize(src, size = nil, type = nil) | ||
@src = src | ||
@size = size || parser.size | ||
@type = type || parser.type | ||
end | ||
|
||
def to_s | ||
src.to_s | ||
end | ||
|
||
def <=>(other) | ||
comparator.call(other) | ||
end | ||
|
||
def valid? | ||
validator.call | ||
end | ||
|
||
def as_json(*) | ||
{ | ||
src: src.to_s, | ||
size: size, | ||
type: type | ||
} | ||
end | ||
|
||
private | ||
|
||
def parser | ||
@parser ||= ::LinkThumbnailer::ImageParser.new(src) | ||
end | ||
|
||
def validator | ||
::LinkThumbnailer::ImageValidator.new(self) | ||
end | ||
|
||
def comparator | ||
::LinkThumbnailer::ImageComparator.new(self) | ||
end | ||
|
||
end | ||
end | ||
end | ||
# frozen_string_literal: true | ||
require 'link_thumbnailer/model' | ||
require 'link_thumbnailer/image_parser' | ||
require 'link_thumbnailer/image_comparator' | ||
require 'link_thumbnailer/image_validator' | ||
|
||
module LinkThumbnailer | ||
module Models | ||
class Image < ::LinkThumbnailer::Model | ||
|
||
attr_reader :src, :type, :size | ||
|
||
def initialize(src, size = nil, type = nil) | ||
@src = src | ||
@size = size || parser.size | ||
@type = type || parser.type | ||
end | ||
|
||
def to_s | ||
src.to_s | ||
end | ||
|
||
def <=>(other) | ||
comparator.call(other) | ||
end | ||
|
||
def valid? | ||
validator.call | ||
end | ||
|
||
def as_json(*) | ||
{ | ||
src: src.to_s, | ||
size: size, | ||
type: type | ||
} | ||
end | ||
|
||
private | ||
|
||
def parser | ||
@parser ||= ::LinkThumbnailer::ImageParser.new(src) | ||
end | ||
|
||
def validator | ||
::LinkThumbnailer::ImageValidator.new(self) | ||
end | ||
|
||
def comparator | ||
::LinkThumbnailer::ImageComparator.new(self) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'link_thumbnailer/model' | ||
|
||
module LinkThumbnailer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'link_thumbnailer/model' | ||
require 'link_thumbnailer/video_parser' | ||
|
||
|
Oops, something went wrong.