notable_books_2018 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +47 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/notable-books-2018 +9 -0
- data/bin/setup +8 -0
- data/lib/environment.rb +12 -0
- data/lib/notable_books_2018/book.rb +32 -0
- data/lib/notable_books_2018/cli.rb +318 -0
- data/lib/notable_books_2018/genre.rb +29 -0
- data/lib/notable_books_2018/scraper.rb +34 -0
- data/lib/notable_books_2018/version.rb +3 -0
- data/notable_books_2018.gemspec +43 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cd5615250f99e3e1eb341e0f6130c317562f9b97da2fd82a3b0050ffdf5aaf85
|
4
|
+
data.tar.gz: c7c0b98818db23e7dde305e40f92455ae31336348507d89d4732303a00113aaa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4efd7f5bf9d96fc9e6d27481a0a425e9e6de6d02bf227887abd7e2034be960e684f94eb847c9f164f1a98a18c097820ac0556454de7c38965ad259bdd36648e2
|
7
|
+
data.tar.gz: 22b4a2332e572da6a200d3892074cb839d56d19145a0a1cc85fb4316249c4f856e232fede6c0d1bf9cc93653b877e442df8b33157a149eaac238d891903324de
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at noraevogt@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
notable_books_2018 (0.1.0)
|
5
|
+
nokogiri
|
6
|
+
paint
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
coderay (1.1.2)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
method_source (0.9.2)
|
14
|
+
mini_portile2 (2.4.0)
|
15
|
+
nokogiri (1.10.1)
|
16
|
+
mini_portile2 (~> 2.4.0)
|
17
|
+
paint (2.1.0)
|
18
|
+
pry (0.12.2)
|
19
|
+
coderay (~> 1.1.0)
|
20
|
+
method_source (~> 0.9.0)
|
21
|
+
rake (10.5.0)
|
22
|
+
rspec (3.8.0)
|
23
|
+
rspec-core (~> 3.8.0)
|
24
|
+
rspec-expectations (~> 3.8.0)
|
25
|
+
rspec-mocks (~> 3.8.0)
|
26
|
+
rspec-core (3.8.0)
|
27
|
+
rspec-support (~> 3.8.0)
|
28
|
+
rspec-expectations (3.8.2)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.8.0)
|
31
|
+
rspec-mocks (3.8.0)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.8.0)
|
34
|
+
rspec-support (3.8.0)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
bundler (~> 2.0)
|
41
|
+
notable_books_2018!
|
42
|
+
pry
|
43
|
+
rake (~> 10.0)
|
44
|
+
rspec
|
45
|
+
|
46
|
+
BUNDLED WITH
|
47
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 norawolf
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# NotableBooks2018
|
2
|
+
|
3
|
+
This gem is a CLI application that utilizes data from the New York Times Book Review's "100 Notable Books of 2018" list. You can browse books and find more information about your book selection. Happy reading!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'notable_books_2018'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install notable_books_2018
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Run this gem by entering
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
$ ./bin/notable-books-2018
|
27
|
+
```
|
28
|
+
|
29
|
+
and follow the prompts to navigate the gem.
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/norawolf/notable_books_2018. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
44
|
+
|
45
|
+
## Code of Conduct
|
46
|
+
|
47
|
+
Everyone interacting in the NotableBooks2018 project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/norawolf/notable_books_2018/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "environment" #does it need to be "environment.rb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
#require "pry"
|
11
|
+
#Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/environment.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "pry"
|
2
|
+
require "nokogiri"
|
3
|
+
require "open-uri"
|
4
|
+
require "paint"
|
5
|
+
|
6
|
+
# here we will require all relevant files in dir notable_books_2018
|
7
|
+
|
8
|
+
require_relative "notable_books_2018/version.rb"
|
9
|
+
require_relative "notable_books_2018/cli.rb"
|
10
|
+
require_relative "notable_books_2018/book.rb"
|
11
|
+
require_relative "notable_books_2018/scraper.rb"
|
12
|
+
require_relative "notable_books_2018/genre.rb"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class NotableBooks2018::Book
|
2
|
+
|
3
|
+
@@all = []
|
4
|
+
|
5
|
+
attr_accessor :title, :author, :description, :publisher, :price
|
6
|
+
attr_reader :genre
|
7
|
+
|
8
|
+
def self.all
|
9
|
+
@@all
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(book_hash)
|
13
|
+
book_hash.each do |attribute, value|
|
14
|
+
self.send("#{attribute}=", value)
|
15
|
+
end
|
16
|
+
@@all << self
|
17
|
+
end
|
18
|
+
|
19
|
+
def genre=(genre)
|
20
|
+
@genre = genre
|
21
|
+
genre.each do |genre_instance|
|
22
|
+
genre_instance.books << self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.create_from_collection(book_array)
|
27
|
+
book_array.each do |book_hash|
|
28
|
+
NotableBooks2018::Book.new(book_hash)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,318 @@
|
|
1
|
+
class NotableBooks2018::CLI
|
2
|
+
|
3
|
+
def run
|
4
|
+
NotableBooks2018::Scraper.scrape_book_info
|
5
|
+
welcome
|
6
|
+
goodbye
|
7
|
+
end
|
8
|
+
|
9
|
+
def welcome
|
10
|
+
puts "\n--------------------------------------------"
|
11
|
+
puts Paint["Welcome to Notable Books of 2018!", :bright]
|
12
|
+
puts "--------------------------------------------"
|
13
|
+
puts <<~DOC
|
14
|
+
|
15
|
+
At the end of 2018, The New York Times Book Review published a list of the
|
16
|
+
notable new book releases from that year. You can use this gem to browse
|
17
|
+
2018's book selections and find more information about each book that
|
18
|
+
interests you.
|
19
|
+
|
20
|
+
Would you like to browse books by genre or view books by list number?
|
21
|
+
DOC
|
22
|
+
|
23
|
+
choose_display
|
24
|
+
end
|
25
|
+
|
26
|
+
def choose_display
|
27
|
+
puts "\nEnter #{Paint["1", :magenta]} to view books by genre."
|
28
|
+
puts "Enter #{Paint["2", :magenta]} to view the numbered book list."
|
29
|
+
puts "Or, enter #{Paint["'exit'", :magenta]} to quit."
|
30
|
+
|
31
|
+
input = gets.chomp
|
32
|
+
if input.to_i == 1
|
33
|
+
view_books_by_genre
|
34
|
+
elsif input.to_i == 2
|
35
|
+
view_books_by_num
|
36
|
+
elsif input == "exit"
|
37
|
+
goodbye
|
38
|
+
else
|
39
|
+
puts "\nThat is not a valid selection."
|
40
|
+
choose_display
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def view_books_by_genre
|
45
|
+
list_genres
|
46
|
+
choose_genre
|
47
|
+
select_book_by_number_through_genre
|
48
|
+
see_more_books_by_genre
|
49
|
+
end
|
50
|
+
|
51
|
+
def list_genres
|
52
|
+
puts "\n--------------------------------------------"
|
53
|
+
puts Paint["Displaying All Genres", :bright]
|
54
|
+
puts "--------------------------------------------"
|
55
|
+
puts "\n"
|
56
|
+
|
57
|
+
@all_genre_names = []
|
58
|
+
NotableBooks2018::Genre.all.collect do |genre|
|
59
|
+
puts genre.name
|
60
|
+
@all_genre_names << genre.name.downcase
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def choose_genre
|
65
|
+
puts "\nEnter a #{Paint["genre name", :magenta]} to browse books by genre."
|
66
|
+
puts "Enter #{Paint["'main'", :magenta]} to return to the main menu or "\
|
67
|
+
"#{Paint["'exit'", :magenta]} to quit."
|
68
|
+
|
69
|
+
@genre_name = gets.chomp.downcase
|
70
|
+
|
71
|
+
if @all_genre_names.include?(@genre_name)
|
72
|
+
print_books_by_genre(@genre_name)
|
73
|
+
elsif @genre_name == "main"
|
74
|
+
welcome
|
75
|
+
elsif @genre_name == "exit"
|
76
|
+
goodbye
|
77
|
+
else
|
78
|
+
puts "\nThat is not a valid entry."
|
79
|
+
choose_genre
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def print_books_by_genre(genre_name)
|
84
|
+
puts "\n--------------------------------------------"
|
85
|
+
puts Paint["Viewing: #{genre_name.split.map(&:capitalize!).join(" ")}",
|
86
|
+
:bright]
|
87
|
+
puts "--------------------------------------------"
|
88
|
+
puts ""
|
89
|
+
|
90
|
+
@indices_from_genre = []
|
91
|
+
NotableBooks2018::Genre.all.each do |genre|
|
92
|
+
if genre_name == genre.name.downcase
|
93
|
+
genre.books.each.with_index(1) do |book, index|
|
94
|
+
puts "#{index}. #{book.title} by #{book.author}"
|
95
|
+
@indices_from_genre << index.to_i
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def select_book_by_number_through_genre
|
102
|
+
puts "\nEnter the #{Paint["number", :magenta]} of a book you would like "\
|
103
|
+
"to read more about."
|
104
|
+
puts "Or, enter #{Paint["'back'", :magenta]} to return the genre list or "\
|
105
|
+
"#{Paint["'exit'", :magenta]} to quit."
|
106
|
+
|
107
|
+
@book_index_from_genre = gets.chomp
|
108
|
+
|
109
|
+
if @indices_from_genre.include?(@book_index_from_genre.to_i)
|
110
|
+
print_book_info_from_genre(@book_index_from_genre.to_i)
|
111
|
+
elsif @book_index_from_genre == "back"
|
112
|
+
view_books_by_genre
|
113
|
+
elsif @book_index_from_genre == "exit"
|
114
|
+
goodbye
|
115
|
+
else
|
116
|
+
puts "\nPlease enter a number between #{@indices_from_genre.first}-"\
|
117
|
+
"#{@indices_from_genre.last}."
|
118
|
+
select_book_by_number_through_genre
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def print_book_info_from_genre(book_index)
|
123
|
+
NotableBooks2018::Genre.all.each do |genre_instance|
|
124
|
+
if @genre_name == genre_instance.name.downcase
|
125
|
+
genre_instance.books.each.with_index(1) do |book, index|
|
126
|
+
if book_index == index
|
127
|
+
book_info_contents(book)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def see_more_books_by_genre
|
135
|
+
puts <<~DOC
|
136
|
+
Would you like to see another book?
|
137
|
+
Enter #{Paint["'back'", :magenta]} to return to your chosen genre's books.
|
138
|
+
Enter #{Paint["'list'", :magenta]} to return to view all genres.
|
139
|
+
Enter #{Paint["'number'", :magenta]} to switch to browsing books by number.
|
140
|
+
Enter #{Paint["'exit'", :magenta]} to quit.
|
141
|
+
DOC
|
142
|
+
|
143
|
+
input = gets.chomp.downcase
|
144
|
+
|
145
|
+
case input
|
146
|
+
when "back"
|
147
|
+
print_books_by_genre(@genre_name)
|
148
|
+
select_book_by_number_through_genre
|
149
|
+
print_book_info_from_genre(@book_index_from_genre)
|
150
|
+
see_more_books_by_genre
|
151
|
+
when "list"
|
152
|
+
view_books_by_genre
|
153
|
+
when "number"
|
154
|
+
view_books_by_num
|
155
|
+
when "exit"
|
156
|
+
goodbye
|
157
|
+
else
|
158
|
+
puts "\nThat is not a valid selction."
|
159
|
+
see_more_books_by_genre
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def view_books_by_num
|
164
|
+
display_number_groups
|
165
|
+
choose_books_by_num
|
166
|
+
end
|
167
|
+
|
168
|
+
def display_number_groups
|
169
|
+
puts <<~DOC
|
170
|
+
|
171
|
+
--------------------------------------------
|
172
|
+
#{Paint["View Books Numbered:", :bright]}
|
173
|
+
--------------------------------------------
|
174
|
+
1-10
|
175
|
+
11-20
|
176
|
+
21-30
|
177
|
+
31-40
|
178
|
+
41-50
|
179
|
+
51-60
|
180
|
+
61-70
|
181
|
+
71-80
|
182
|
+
81-90
|
183
|
+
91-100
|
184
|
+
DOC
|
185
|
+
end
|
186
|
+
|
187
|
+
def choose_books_by_num
|
188
|
+
@number_input = nil
|
189
|
+
|
190
|
+
puts "\nEnter a #{Paint["number", :magenta]} to see a list of books."
|
191
|
+
puts "Enter #{Paint["'main'", :magenta]} to return to the main menu or "\
|
192
|
+
"#{Paint["'exit'", :magenta]} to quit."
|
193
|
+
|
194
|
+
@number_input = gets.chomp
|
195
|
+
|
196
|
+
if (1..100).include?(@number_input.to_i)
|
197
|
+
print_book_list(@number_input.to_i)
|
198
|
+
select_book_by_number
|
199
|
+
see_more_books
|
200
|
+
elsif @number_input.downcase == "main"
|
201
|
+
welcome
|
202
|
+
elsif @number_input.downcase == "exit"
|
203
|
+
goodbye
|
204
|
+
else
|
205
|
+
puts "\nThat is not a valid selection."
|
206
|
+
choose_books_by_num
|
207
|
+
end
|
208
|
+
puts ""
|
209
|
+
end
|
210
|
+
|
211
|
+
def print_book_list(by_number)
|
212
|
+
if by_number == 100
|
213
|
+
puts "\n--------------------------------------------"
|
214
|
+
puts "#{Paint["Displaying The 100th Notable Book", :bright]}"
|
215
|
+
puts "--------------------------------------------"
|
216
|
+
elsif by_number >= 92 && by_number != 100
|
217
|
+
puts "\n--------------------------------------------"
|
218
|
+
puts "#{Paint["Displaying Notable Books #{by_number} - 100", :bright]}"
|
219
|
+
"--------------------------------------------"
|
220
|
+
else
|
221
|
+
puts "\n--------------------------------------------"
|
222
|
+
puts "#{Paint["Displaying Notable Books #{by_number} - #{by_number+9}",
|
223
|
+
:bright]}"
|
224
|
+
puts "--------------------------------------------"
|
225
|
+
puts ""
|
226
|
+
end
|
227
|
+
|
228
|
+
NotableBooks2018::Book.all[by_number-1, 10].each.with_index(by_number) do |book, index|
|
229
|
+
puts "#{index}. #{book.title} by #{book.author}"
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
def select_book_by_number
|
235
|
+
puts "\nEnter the #{Paint["number", :magenta]} of a book you would like "\
|
236
|
+
"more information about."
|
237
|
+
puts "Or, enter #{Paint["'back'", :magenta]} to return to the numbered "\
|
238
|
+
"book list or #{Paint["'exit'", :magenta]} to quit."
|
239
|
+
second_input = gets.chomp
|
240
|
+
|
241
|
+
if ((@number_input.to_i)..((@number_input.to_i)+9)).include?(second_input.to_i)
|
242
|
+
print_book_info(second_input.to_i)
|
243
|
+
elsif second_input == "back"
|
244
|
+
view_books_by_num
|
245
|
+
elsif second_input.downcase == "exit"
|
246
|
+
goodbye
|
247
|
+
else
|
248
|
+
puts "\nPlease enter a number #{@number_input.to_i}-#{(@number_input.to_i)+9}."
|
249
|
+
select_book_by_number
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def print_book_info(book_index)
|
254
|
+
NotableBooks2018::Book.all.each.with_index(1) do |book, index|
|
255
|
+
if index == book_index
|
256
|
+
book_info_contents(book)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def book_info_contents(book)
|
262
|
+
puts "\n--------------------------------------------"
|
263
|
+
puts Paint["#{book.title}", :bright]
|
264
|
+
puts "by #{book.author}"
|
265
|
+
puts "--------------------------------------------"
|
266
|
+
puts "#{Paint["\nPublisher:", :bright]} #{book.publisher}"
|
267
|
+
puts "#{Paint["Price:", :bright]} #{book.price}"
|
268
|
+
if book.genre.length == 1
|
269
|
+
puts Paint["\nGenre:", :bright]
|
270
|
+
else
|
271
|
+
puts Paint["\nGenres:", :bright]
|
272
|
+
end
|
273
|
+
book.genre.collect do |genre|
|
274
|
+
puts "#{genre.name}"
|
275
|
+
end
|
276
|
+
puts Paint["\nDescription:",:bright]
|
277
|
+
puts "#{wrap_text(book.description)}"
|
278
|
+
puts "\n--------------------------------------------"
|
279
|
+
end
|
280
|
+
|
281
|
+
def see_more_books
|
282
|
+
puts <<~DOC
|
283
|
+
Would you like to see another book?
|
284
|
+
Enter #{Paint["'back'", :magenta]} to return to your selected list.
|
285
|
+
Enter #{Paint["'list'", :magenta]} to return to the main list of books by number.
|
286
|
+
Enter #{Paint["'genre'", :magenta]} to switch to browsing books by genre.
|
287
|
+
Enter #{Paint["'exit'", :magenta]} to quit.
|
288
|
+
DOC
|
289
|
+
|
290
|
+
input = gets.chomp.downcase
|
291
|
+
|
292
|
+
case input
|
293
|
+
when "back"
|
294
|
+
print_book_list(@number_input.to_i)
|
295
|
+
select_book_by_number
|
296
|
+
see_more_books
|
297
|
+
when "list"
|
298
|
+
view_books_by_num
|
299
|
+
when "genre"
|
300
|
+
view_books_by_genre
|
301
|
+
when "exit"
|
302
|
+
goodbye
|
303
|
+
else
|
304
|
+
puts "\nThat is not a valid selection."
|
305
|
+
see_more_books
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
def wrap_text(txt, col = 80)
|
310
|
+
txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n")
|
311
|
+
end
|
312
|
+
|
313
|
+
def goodbye
|
314
|
+
puts "\nThank you for browsing Notable Books 2018. Happy reading!"
|
315
|
+
exit
|
316
|
+
end
|
317
|
+
|
318
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class NotableBooks2018::Genre
|
2
|
+
|
3
|
+
attr_accessor :name
|
4
|
+
attr_reader :books
|
5
|
+
|
6
|
+
@@all = []
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
@@all << self
|
11
|
+
@books = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.all
|
15
|
+
@@all
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.find_by_name(name)
|
19
|
+
all.find {|genre| genre.name == name}
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.create_by_name(name)
|
23
|
+
NotableBooks2018::Genre.new(name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.find_or_create_by_name(name)
|
27
|
+
find_by_name(name) || create_by_name(name)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class NotableBooks2018::Scraper
|
2
|
+
def self.scrape_page
|
3
|
+
url = "https://www.nytimes.com/interactive/2018/11/19/books/review/100-notable-books.html"
|
4
|
+
Nokogiri::HTML(open(url))
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.scrape_book_info
|
8
|
+
books_array = []
|
9
|
+
scrape_page.css(".g-book-data").each.with_index do |nodeset|
|
10
|
+
book_hash = {}
|
11
|
+
|
12
|
+
details = nodeset.css(".g-book-author").text
|
13
|
+
.split(". ").map!(&:strip)
|
14
|
+
|
15
|
+
book_hash[:title] = nodeset.css(".g-book-title").text.strip
|
16
|
+
book_hash[:author] = nodeset.css(".g-book-author b").text.strip.chomp(".")
|
17
|
+
book_hash[:genre] = create_genres(nodeset.css(".g-book-tag").text
|
18
|
+
.split(".").map!(&:strip).reject(&:empty?))
|
19
|
+
book_hash[:description] = nodeset.css(".g-book-description").text.strip
|
20
|
+
book_hash[:price] = details.sort.first
|
21
|
+
book_hash[:publisher] = details.last.chomp(".")
|
22
|
+
books_array << book_hash
|
23
|
+
end
|
24
|
+
|
25
|
+
NotableBooks2018::Book.create_from_collection(books_array)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.create_genres(css)
|
29
|
+
genre_data = css
|
30
|
+
genre_data.collect do |genre_name|
|
31
|
+
NotableBooks2018::Genre.find_or_create_by_name(genre_name)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "notable_books_2018/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "notable_books_2018"
|
8
|
+
spec.version = NotableBooks2018::VERSION
|
9
|
+
spec.authors = ["norawolf"]
|
10
|
+
spec.email = ["noraevogt@gmail.com"]
|
11
|
+
spec.files = Dir['lib/notable_books_2018/*.rb'] + Dir['bin/*'] + Dir['lib/environment.rb'] + Dir['[A-Z]*']
|
12
|
+
spec.summary = %q{A gem to browse the New York Times' Notable Books of 2018 list.}
|
13
|
+
spec.homepage = "https://github.com/norawolf/notable_books_2018.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "Set to 'http://rubygems.org'"
|
20
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
# spec.metadata["source_code_uri"] = "https://github.com/norawolf/notable_books_2018"
|
22
|
+
# else
|
23
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
# "public gem pushes."
|
25
|
+
# end
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = "bin"
|
33
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec"
|
39
|
+
spec.add_development_dependency "pry"
|
40
|
+
|
41
|
+
spec.add_dependency "nokogiri"
|
42
|
+
spec.add_dependency "paint"
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: notable_books_2018
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- norawolf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-01 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: nokogiri
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: paint
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- noraevogt@gmail.com
|
100
|
+
executables:
|
101
|
+
- console
|
102
|
+
- notable-books-2018
|
103
|
+
- setup
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".gitignore"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/notable-books-2018
|
116
|
+
- bin/setup
|
117
|
+
- lib/environment.rb
|
118
|
+
- lib/notable_books_2018/book.rb
|
119
|
+
- lib/notable_books_2018/cli.rb
|
120
|
+
- lib/notable_books_2018/genre.rb
|
121
|
+
- lib/notable_books_2018/scraper.rb
|
122
|
+
- lib/notable_books_2018/version.rb
|
123
|
+
- notable_books_2018.gemspec
|
124
|
+
homepage: https://github.com/norawolf/notable_books_2018.git
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubygems_version: 3.0.2
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: A gem to browse the New York Times' Notable Books of 2018 list.
|
147
|
+
test_files: []
|