the-office-quote-generator 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/console +4 -0
- data/lib/character.rb +69 -0
- data/lib/office_quote_controller.rb +69 -0
- data/lib/quote.rb +68 -0
- data/lib/scraper.rb +36 -0
- data/lib/the_office_quote_generator.rb +6 -0
- metadata +91 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 3447749b38a478167ac687739af9884bbacdd5a3
|
|
4
|
+
data.tar.gz: 850b071064d959ed6694f3dc3563695ff9aaad8f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1b8b902de6f7a9f8f41d1638b2e8a3d0a5176217687b17a5b685b6c099c1e1a6046d415bb4878c5098c08394e12ec6386888a90f1e1f06d12aa7b191556d4a45
|
|
7
|
+
data.tar.gz: 90e163d05a57a41664fab7c1f5a382e97bd071fa43982d2d349b2f5fbbf3b83455bf1740d81494805564cb3a01beb40eabc8a015299297d74ec3265e3c0362ab
|
data/bin/console
ADDED
data/lib/character.rb
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
class Character
|
|
2
|
+
|
|
3
|
+
attr_accessor :name
|
|
4
|
+
|
|
5
|
+
@@all = []
|
|
6
|
+
|
|
7
|
+
def initialize(name)
|
|
8
|
+
@name = name
|
|
9
|
+
@quotes = []
|
|
10
|
+
@@all << self
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add_quote(quote)
|
|
14
|
+
if !@quotes.include?(quote)
|
|
15
|
+
@quotes << quote
|
|
16
|
+
end
|
|
17
|
+
if quote.character == nil
|
|
18
|
+
quote.character = self
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def quotes
|
|
23
|
+
@quotes
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.all
|
|
27
|
+
@@all
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.list_all_characters
|
|
31
|
+
@@all.each do |character|
|
|
32
|
+
puts character.name
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.list_all_quotes_by_character
|
|
37
|
+
@@all.each do |character|
|
|
38
|
+
i = 0
|
|
39
|
+
puts "Quotes for #{character.name}: "
|
|
40
|
+
character.quotes.each do |quote|
|
|
41
|
+
i += 1
|
|
42
|
+
puts "#{i}. #{quote.content}"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.list_all_quotes_for_character(name)
|
|
48
|
+
@@all.each do |character|
|
|
49
|
+
i = 0
|
|
50
|
+
if character.name == name
|
|
51
|
+
puts "Quotes for #{character.name}: "
|
|
52
|
+
character.quotes.each do |quote|
|
|
53
|
+
i += 1
|
|
54
|
+
puts "#{i}. #{quote.content}"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.random_quote_for_character(name)
|
|
61
|
+
@@all.each do |character|
|
|
62
|
+
if character.name == name
|
|
63
|
+
n = character.quotes.size - 1
|
|
64
|
+
r = rand(0..n)
|
|
65
|
+
puts "#{character.quotes[r].content}"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
class OfficeQuoteController
|
|
2
|
+
|
|
3
|
+
def initialize
|
|
4
|
+
quotes = Scraper.new
|
|
5
|
+
quotes.get_quote_pages
|
|
6
|
+
start_cli
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def start_cli
|
|
10
|
+
input = ""
|
|
11
|
+
puts "Welcome to The Office Quote Generator"
|
|
12
|
+
while input != "exit" do
|
|
13
|
+
puts "If you'd like to exit, enter \"exit\""
|
|
14
|
+
puts "Please select from the following options: "
|
|
15
|
+
puts "1. Choose a quote from a specific character"
|
|
16
|
+
puts "2. Hear a dialouge quote (between multiple characters)"
|
|
17
|
+
puts "3. Hear a random quote"
|
|
18
|
+
input = gets.chomp
|
|
19
|
+
if input == "1"
|
|
20
|
+
get_quotes_by_character
|
|
21
|
+
elsif input == "2"
|
|
22
|
+
get_dialouge_quote
|
|
23
|
+
elsif input == "3"
|
|
24
|
+
get_random_quote
|
|
25
|
+
elsif input != "exit"
|
|
26
|
+
puts "Please enter 1, 2 or 3"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def get_quotes_by_character
|
|
32
|
+
input = ""
|
|
33
|
+
while input != "exit" && input != "n" && input != "N" do
|
|
34
|
+
puts "You may choose from these characters: "
|
|
35
|
+
Character.list_all_characters
|
|
36
|
+
puts "Please select from one of the following characters by entering their name: "
|
|
37
|
+
input = gets.chomp
|
|
38
|
+
puts "Would you like to hear 1 or all of their quotes?"
|
|
39
|
+
puts "Enter \"1\" for one quote and anything else to hear all."
|
|
40
|
+
option = gets.chomp
|
|
41
|
+
if option != "1"
|
|
42
|
+
Character.list_all_quotes_for_character(input)
|
|
43
|
+
else
|
|
44
|
+
Character.random_quote_for_character(input)
|
|
45
|
+
end
|
|
46
|
+
puts "Would you like to choose another character? (y/n)"
|
|
47
|
+
input = gets.chomp
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def get_dialouge_quote
|
|
52
|
+
input = ""
|
|
53
|
+
while input != "exit" && input != "n" && input != "N" do
|
|
54
|
+
Quote.get_dialouge
|
|
55
|
+
puts "Would you like to hear another dialouge? (y/n)"
|
|
56
|
+
input = gets.chomp
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def get_random_quote
|
|
61
|
+
input = ""
|
|
62
|
+
while input != "exit" && input != "n" && input != "N" do
|
|
63
|
+
Quote.get_random
|
|
64
|
+
puts "Would you like to hear another quote? (y/n)"
|
|
65
|
+
input = gets.chomp
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
end
|
data/lib/quote.rb
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require 'open-uri'
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
require 'pry'
|
|
4
|
+
|
|
5
|
+
class Quote
|
|
6
|
+
attr_accessor :character, :content
|
|
7
|
+
|
|
8
|
+
@@all = []
|
|
9
|
+
@@dialouge_quotes = []
|
|
10
|
+
|
|
11
|
+
def initialize(content, character="")
|
|
12
|
+
@content = content
|
|
13
|
+
if character != ""
|
|
14
|
+
self.character=(character)
|
|
15
|
+
else
|
|
16
|
+
@@dialouge_quotes << self
|
|
17
|
+
end
|
|
18
|
+
@@all << self
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def character=(character)
|
|
22
|
+
if self.class.find_character_by_name(character) != nil
|
|
23
|
+
@character = self.class.find_character_by_name(character)
|
|
24
|
+
else
|
|
25
|
+
@character = Character.new(character)
|
|
26
|
+
end
|
|
27
|
+
@character.add_quote(self)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.all
|
|
31
|
+
@@all
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.list_all_quotes
|
|
35
|
+
@@all.each do |quote|
|
|
36
|
+
puts quote.content
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.find_character_by_name(name)
|
|
41
|
+
character = nil
|
|
42
|
+
self.all.each do |quote|
|
|
43
|
+
if quote.character != nil
|
|
44
|
+
if quote.character.name == name
|
|
45
|
+
character = quote.character
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
character
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.get_dialouge
|
|
53
|
+
n = @@dialouge_quotes.size - 1
|
|
54
|
+
r = rand(0..n)
|
|
55
|
+
puts "#{@@dialouge_quotes[r].content}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.get_random
|
|
59
|
+
n = @@all.size - 1
|
|
60
|
+
r = rand(0..n)
|
|
61
|
+
if @@all[r].character != nil
|
|
62
|
+
puts "#{@@all[r].content} -#{@@all[r].character.name}"
|
|
63
|
+
else
|
|
64
|
+
puts "#{@@all[r].content}"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end
|
data/lib/scraper.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'open-uri'
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
require 'pry'
|
|
4
|
+
|
|
5
|
+
class Scraper
|
|
6
|
+
|
|
7
|
+
def scrape_office_quotes(page)
|
|
8
|
+
index_page = Nokogiri::HTML(open(page))
|
|
9
|
+
line_string = ""
|
|
10
|
+
index_page.css("div.quotes blockquote").each do |quote|
|
|
11
|
+
character = quote.css("small").text.strip
|
|
12
|
+
if character.length > 40
|
|
13
|
+
character = ""
|
|
14
|
+
end
|
|
15
|
+
quote.css("p").each do |line|
|
|
16
|
+
# Using .text will not push items with breaks right next to each other
|
|
17
|
+
# Using .to_s on the nokogiri element allow me to identify <br>'s create space between each line within the <p> element
|
|
18
|
+
line_string = line.to_s
|
|
19
|
+
line_string.gsub!("<br>", " ")
|
|
20
|
+
line_string.gsub!("<p>", "")
|
|
21
|
+
line_string.gsub!("</p>", "")
|
|
22
|
+
line_string = line_string.strip
|
|
23
|
+
end
|
|
24
|
+
Quote.new(line_string, character)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def get_quote_pages
|
|
29
|
+
216.times do |i|
|
|
30
|
+
# 10.times do |i|
|
|
31
|
+
page = "https://www.tvfanatic.com/quotes/shows/the-office/page-" + "#{i + 1}" + ".html"
|
|
32
|
+
scrape_office_quotes(page)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: the-office-quote-generator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Elena Hopkins
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-06-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.10'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: nokogiri
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: pry
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: Allows a user to generate quotes from "The Office"
|
|
56
|
+
email: elenamariehopkins@gmail.com
|
|
57
|
+
executables: []
|
|
58
|
+
extensions: []
|
|
59
|
+
extra_rdoc_files: []
|
|
60
|
+
files:
|
|
61
|
+
- bin/console
|
|
62
|
+
- lib/character.rb
|
|
63
|
+
- lib/office_quote_controller.rb
|
|
64
|
+
- lib/quote.rb
|
|
65
|
+
- lib/scraper.rb
|
|
66
|
+
- lib/the_office_quote_generator.rb
|
|
67
|
+
homepage:
|
|
68
|
+
licenses:
|
|
69
|
+
- MIT
|
|
70
|
+
metadata: {}
|
|
71
|
+
post_install_message:
|
|
72
|
+
rdoc_options: []
|
|
73
|
+
require_paths:
|
|
74
|
+
- lib
|
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '0'
|
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: '0'
|
|
85
|
+
requirements: []
|
|
86
|
+
rubyforge_project:
|
|
87
|
+
rubygems_version: 2.6.11
|
|
88
|
+
signing_key:
|
|
89
|
+
specification_version: 4
|
|
90
|
+
summary: Quotes from the TV series, "The Office"
|
|
91
|
+
test_files: []
|