Class: NewsScraper::Trainer::PresetSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/news_scraper/trainer/preset_selector.rb

Constant Summary collapse

PROVIDER_PHRASE =
'I will provide a pattern using'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url:, payload:) ⇒ PresetSelector

Returns a new instance of PresetSelector.



6
7
8
9
# File 'lib/news_scraper/trainer/preset_selector.rb', line 6

def initialize(url:, payload:)
  @url = url
  @payload = payload
end

Instance Method Details

#select(data_type) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/news_scraper/trainer/preset_selector.rb', line 11

def select(data_type)
  pattern_options = pattern_options(data_type)

  selected_option = CLI.prompt_with_options(
    "Select which preset to use for #{data_type}:",
    pattern_options.keys
  )

  if selected_option.start_with?(PROVIDER_PHRASE)
    pattern_type = pattern_options[selected_option]
    return {
      'method' => pattern_type,
      'pattern' => CLI.get_input("Provide the #{pattern_type} pattern:")
    }
  end
  return if selected_option == 'skip'

  selected_preset_code = pattern_options[selected_option]
  result = transform_results[data_type][selected_preset_code].merge(
    'variable' => [selected_preset_code, data_type].join('_')
  )
  result.delete('data')
  result
end