nyt-bestsellers 0.0.7 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c49821854cd98f8b38fe96244b6251c47a12b5361086a996fbfdd3db92c76f4e
4
- data.tar.gz: b46bdb781c2bcc3f63c5e407e45641876109292b6c38e99a077d721d743b7505
3
+ metadata.gz: 6a62b5da961841947fa45b6b7802932193d0e5b3b757eecb5cb9f79778395e65
4
+ data.tar.gz: 370cb594e99fce26fe4d9fc23e3a9c3c4855539203c384dfa67e7fab2337df68
5
5
  SHA512:
6
- metadata.gz: 8c6f47393860cdfa78c49995fa388d51e3476b37e728b0c46392272a28df04f031e84dfb0b9b3d262667bc225131d9b4103731d741bb70ba6ae1b91e7cbc7b68
7
- data.tar.gz: 3e2f70280e919c966f38d30659babc47f922d576d6fae504f98ded11a5c01554f1cc58dc5c7a9fff133cad375468dc001c7c083ddc1a1a3b518f7c2f0be52807
6
+ metadata.gz: 1d3e40274785e3428548193980372760a0cb51431cddd48446185e1b67bca58faa794ca88bb707f78ea5de0bd1278aa10b8e0cd38e2497d27ccc2e7401930662
7
+ data.tar.gz: f692331ff871353e2f742a661df88ea263008c34e226807fbfb00a58cf8bf351ec33dcc4430484e8009d85a0c7d4055231364915f4b2549f38799d6a3c278bf0
data/bin/nyt-bestsellers CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
-
2
+ require 'bundler/setup'
3
3
  require 'nyt_bestsellers'
4
4
 
5
5
  NYTBestsellers::CLI.new.call
@@ -1,4 +1,12 @@
1
+ require 'pry'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'colorize'
5
+ require 'mechanize'
6
+ require 'scraper'
7
+ require 'book'
8
+ require 'cli'
9
+ require 'genre'
10
+
1
11
  module NYTBestsellers
2
12
  end
3
-
4
- require 'environment'
data/lib/nytimes/book.rb CHANGED
@@ -1,29 +1,29 @@
1
- class NYTBestsellers::Book
1
+ module NYTBestsellers
2
+ class Book
3
+ attr_accessor :genre, :title, :author, :publisher, :wol, :summary
4
+ @@all = []
2
5
 
3
- attr_accessor :genre, :title, :author, :publisher, :wol, :summary
4
-
5
- @@all = []
6
-
7
- def initialize(hash = {})
8
- hash.each do |key, value|
9
- self.send("#{key}=", value)
10
- end
11
- @genre = NYTBestsellers::Genre.find_by_name(hash[:genre])
12
- if !wol.empty? || !summary.empty?
13
- @genre.books << self
14
- @@all << self
6
+ def initialize(hash = {})
7
+ hash.each do |key, value|
8
+ self.send("#{key}=", value)
9
+ end
10
+ @genre = NYTBestsellers::Genre.find_by_name(hash[:genre])
11
+ if !wol.empty? || !summary.empty?
12
+ @genre.books << self
13
+ @@all << self
14
+ end
15
15
  end
16
- end
17
16
 
18
- def self.all
19
- @@all
20
- end
17
+ def self.all
18
+ @@all
19
+ end
21
20
 
22
- def self.find_by_title(name)
23
- self.all.find do |book|
24
- if name == book.title
25
- book
21
+ def self.find_by_title(name)
22
+ self.all.find do |book|
23
+ if name == book.title
24
+ book
25
+ end
26
26
  end
27
- end
28
- end
27
+ end
28
+ end
29
29
  end
data/lib/nytimes/cli.rb CHANGED
@@ -1,118 +1,119 @@
1
- class NYTBestsellers::CLI
2
-
3
- def call
4
- NYTBestsellers::Scraper.make_genres
5
- NYTBestsellers::Scraper.make_books
6
- puts ""
7
- puts "Welcome to the New York Times Bestsellers List".blue.bold
8
- run
9
- end
1
+ module NYTBestsellers
2
+ class CLI
3
+ def call
4
+ NYTBestsellers::Scraper.make_genres
5
+ NYTBestsellers::Scraper.make_books
6
+ puts ""
7
+ puts "Welcome to the New York Times Bestsellers List".blue.bold
8
+ run
9
+ end
10
10
 
11
- def run
12
- puts "-------------------"
13
- puts ""
14
- puts "NYT Bestsellers - Week of #{NYTBestsellers::Scraper.get_date}".bold.blue
15
- puts "(Lists are published early online.)".bold.blue
16
- puts ""
17
- puts "Here are the categories:"
18
- puts ""
19
- display_genres
20
- puts ""
11
+ def run
12
+ puts "-------------------"
13
+ puts ""
14
+ puts "NYT Bestsellers - Week of #{NYTBestsellers::Scraper.get_date}".bold.blue
15
+ puts "(Lists are published early online.)".bold.blue
16
+ puts ""
17
+ puts "Here are the categories:"
18
+ puts ""
19
+ display_genres
20
+ puts ""
21
21
 
22
- response = ""
23
- while response != "exit"
24
- puts "Select a category by number or type " + "'exit'".light_red + " to close the CLI."
25
- response = gets.strip
26
- if (1..genre_count).to_a.include?(response.to_i)
27
- genre_books(response.to_i)
28
- elsif response == "exit"
29
- puts "Goodbye!~".bold.red
30
- puts ""
31
- exit
22
+ response = ""
23
+ while response != "exit"
24
+ puts "Select a category by number or type " + "'exit'".light_red + " to close the CLI."
25
+ response = gets.strip
26
+ if (1..genre_count).to_a.include?(response.to_i)
27
+ genre_books(response.to_i)
28
+ elsif response == "exit"
29
+ puts "Goodbye!~".bold.red
30
+ puts ""
31
+ exit
32
+ end
32
33
  end
33
34
  end
34
- end
35
-
36
- def genre_count
37
- NYTBestsellers::Genre.all.count
38
- end
39
35
 
40
- def display_genres
41
- NYTBestsellers::Genre.all.each_with_index do |genre, index|
42
- puts "#{index+1} - #{genre.name}"
36
+ def genre_count
37
+ NYTBestsellers::Genre.all.count
43
38
  end
44
- end
45
-
46
- def genre_books(response)
47
- genre = NYTBestsellers::Genre.find_by_num(response)
48
- puts ""
49
- puts "****----#{genre.name.upcase}----****".blue.bold
50
- puts ""
51
- puts " Rank^ Title"
52
- puts " ----- -----"
53
- genre.books.each_with_index do |book, index|
54
- rank = " #{index+1} "
55
- rank << " " if (0..8).include?(index)
56
39
 
57
- title = " #{book.title}"
58
- puts rank + title
40
+ def display_genres
41
+ NYTBestsellers::Genre.all.each_with_index do |genre, index|
42
+ puts "#{index+1} - #{genre.name}"
43
+ end
59
44
  end
60
45
 
61
- puts ""
62
- puts "(^refers to the current position on the bestseller's list)"
63
- puts ""
46
+ def genre_books(response)
47
+ genre = NYTBestsellers::Genre.find_by_num(response)
48
+ puts ""
49
+ puts "****----#{genre.name.upcase}----****".blue.bold
50
+ puts ""
51
+ puts " Rank^ Title"
52
+ puts " ----- -----"
53
+ genre.books.each_with_index do |book, index|
54
+ rank = " #{index+1} "
55
+ rank << " " if (0..8).include?(index)
56
+
57
+ title = " #{book.title}"
58
+ puts rank + title
59
+ end
64
60
 
65
- book_input = ""
66
- while book_input != "exit"
67
- puts "Select a book by rank number to get more info."
68
- puts "You may type" + " 'back'".light_red + " to return to the categories or " + "'exit'".light_red + " to close the CLI."
69
- book_input = gets.strip
70
61
  puts ""
71
- if (1..genre.books.count).include?(book_input.to_i)
72
- display_book_info(response, book_input)
73
- elsif book_input == "back"
74
- run
75
- elsif book_input == "exit"
76
- puts "Goodbye!~".bold.red
62
+ puts "(^refers to the current position on the bestseller's list)"
63
+ puts ""
64
+
65
+ book_input = ""
66
+ while book_input != "exit"
67
+ puts "Select a book by rank number to get more info."
68
+ puts "You may type" + " 'back'".light_red + " to return to the categories or " + "'exit'".light_red + " to close the CLI."
69
+ book_input = gets.strip
77
70
  puts ""
78
- exit
71
+ if (1..genre.books.count).include?(book_input.to_i)
72
+ display_book_info(response, book_input)
73
+ elsif book_input == "back"
74
+ run
75
+ elsif book_input == "exit"
76
+ puts "Goodbye!~".bold.red
77
+ puts ""
78
+ exit
79
+ end
79
80
  end
80
81
  end
81
- end
82
-
83
- def display_book_info(response, book_input)
84
- genre = NYTBestsellers::Genre.find_by_num(response)
85
82
 
86
- genre.books.each_with_index do |book, index|
87
- if book_input.to_i == index+1
88
- puts "****----#{book.title}----****".blue.bold
89
- puts ""
90
- puts "Weeks On Bestseller:".bold + " #{book.wol}"
91
- puts "Author:".bold + " #{book.author}"
92
- puts "Publisher:".bold + " #{book.publisher}"
93
- puts "Genre:".bold + " #{book.genre.name}"
94
- puts ""
95
- puts "---------Summary---------".bold
96
- puts "#{book.summary}"
97
- puts ""
83
+ def display_book_info(response, book_input)
84
+ genre = NYTBestsellers::Genre.find_by_num(response)
98
85
 
99
- input = ""
100
- while input != "exit"
101
- puts "Type" + " 'back'".light_red + "," + " 'menu',".light_red + " or" + " 'exit'".light_red + "."
102
- input = gets.strip
86
+ genre.books.each_with_index do |book, index|
87
+ if book_input.to_i == index+1
88
+ puts "****----#{book.title}----****".blue.bold
103
89
  puts ""
104
- if input == "back"
105
- genre_books(response)
106
- elsif input == "menu"
107
- run
108
- puts ""
109
- elsif input == "exit"
110
- puts "Goodbye!~".bold.red
90
+ puts "Weeks On Bestseller:".bold + " #{book.wol}"
91
+ puts "Author:".bold + " #{book.author}"
92
+ puts "Publisher:".bold + " #{book.publisher}"
93
+ puts "Genre:".bold + " #{book.genre.name}"
94
+ puts ""
95
+ puts "---------Summary---------".bold
96
+ puts "#{book.summary}"
97
+ puts ""
98
+
99
+ input = ""
100
+ while input != "exit"
101
+ puts "Type" + " 'back'".light_red + "," + " 'menu',".light_red + " or" + " 'exit'".light_red + "."
102
+ input = gets.strip
111
103
  puts ""
112
- exit
113
- end #second if
114
- end
115
- end #first if
116
- end #each
104
+ if input == "back"
105
+ genre_books(response)
106
+ elsif input == "menu"
107
+ run
108
+ puts ""
109
+ elsif input == "exit"
110
+ puts "Goodbye!~".bold.red
111
+ puts ""
112
+ exit
113
+ end #second if
114
+ end
115
+ end #first if
116
+ end #each
117
+ end
117
118
  end
118
119
  end
data/lib/nytimes/genre.rb CHANGED
@@ -1,31 +1,30 @@
1
- class NYTBestsellers::Genre
2
-
3
- attr_accessor :name, :url
4
-
5
- @@all = []
6
-
7
- def initialize(hash = {})
8
- hash.each do |key, value|
9
- self.send("#{key}=", value)
10
- end
11
- @@all << self
12
- @books = []
13
- end
14
-
15
- def self.all
16
- @@all
17
- end
18
-
19
- def books
20
- @books
21
- end
22
-
23
- def self.find_by_name(genre_name)
24
- self.all.find {|x| x.name == genre_name}
1
+ module NYTBestsellers
2
+ class Genre
3
+ attr_accessor :name, :url
4
+ @@all = []
5
+
6
+ def initialize(hash = {})
7
+ hash.each do |key, value|
8
+ self.send("#{key}=", value)
9
+ end
10
+ @@all << self
11
+ @books = []
12
+ end
13
+
14
+ def self.all
15
+ @@all
16
+ end
17
+
18
+ def books
19
+ @books
20
+ end
21
+
22
+ def self.find_by_name(genre_name)
23
+ self.all.find {|x| x.name == genre_name}
24
+ end
25
+
26
+ def self.find_by_num(num_input)
27
+ self.all[num_input.to_i-1]
28
+ end
25
29
  end
26
-
27
- def self.find_by_num(num_input)
28
- self.all[num_input.to_i-1]
29
- end
30
-
31
30
  end
@@ -1,43 +1,47 @@
1
- class NYTBestsellers::Scraper
2
-
3
- def self.get_page
4
- @@page ||= Mechanize.new.get("http://www.nytimes.com/books/best-sellers/")
5
- end
1
+ module NYTBestsellers
2
+ class Scraper
3
+ def self.get_page
4
+ @@page ||= Mechanize.new.get("http://www.nytimes.com/books/best-sellers/")
5
+ end
6
6
 
7
- def self.make_genres
8
- get_page.css("section.subcategory").each do |category|
9
- NYTBestsellers::Genre.new({
10
- name: category.css("a.subcategory-heading-link").text.strip,
11
- url: "http://www.nytimes.com#{category.css("a").attr("href").text}"
12
- })
7
+ def self.make_genres
8
+ get_page.css("section h2").each do |category|
9
+ NYTBestsellers::Genre.new({
10
+ name: category.css("a").text.strip,
11
+ url: "http://www.nytimes.com#{category.css("a").attr("href").text}"
12
+ })
13
+ end
13
14
  end
14
- end
15
15
 
16
- def self.get_genre_pages
17
- agent = Mechanize.new
18
- NYTBestsellers::Genre.all.collect do |genre|
19
- page = agent.get("#{genre.url}")
16
+ def self.get_genre_pages
17
+ agent = Mechanize.new
18
+ NYTBestsellers::Genre.all.collect do |genre|
19
+ page = agent.get("#{genre.url}")
20
+ end
20
21
  end
21
- end
22
22
 
23
- def self.make_books
24
- get_genre_pages.each do |page|
25
- books = page.css("div.book-body")
23
+ def self.make_books
24
+ get_genre_pages.each do |page|
25
+ book_genre = page.css("section h2")[0].text
26
+ books = page.css("ol")[0].css("li article a")
26
27
 
27
- books.each do |book|
28
- NYTBestsellers::Book.new({
29
- genre: page.css("h1").text.split(/\s-\s/)[0].strip, #genre
30
- title: book.css("h2.title").text.split.collect(&:capitalize).join(" "),
31
- author: book.css("p.author").text.split.delete_if{|x| x == "by"}.join(" "),
32
- publisher: book.css("p.publisher").text,
33
- wol: book.css("p.freshness").text,
34
- summary: book.css("p.description").text
35
- })
28
+ books.each do |book|
29
+ if book.css("p")[1] != nil && book.css("p")[1].attribute("itemprop").value == "author"
30
+ NYTBestsellers::Book.new({
31
+ genre: book_genre,
32
+ title: book.css("h3").text.split.collect(&:capitalize).join(" "),
33
+ author: book.css("p")[1].text.split.delete_if{|x| x == "by"}.join(" "),
34
+ publisher: book.css("p")[2].text,
35
+ wol: book.css("p")[0].text,
36
+ summary: book.css("p")[3].text
37
+ })
38
+ end
39
+ end
36
40
  end
37
41
  end
38
- end
39
42
 
40
- def self.get_date
41
- get_page.css("div.date-range").text.strip
43
+ def self.get_date
44
+ get_page.css("div.date-range").text.strip
45
+ end
42
46
  end
43
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyt-bestsellers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amy Back
@@ -106,14 +106,14 @@ dependencies:
106
106
  requirements:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 2.7.6
110
110
  type: :development
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
- version: '0'
116
+ version: 2.7.6
117
117
  description: A CLI based on the New York Times Bestsellers List. Provides list of
118
118
  top-selling books by genre.
119
119
  email: aback1227@gmail.com
@@ -123,13 +123,12 @@ extensions: []
123
123
  extra_rdoc_files: []
124
124
  files:
125
125
  - bin/nyt-bestsellers
126
- - config/environment.rb
127
126
  - lib/nyt_bestsellers.rb
128
127
  - lib/nytimes/book.rb
129
128
  - lib/nytimes/cli.rb
130
129
  - lib/nytimes/genre.rb
131
130
  - lib/nytimes/scraper.rb
132
- homepage: https://github.com/aback1227/nyt-bestsellers
131
+ homepage: https://github.com/aback223/nyt-bestsellers
133
132
  licenses:
134
133
  - MIT
135
134
  metadata: {}
@@ -1,10 +0,0 @@
1
- require 'pry'
2
- require 'nokogiri'
3
- require 'open-uri'
4
- require 'colorize'
5
- require 'mechanize'
6
-
7
- require 'scraper'
8
- require 'book'
9
- require 'cli'
10
- require 'genre'