docbrown/app/services/medium_article_retrieval_service.rb
Anna Buianova 784afdf41e
Routine rubocop fixes (#19254)
* Rubocop fixes

* Rubocop fixes

* Fixed rubocop violation

* Fixed policies rubocop violations

* More rubocop fixes
2023-03-24 14:37:44 +03:00

40 lines
1,022 B
Ruby

class MediumArticleRetrievalService
attr_reader :url
def self.call(...)
new(...).call
end
def initialize(url)
@url = url
end
def call
response = HTTParty.get(url)
page = Nokogiri::HTML(response.body)
title = page.at("meta[name='title']")["content"]
reading_time = page.at("meta[name='twitter:data1']")["value"]
author = page.at("meta[name='author']")["content"]
author_image = page.at("img[alt='#{author}']")["src"]
published_time = page.at("meta[property='article:published_time']")["content"]
{
title: title,
author: author,
author_image: author_image,
reading_time: reading_time,
published_time: published_time,
publication_date: publication_date(published_time),
url: url
}
end
private
def publication_date(published_time)
I18n.l(Time.zone.parse(published_time), format: :medium)
rescue ArgumentError, NoMethodError => e
Rails.logger.error("#{published_time} is not a valid date: #{e}")
end
end