Last active
October 14, 2019 22:30
-
-
Save maxx1128/af6c306dee50a0cb998b803d1fa57232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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