module NewsScraper::ExtractorsHelpers

Public Instance Methods

http_request(url) { |response| ... } click to toggle source

Perform an HTTP request with a standardized response

Params

  • url: the url on which to perform a get request

# File lib/news_scraper/extractors_helpers.rb, line 8
def http_request(url)
  url = URIParser.new(url).with_scheme

  CLI.put_header(url)
  CLI.log "Beginning HTTP request for #{url}"
  response = HTTParty.get(url, headers: { "User-Agent" => "news-scraper-#{NewsScraper::VERSION}" })

  raise ResponseError.new(
    error_code: response.code,
    message: response.message,
    url: url
  ) unless response.code == 200

  CLI.log "#{response.code} - #{response.message}. Request successful for #{url}"
  CLI.put_footer

  if block_given?
    yield response
  else
    response
  end
end