Class: Html2rss::RequestService

Inherits:
Object
  • Object
show all
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/network_guard.rb,
lib/html2rss/request_service/response_guard.rb,
lib/html2rss/request_service/blocked_surface.rb,
lib/html2rss/request_service/faraday_strategy.rb,
lib/html2rss/request_service/puppet_commander.rb,
lib/html2rss/request_service/botasaurus_contract.rb,
lib/html2rss/request_service/botasaurus_strategy.rb,
lib/html2rss/request_service/local_file_strategy.rb,
lib/html2rss/request_service/browserless_strategy.rb,
lib/html2rss/request_service/puppet_commander/preload_runner.rb,
lib/html2rss/request_service/puppet_commander/navigation_guards.rb

Overview

Requests website URLs to retrieve their HTML for further processing. Provides concrete transport strategies (e.g. Faraday, Browserless).

Feed-level :auto is not registered here — FeedPipeline::StrategyPlan resolves it to a concrete strategy (or FeedPipeline::AutoFallback chain) before execute.

Defined Under Namespace

Modules: BlockedSurface Classes: BlockedSurfaceDetected, BotasaurusConfigurationError, BotasaurusConnectionFailed, BotasaurusContract, BotasaurusStrategy, BrowserlessConfigurationError, BrowserlessConnectionFailed, BrowserlessStrategy, Budget, Context, CrossOriginFollowUpDenied, FaradayStrategy, InteractionBudgetExceeded, InvalidUrl, LocalFileStrategy, NetworkGuard, Policy, PrivateNetworkDenied, PuppetCommander, RequestBudgetExceeded, RequestTimedOut, Response, ResponseGuard, ResponseTooLarge, Strategy, UnknownStrategy, UnsupportedResponseContentType, UnsupportedUrlScheme

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequestService

Returns a new instance of RequestService.



61
62
63
64
65
66
67
68
69
# File 'lib/html2rss/request_service.rb', line 61

def initialize
  @strategies = {
    faraday: FaradayStrategy,
    botasaurus: BotasaurusStrategy,
    browserless: BrowserlessStrategy,
    local_file: LocalFileStrategy
  }
  @default_strategy_name = :faraday
end

Instance Attribute Details

#default_strategy_nameSymbol

Returns the default strategy name.

Returns:

  • (Symbol)

    the default strategy name



72
73
74
# File 'lib/html2rss/request_service.rb', line 72

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.

Parameters:

  • ctx (Context)

    the context for the request.

  • strategy (Symbol) (defaults to: default_strategy_name)

    the strategy to use (defaults to the default strategy).

Returns:

  • (Response)

    the response from the executed strategy.

Raises:

  • (ArgumentError)

    if the context is nil.

  • (UnknownStrategy)

    if the strategy is not registered.



129
130
131
132
133
134
135
136
# File 'lib/html2rss/request_service.rb', line 129

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) ⇒ Class

Registers a new strategy.

Parameters:

  • name (Symbol)

    the name of the strategy

  • strategy_class (Class)

    the class implementing the strategy

Returns:

  • (Class)

    the registered strategy class

Raises:

  • (ArgumentError)

    if strategy_class is not a Class



94
95
96
97
98
99
100
# File 'lib/html2rss/request_service.rb', line 94

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_namesArray<String>

Returns the names of the registered strategies.

Returns:

  • (Array<String>)

    the names of the registered strategies



86
# File 'lib/html2rss/request_service.rb', line 86

def strategy_names = @strategies.keys.map(&:to_s)

#strategy_registered?(name) ⇒ Boolean

Checks if a strategy is registered.

Parameters:

  • name (Symbol)

    the name of the strategy

Returns:

  • (Boolean)

    true if the strategy is registered, false otherwise.



106
107
108
# File 'lib/html2rss/request_service.rb', line 106

def strategy_registered?(name)
  @strategies.key?(name.to_sym)
end

#unregister_strategy(name) ⇒ Boolean

Unregisters a strategy.

Parameters:

  • name (Symbol)

    the name of the strategy

Returns:

  • (Boolean)

    true if the strategy was unregistered, false otherwise.

Raises:

  • (ArgumentError)

    if attempting to unregister the default strategy.



115
116
117
118
119
120
# File 'lib/html2rss/request_service.rb', line 115

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