Class: NewsScraper::Transformers::TrainerArticle

Inherits:
Article
  • Object
show all
Defined in:
lib/news_scraper/transformers/trainer_article.rb

Instance Method Summary collapse

Constructor Details

#initialize(url:, payload:) ⇒ TrainerArticle

Initialize a TrainerArticle object

Params

  • url: keyword arg - the url on which scraping was done

  • payload: keyword arg - the result of the scrape



10
11
12
# File 'lib/news_scraper/transformers/trainer_article.rb', line 10

def initialize(url:, payload:)
  super(url: url, payload: payload)
end

Instance Method Details

#transformObject

Transform the article

Returns

  • transformed_response: tries all possible presets and returns a hash representing the results



19
20
21
22
23
24
25
26
27
28
# File 'lib/news_scraper/transformers/trainer_article.rb', line 19

def transform
  presets = NewsScraper.configuration.scrape_patterns['presets']
  transformed_response = presets.each_with_object({}) do |(data_type, preset_options), response|
    response[data_type] = preset_options.each_with_object({}) do |(option, scrape_details), data_type_options|
      data = parsed_data(scrape_details['method'].to_sym, scrape_details['pattern'])
      data_type_options[option] = scrape_details.merge('data' => data)
    end
  end
  transformed_response.merge('url' => @url, 'root_domain' => @root_domain)
end