Skip to content

Commit

Permalink
feat: use zeitwerk for autoloading (#47)
Browse files Browse the repository at this point in the history
* feat: use zeitwerk for autoloading

* fix memoizing of postprocessor/extractor
  • Loading branch information
gildesmarais committed Oct 13, 2019
1 parent f4e2634 commit bce523d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 44 deletions.
28 changes: 15 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PATH
nokogiri (>= 1.10, < 2.0)
reverse_markdown (~> 1.3)
sanitize (~> 5.0)
zeitwerk

GEM
remote: https://rubygems.org/
Expand All @@ -26,7 +27,7 @@ GEM
crass (1.0.4)
diff-lcs (1.3)
docile (1.3.2)
faraday (0.16.2)
faraday (0.17.0)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.13.1)
faraday (>= 0.7.4, < 1.0)
Expand All @@ -42,25 +43,25 @@ GEM
mini_portile2 (~> 2.4.0)
nokogumbo (2.0.1)
nokogiri (~> 1.8, >= 1.8.4)
parallel (1.17.0)
parallel (1.18.0)
parser (2.6.5.0)
ast (~> 2.4.0)
rainbow (3.0.0)
reverse_markdown (1.3.0)
nokogiri
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.2)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.0)
rspec-support (~> 3.9.0)
rspec-expectations (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.2)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.3)
rspec-support (~> 3.9.0)
rspec-support (3.9.0)
rubocop (0.75.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
Expand Down Expand Up @@ -88,6 +89,7 @@ GEM
unicode-display_width (1.6.0)
vcr (5.0.0)
yard (0.9.20)
zeitwerk (2.2.0)

PLATFORMS
ruby
Expand Down
3 changes: 2 additions & 1 deletion html2rss.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
spec.description = 'Give the URL to scrape and some CSS selectors. Get a RSS::Rss instance in return.'
spec.homepage = 'https://github.com/gildesmarais/html2rss'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.4.0'
spec.required_ruby_version = '>= 2.4.4'

if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
Expand All @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'nokogiri', '>= 1.10', '< 2.0'
spec.add_dependency 'reverse_markdown', '~> 1.3'
spec.add_dependency 'sanitize', '~> 5.0'
spec.add_dependency 'zeitwerk'
spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'byebug'
spec.add_development_dependency 'rspec', '~> 3.0'
Expand Down
9 changes: 5 additions & 4 deletions lib/html2rss.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'html2rss/config'
require 'html2rss/feed_builder'
require 'html2rss/version'
require 'html2rss/utils'
require 'zeitwerk'

loader = Zeitwerk::Loader.for_gem
loader.setup

require 'yaml'

##
Expand Down
16 changes: 6 additions & 10 deletions lib/html2rss/attribute_post_processors.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
require_relative 'attribute_post_processors/html_to_markdown'
require_relative 'attribute_post_processors/parse_time'
require_relative 'attribute_post_processors/parse_uri'
require_relative 'attribute_post_processors/sanitize_html'
require_relative 'attribute_post_processors/substring'
require_relative 'attribute_post_processors/template'

module Html2rss
##
# Provides a namespace for attribute post processors.
module AttributePostProcessors
def self.get_processor(name)
camel_cased_name = name.split('_').map(&:capitalize).join
class_name = ['Html2rss', 'AttributePostProcessors', camel_cased_name].join('::')
@get_processor ||= Hash.new do |processors, key|
camel_cased_name = key.split('_').map(&:capitalize).join
class_name = ['Html2rss', 'AttributePostProcessors', camel_cased_name].join('::')
processors[key] = Object.const_get(class_name)
end

Object.const_get(class_name)
@get_processor[name]
end
end
end
1 change: 0 additions & 1 deletion lib/html2rss/feed_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'rss'
require_relative 'item'

module Html2rss
##
Expand Down
3 changes: 0 additions & 3 deletions lib/html2rss/item.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
require 'faraday'
require 'faraday_middleware'
require 'open-uri'
require 'nokogiri'
require_relative 'item_extractors'
require_relative 'attribute_post_processors'

module Html2rss
##
Expand Down
18 changes: 6 additions & 12 deletions lib/html2rss/item_extractors.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
require_relative 'item_extractors/attribute'
require_relative 'item_extractors/current_time'
require_relative 'item_extractors/href'
require_relative 'item_extractors/html'
require_relative 'item_extractors/static'
require_relative 'item_extractors/text'

module Html2rss
##
# Provides a namespace for item extractors.
module ItemExtractors
DEFAULT = 'text'.freeze
DEFAULT = 'Text'.freeze

def self.get_extractor(name)
@extractors = Hash.new do |hash, key|
camel_cased_name = key.split('_').map(&:capitalize).join
@get_extractor ||= Hash.new do |extractors, key|
camel_cased_name = (key || DEFAULT).split('_').map(&:capitalize).join
class_name = ['Html2rss', 'ItemExtractors', camel_cased_name].join('::')
extractors[key] = Object.const_get(class_name)
end

hash[key] = Object.const_get(class_name)
end[name || DEFAULT]
@get_extractor[name]
end

##
Expand Down

0 comments on commit bce523d

Please sign in to comment.