Skip to content

Commit

Permalink
Use frozen string literal to reduce number of string allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
tachyons committed Mar 21, 2018
1 parent 7e8ad71 commit 809b1b9
Show file tree
Hide file tree
Showing 76 changed files with 398 additions and 321 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'

# Specify your gem's dependencies in link_thumbnailer.gemspec
Expand Down
1 change: 1 addition & 0 deletions Rakefile
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'

Expand Down
1 change: 1 addition & 0 deletions lib/generators/link_thumbnailer/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module Generators
class InstallGenerator < ::Rails::Generators::Base
Expand Down
1 change: 1 addition & 0 deletions lib/generators/templates/initializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# Use this hook to configure LinkThumbnailer bahaviors.
LinkThumbnailer.configure do |config|
# Numbers of redirects before raising an exception when trying to parse given url.
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'json'
require 'link_thumbnailer/version'
require 'link_thumbnailer/configuration'
Expand Down
141 changes: 71 additions & 70 deletions lib/link_thumbnailer/configuration.rb
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
1 change: 1 addition & 0 deletions lib/link_thumbnailer/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
Exceptions = Class.new(StandardError)
RedirectLimit = Class.new(Exceptions)
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/grader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'delegate'
require 'link_thumbnailer/graders/base'
require 'link_thumbnailer/graders/length'
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/graders/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'delegate'

module LinkThumbnailer
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/graders/html_attribute.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module Graders
class HtmlAttribute < ::LinkThumbnailer::Graders::Base
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/graders/length.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module Graders
class Length < ::LinkThumbnailer::Graders::Base
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/graders/link_density.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module Graders
class LinkDensity < ::LinkThumbnailer::Graders::Base
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/graders/position.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module Graders
class Position < ::LinkThumbnailer::Graders::Base
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/image_comparator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'link_thumbnailer/image_comparators/base'
require 'link_thumbnailer/image_comparators/size'

Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/image_comparators/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module ImageComparators
class Base
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/image_comparators/size.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module LinkThumbnailer
module ImageComparators
class Size < ::LinkThumbnailer::ImageComparators::Base
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/image_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'image_info'

module LinkThumbnailer
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/image_validator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'delegate'

module LinkThumbnailer
Expand Down
36 changes: 19 additions & 17 deletions lib/link_thumbnailer/model.rb
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
1 change: 1 addition & 0 deletions lib/link_thumbnailer/models/description.rb
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'

Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/models/favicon.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'link_thumbnailer/model'

module LinkThumbnailer
Expand Down
109 changes: 55 additions & 54 deletions lib/link_thumbnailer/models/image.rb
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
1 change: 1 addition & 0 deletions lib/link_thumbnailer/models/title.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'link_thumbnailer/model'

module LinkThumbnailer
Expand Down
1 change: 1 addition & 0 deletions lib/link_thumbnailer/models/video.rb
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'

Expand Down
Loading

0 comments on commit 809b1b9

Please sign in to comment.