Skip to content

Commit dafc0ae

Browse files
committed
feat(deps): use html2rss in latest development status
Signed-off-by: Gil Desmarais <git@desmarais.de>
1 parent c1b1d03 commit dafc0ae

11 files changed

Lines changed: 225 additions & 122 deletions

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ source 'https://rubygems.org'
44

55
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
66

7-
gem 'html2rss', '~> 0.14'
7+
# gem 'html2rss', '~> 0.14'
8+
gem 'html2rss', github: 'html2rss/html2rss'
9+
810
gem 'html2rss-configs', github: 'html2rss/html2rss-configs'
911

1012
# Use these instead of the two above (uncomment them) when developing locally:

Gemfile.lock

Lines changed: 142 additions & 85 deletions
Large diffs are not rendered by default.

app.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,38 @@ def self.development? = ENV['RACK_ENV'] == 'development'
8484
end
8585

8686
r.on String, String do |folder_name, config_name_with_ext|
87-
handle_html2rss_configs(request, folder_name, config_name_with_ext)
87+
response['Content-Type'] = CONTENT_TYPE_RSS
88+
89+
name = "#{folder_name}/#{File.basename(config_name_with_ext, '.*')}"
90+
config = Html2rss::Configs.find_by_name(name)
91+
92+
if (params = request.params).any?
93+
config[:params] ||= {}
94+
config[:params].merge!(params)
95+
end
96+
97+
feed = Html2rss.feed(config)
98+
99+
HttpCache.expires(response, feed.channel.ttl.to_i * 60, cache_control: 'public')
100+
101+
feed.to_xml
88102
end
89103

90104
r.on String do |config_name_with_ext|
91-
handle_local_config_feeds(request, config_name_with_ext)
105+
response['Content-Type'] = CONTENT_TYPE_RSS
106+
107+
config = LocalConfig.find(File.basename(config_name_with_ext, '.*'))
108+
109+
if (params = request.params).any?
110+
config[:params] ||= {}
111+
config[:params].merge!(params)
112+
end
113+
114+
feed = Html2rss.feed(config)
115+
116+
HttpCache.expires(response, feed.channel.ttl.to_i * 60, cache_control: 'public')
117+
118+
feed.to_xml
92119
end
93120
end
94121

app/html2rss_facade.rb

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,47 @@ class Html2rssFacade
1414

1515
attr_reader :feed_config, :typecast_params
1616

17-
##
18-
# @param name [String] the name of a html2rss-configs provided config.
19-
# @param typecast_params [Object]
20-
# @return [String] the serialized RSS feed
21-
def self.from_config(name, typecast_params, &)
22-
feed_config = Html2rss::Configs.find_by_name(name)
23-
new(feed_config, typecast_params).feed(&)
24-
end
17+
class << self
18+
##
19+
# @param name [String] the name of a html2rss-configs provided config.
20+
# @param typecast_params [Object]
21+
# @return [String] the serialized RSS feed
22+
def from_config(name, typecast_params, &)
23+
feed_config = Html2rss::Configs.find_by_name(name)
24+
new(feed_config, typecast_params).feed(&)
25+
end
2526

26-
##
27-
# @param name [String] the name of a feed in the file `config/feeds.yml`
28-
# @param typecast_params [Object]
29-
# @return [String] the serialized RSS feed
30-
def self.from_local_config(name, typecast_params, &)
31-
feed_config = LocalConfig.find(name)
32-
new(feed_config, typecast_params).feed(&)
33-
end
27+
##
28+
# @param name [String] the name of a feed in the file `config/feeds.yml`
29+
# @param typecast_params [Object]
30+
# @return [String] the serialized RSS feed
31+
def from_local_config(name, typecast_params, &)
32+
feed_config = LocalConfig.find(name)
33+
new(feed_config, typecast_params).feed(&)
34+
end
3435

35-
##
36-
# @param feed_config [Hash<Symbol, Object>]
37-
# @param typecast_params [Object]
38-
# @param global_config [Hash<Symbol, Object>]
39-
# @return [Html2rss::Config]
40-
# @raise [Roda::RodaPlugins::TypecastParams::Error]
41-
def self.feed_config_to_config(feed_config, typecast_params, global_config: LocalConfig.global)
42-
dynamic_params = Html2rss::Config::Channel.required_params_for_config(feed_config[:channel])
43-
.to_h { |name| [name, typecast_params.str!(name)] }
44-
Html2rss::Config.new(feed_config, global_config, dynamic_params)
36+
##
37+
# @param feed_config [Hash<Symbol, Object>]
38+
# @param typecast_params [Object]
39+
# @param global_config [Hash<Symbol, Object>]
40+
# @return [Html2rss::Config]
41+
# @raise [Roda::RodaPlugins::TypecastParams::Error]
42+
def feed_config_to_config(feed_config, typecast_params, global_config: LocalConfig.global)
43+
params = required_params_for_config(feed_config[:channel])
44+
.to_h { |name| [name, typecast_params.str!(name)] }
45+
46+
config = global_config.merge(feed_config)
47+
config[:params] = params if params.any?
48+
config
49+
end
50+
51+
private
52+
53+
def required_params_for_config(config)
54+
config.each_with_object(Set.new) do |(_, value), required_params|
55+
required_params.merge(value.scan(/%<(\w+)>[s|d]/).flatten) if value.is_a?(String)
56+
end
57+
end
4558
end
4659

4760
##

app/http_cache.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module HttpCache
1515
# @param seconds [Integer]
1616
# @param cache_control [String, nil]
1717
def expires(response, seconds, cache_control: nil)
18+
expires_now(response) and return if seconds <= 0
19+
1820
response['Expires'] = (Time.now + seconds).httpdate
1921

2022
cache_value = "max-age=#{seconds}"

app/ssrf_filter_strategy.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def execute
1616
response = SsrfFilter.get(ctx.url, headers:)
1717

1818
Html2rss::RequestService::Response.new(body: response.body,
19+
url: ctx.url,
1920
headers: response.to_hash.transform_values(&:first))
2021
end
2122
end

config/feeds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ feeds:
1616
selector: "li > div"
1717
title:
1818
selector: "h4"
19-
link:
19+
url:
2020
selector: "a"
2121
extractor: "href"

helpers/handle_error.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ module Web
88
class App
99
def handle_error(error) # rubocop:disable Metrics/MethodLength
1010
case error
11-
when Html2rss::Config::ParamsMissing,
11+
when Html2rss::Config::DynamicParams::ParamsMissing,
1212
Roda::RodaPlugins::TypecastParams::Error
1313
set_error_response('Parameters missing or invalid', 422)
14-
when Html2rss::AttributePostProcessors::UnknownPostProcessorName,
15-
Html2rss::ItemExtractors::UnknownExtractorName,
16-
Html2rss::Config::ChannelMissing
14+
when Html2rss::Selectors::PostProcessors::UnknownPostProcessorName,
15+
Html2rss::Selectors::Extractors::UnknownExtractorName
1716
set_error_response('Invalid feed config', 422)
1817
when LocalConfig::NotFound,
1918
Html2rss::Configs::ConfigNotFound

helpers/handle_html2rss_configs.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def handle_html2rss_configs(request, _folder_name, _config_name_with_ext)
88

99
Html2rssFacade.from_config(path.full_config_name, typecast_params) do |config|
1010
response['Content-Type'] = CONTENT_TYPE_RSS
11-
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
11+
12+
HttpCache.expires(response, config.dig(:channel, :ttl).to_i * 60, cache_control: 'public')
1213
end
1314
end
1415
end

helpers/handle_local_config_feeds.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def handle_local_config_feeds(request, _config_name_with_ext)
88

99
Html2rssFacade.from_local_config(path.full_config_name, typecast_params) do |config|
1010
response['Content-Type'] = CONTENT_TYPE_RSS
11-
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
11+
12+
HttpCache.expires(response, config.dig(:channel, :ttl).to_i * 60, cache_control: 'public')
1213
end
1314
end
1415
end

0 commit comments

Comments
 (0)