Class: Html2rss::RequestService

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeRequestService

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_nameSymbol

Returns the default strategy name.

Returns:

  • (Symbol)

    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.

Parameters:

  • ctx (Context)

    the context for the request

  • strategy (Symbol) (defaults to: default_strategy_name)

    the strategy to use

Returns:

  • (Response)

    the response from the strategy

Raises:



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.

Parameters:

  • name (Symbol)

    the name of the strategy

  • strategy_class (Class)

    the class of the strategy

Raises:

  • (ArgumentError)


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

Returns the names of the registered strategies.

Returns:

  • (Array<String>)

    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.

Parameters:

  • name (Symbol)

    the name of the strategy

Returns:

  • (Boolean)

    true if the strategy is registered, false otherwise



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.

Parameters:

  • name (Symbol)

    the name of the strategy

Returns:

  • (Boolean)

    true if the strategy was unregistered, false otherwise

Raises:

  • (ArgumentError)


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