Skip to content

Instantly share code, notes, and snippets.

@maxx1128
Last active October 14, 2019 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxx1128/af6c306dee50a0cb998b803d1fa57232 to your computer and use it in GitHub Desktop.
Save maxx1128/af6c306dee50a0cb998b803d1fa57232 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'open-uri'
require 'nokogiri'
require 'json'
class FreemanQuotes
URL = 'https://en.wikiquote.org/wiki/Freeman%27s_Mind'
def initialize
@page = Nokogiri::HTML.parse(open(URL))
end
def call
quotes
end
private
def quotes
freeman_quotes = []
@page.css('dd').each do |quote|
speaker = quote.css('b').to_s
freeman_quotes.push(clean_quote(quote)) if speaker.include? 'Freeman'
end
freeman_quotes
end
def clean_quote(quote)
quote
.to_s
.gsub(Regexp.union('<b>Freeman</b>: ',
'<b>Freeman</b>: ',
'<dd>',
'</dd>',
/<[^>]*>/,
/\[(.*?)\]/,
/\([^)]*\)/), '')
.gsub(' ', ' ')
.gsub('Freeman:', '').strip
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment