Class: Html2rss::RequestService
- Inherits:
-
Object
- Object
- Html2rss::RequestService
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/html2rss/request_service.rb,
lib/html2rss/request_service/context.rb,
lib/html2rss/request_service/response.rb,
lib/html2rss/request_service/strategy.rb,
lib/html2rss/request_service/faraday_strategy.rb,
lib/html2rss/request_service/puppet_commander.rb,
lib/html2rss/request_service/browserless_strategy.rb
Overview
Requests website URLs to retrieve their HTML for further processing. Provides strategies, i.e. to integrate Browserless.io.
Defined Under Namespace
Classes: BrowserlessStrategy, Context, FaradayStrategy, InvalidUrl, PuppetCommander, Response, Strategy, UnknownStrategy, UnsupportedUrlScheme
Instance Attribute Summary collapse
-
#default_strategy_name ⇒ Symbol
The default strategy name.
Instance Method Summary collapse
-
#execute(ctx, strategy: default_strategy_name) ⇒ Response
Executes the request.
-
#initialize ⇒ RequestService
constructor
A new instance of RequestService.
-
#register_strategy(name, strategy_class) ⇒ Object
Registers a new strategy.
-
#strategy_names ⇒ Array<String>
The names of the registered strategies.
-
#strategy_registered?(name) ⇒ Boolean
Checks if a strategy is registered.
-
#unregister_strategy(name) ⇒ Boolean
Unregisters a strategy.
Constructor Details
#initialize ⇒ RequestService
Returns a new instance of RequestService.
31 32 33 34 35 36 37 |
# File 'lib/html2rss/request_service.rb', line 31 def initialize @strategies = { faraday: FaradayStrategy, browserless: BrowserlessStrategy } @default_strategy_name = :faraday end |
Instance Attribute Details
#default_strategy_name ⇒ Symbol
Returns the default strategy name.
40 41 42 |
# File 'lib/html2rss/request_service.rb', line 40 def default_strategy_name @default_strategy_name end |
Instance Method Details
#execute(ctx, strategy: default_strategy_name) ⇒ Response
Executes the request.
89 90 91 92 93 94 95 |
# File 'lib/html2rss/request_service.rb', line 89 def execute(ctx, strategy: default_strategy_name) strategy_class = @strategies.fetch(strategy) do raise UnknownStrategy, "The strategy '#{strategy}' is not known. Available strategies are: #{strategy_names.join(', ')}" end strategy_class.new(ctx).execute end |
#register_strategy(name, strategy_class) ⇒ Object
Registers a new strategy.
59 60 61 62 63 |
# File 'lib/html2rss/request_service.rb', line 59 def register_strategy(name, strategy_class) raise ArgumentError, 'Strategy class must be a Class' unless strategy_class.is_a?(Class) @strategies[name.to_sym] = strategy_class end |
#strategy_names ⇒ Array<String>
Returns the names of the registered strategies.
53 |
# File 'lib/html2rss/request_service.rb', line 53 def strategy_names = @strategies.keys.map(&:to_s) |
#strategy_registered?(name) ⇒ Boolean
Checks if a strategy is registered.
69 70 71 |
# File 'lib/html2rss/request_service.rb', line 69 def strategy_registered?(name) @strategies.key?(name.to_sym) end |
#unregister_strategy(name) ⇒ Boolean
Unregisters a strategy.
77 78 79 80 81 |
# File 'lib/html2rss/request_service.rb', line 77 def unregister_strategy(name) raise ArgumentError, 'Cannot unregister the default strategy' if name.to_sym == @default_strategy_name !!@strategies.delete(name.to_sym) end |