Class: Html2rss::RequestService
- Inherits:
-
Object
- Object
- Html2rss::RequestService
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/html2rss/request_service.rb,
lib/html2rss/request_service/budget.rb,
lib/html2rss/request_service/policy.rb,
lib/html2rss/request_service/context.rb,
lib/html2rss/request_service/response.rb,
lib/html2rss/request_service/strategy.rb,
lib/html2rss/request_service/response_guard.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, e.g. integrating Browserless.io.
Defined Under Namespace
Classes: BlockedSurfaceDetected, BrowserlessConfigurationError, BrowserlessConnectionFailed, BrowserlessStrategy, Budget, Context, CrossOriginFollowUpDenied, FaradayStrategy, InvalidUrl, Policy, PrivateNetworkDenied, PuppetCommander, RequestBudgetExceeded, RequestTimedOut, Response, ResponseGuard, ResponseTooLarge, Strategy, UnknownStrategy, UnsupportedResponseContentType, 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 using the specified strategy.
-
#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.
40 41 42 43 44 45 46 |
# File 'lib/html2rss/request_service.rb', line 40 def initialize @strategies = { faraday: FaradayStrategy, browserless: BrowserlessStrategy } @default_strategy_name = :faraday end |
Instance Attribute Details
#default_strategy_name ⇒ Symbol
Returns the default strategy name.
49 50 51 |
# File 'lib/html2rss/request_service.rb', line 49 def default_strategy_name @default_strategy_name end |
Instance Method Details
#execute(ctx, strategy: default_strategy_name) ⇒ Response
Executes the request using the specified strategy.
104 105 106 107 108 109 110 111 |
# File 'lib/html2rss/request_service.rb', line 104 def execute(ctx, strategy: default_strategy_name) strategy_class = @strategies.fetch(strategy.to_sym) do raise UnknownStrategy, "The strategy '#{strategy}' is not known. Available strategies: #{strategy_names.join(', ')}" end strategy_class.new(ctx).execute end |
#register_strategy(name, strategy_class) ⇒ Object
Registers a new strategy.
69 70 71 72 73 74 75 |
# File 'lib/html2rss/request_service.rb', line 69 def register_strategy(name, strategy_class) unless strategy_class.is_a?(Class) raise ArgumentError, "Expected a Class for strategy, got #{strategy_class.class}" end @strategies[name.to_sym] = strategy_class end |
#strategy_names ⇒ Array<String>
Returns the names of the registered strategies.
62 |
# File 'lib/html2rss/request_service.rb', line 62 def strategy_names = @strategies.keys.map(&:to_s) |
#strategy_registered?(name) ⇒ Boolean
Checks if a strategy is registered.
81 82 83 |
# File 'lib/html2rss/request_service.rb', line 81 def strategy_registered?(name) @strategies.key?(name.to_sym) end |
#unregister_strategy(name) ⇒ Boolean
Unregisters a strategy.
90 91 92 93 94 95 |
# File 'lib/html2rss/request_service.rb', line 90 def unregister_strategy(name) # rubocop:disable Naming/PredicateMethod name_sym = name.to_sym raise ArgumentError, 'Cannot unregister the default strategy.' if name_sym == @default_strategy_name !!@strategies.delete(name_sym) end |