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/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

Instance Method Summary collapse

Constructor Details

#initializeRequestService

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_nameSymbol

Returns the default strategy name.

Returns:

  • (Symbol)

    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.

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.



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.

Parameters:

  • name (Symbol)

    the name of the strategy

  • strategy_class (Class)

    the class implementing the strategy

Raises:

  • (ArgumentError)

    if strategy_class is not a Class



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

Returns the names of the registered strategies.

Returns:

  • (Array<String>)

    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.

Parameters:

  • name (Symbol)

    the name of the strategy

Returns:

  • (Boolean)

    true if the strategy is registered, false otherwise.



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.

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.



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