amazon_book_ez 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ class AmazonBook::Book
2
+ attr_accessor :url, :name, :review, :author, :availability, :price, :publisher
3
+
4
+ @@all = []
5
+
6
+ def initialize(name, url)
7
+ @name = name
8
+ @url = url
9
+ @@all << self
10
+ end
11
+
12
+ def self.create_from_list(book_array)
13
+ book_array.each do |book|
14
+ self.new(book[:name],book[:url])
15
+ end
16
+ end
17
+
18
+ def add_attributes(book_hash)
19
+ @review = book_hash[:review]
20
+ @author = book_hash[:author]
21
+ @price = book_hash[:price]
22
+ @availability = book_hash[:availability]
23
+ @publisher = book_hash[:publisher]
24
+ end
25
+
26
+ def self.all
27
+ @@all
28
+ end
29
+
30
+ end
@@ -0,0 +1,66 @@
1
+ class AmazonBook::CLI
2
+
3
+ def start
4
+ puts "Welcome to Amazon Book Best Sellers Gem! Please enter the number of top Amazon books you'd like to see on the list:"
5
+
6
+ input = gets.strip
7
+ if input.to_i.is_a? Integer
8
+ puts "Printing a list of #{input} books:"
9
+ print_list(input)
10
+ elsif input.downcase == "exit"
11
+ puts "Thank you and hope to see you again!"
12
+ else
13
+ puts "Input invalid. Please input an integer or type exit."
14
+ end
15
+
16
+ puts "Would you like to learn more on any of the books in the list? Enter the number of the book, or exit."
17
+
18
+ input_1 = gets.strip
19
+ cli_attribute(input_1)
20
+ # if input_1.to_i.is_a? Integer && input_1.to_i < input.to_i
21
+ # print_attribute(input_1)
22
+ # elsif input.downcase == "exit"
23
+ # puts "Thank you and hope to see you again!"
24
+ # else
25
+ # puts "Input invalid. Please try again or type exit."
26
+ # end
27
+ end
28
+
29
+ def print_list(number)
30
+ AmazonBook::Scraper.scrape_list_page.each_with_index do |item, i|
31
+ if i+1 <= number.to_i
32
+ i_1 = i + 1
33
+ puts "#{i_1}. #{item[:name]}"
34
+ end
35
+ end
36
+
37
+ AmazonBook::Book.create_from_list(AmazonBook::Scraper.scrape_list_page)
38
+ end
39
+
40
+ def print_attribute(number) #below is what this should look like
41
+ url = AmazonBook::Scraper.scrape_list_page[number.to_i - 1][:url]
42
+ book = AmazonBook::Book.all.detect {|book| book.url == url}
43
+ AmazonBook::Scraper.scrape_book_page(url)
44
+
45
+ puts "Book Name: #{book.name}"
46
+ puts "Review: #{book.review}"
47
+ puts "Author: #{book.author}"
48
+ puts "Availability: #{book.availability}"
49
+ puts "Price: #{book.price}"
50
+ end
51
+
52
+ def cli_attribute(input_1)
53
+ while input_1.downcase != "exit"
54
+ if input_1.to_i < AmazonBook::Scraper.scrape_list_page.length
55
+ print_attribute(input_1)
56
+ puts "Would you like to see another book?"
57
+ input_1 = gets.strip
58
+ cli_attribute(input_1)
59
+ else
60
+ puts "Input invalid. Please try again or type exit."
61
+ end
62
+ end
63
+ puts "Thank you and hope to see you again!"
64
+ end
65
+
66
+ end
@@ -0,0 +1,32 @@
1
+ require 'open-uri'
2
+ class AmazonBook::Scraper
3
+
4
+ BASE_PATH = "./././fixtures/"
5
+
6
+ def self.scrape_list_page #returns a list of books
7
+ doc = Nokogiri::HTML(open("./././fixtures/booklist.html"))
8
+ book_list = [] #should return [{:name => "Harry Potter", :url => "www.harrypotter.com"},{:name => "Winnie the Pooh", :url => "www.pooh.com"}]
9
+ doc.css("#zg_centerListWrapper .zg_itemWrapper").each_with_index do |a,i|
10
+ book_list [i] = {}
11
+ book_list[i][:name] = "#{a.css("a div").children.attr('alt').value}"
12
+ book_list[i][:url] = a.css("a").attr('href').value
13
+ end
14
+
15
+ book_list
16
+ end
17
+
18
+ def self.scrape_book_page(url) #add attributes to book objects
19
+ #scrape book page's url
20
+ #should return {:name => Harry Potter, :author => JK Rowling, :review => 4.9 out of 5.0...}
21
+ book = AmazonBook::Book.all.detect {|book| book.url == url}
22
+ doc = Nokogiri::HTML(open(url))
23
+ book_hash = {}
24
+ book_hash[:author] = doc.css("#dp-container #centerCol #booksTitle #byline a").css(".contributorNameID").children.text
25
+ book_hash[:review] = doc.css("#dp-container #centerCol #averageCustomerReviews_feature_div #averageCustomerReviews span span").attr("title").value
26
+ book_hash[:price] = doc.css("#dp-container #centerCol #MediaMatrix #tmmSwatches ul li").css("a > span").text.split[1] #first price in the leftest box, usually the price of kindle version
27
+ book_hash[:availability] = doc.css(".en_US #rightCol #buybox #availability").css("span").children.text.gsub("\n", "").squeeze(" ")
28
+ # book_hash[:publisher]
29
+ book.add_attributes(book_hash)
30
+ book_hash
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module AmazonBook
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ module AmazonBook
2
+ end
3
+
4
+ require_relative "../config/environment"
data/notes.md ADDED
@@ -0,0 +1,11 @@
1
+ Ask the user how many books in the list she/he wants to see.
2
+ Show a list of top books.
3
+ 1. 12 Rules for Life: An Antidote to Chaos
4
+ 2. A wrinkle in Time (Time Quintet)
5
+ Which book do you want to learn more? (asks for number input)
6
+ Object Properties:
7
+ Name
8
+ Number of stars (Review)
9
+ Author
10
+ In Stock?/Out of Stock?
11
+ Price
data/rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/spec.md ADDED
@@ -0,0 +1,5 @@
1
+ Specs:
2
+
3
+ [X] Have a CLI for interfacing with the application
4
+ [X] Pull data from an external source
5
+ [X] Implement both list and detail views
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amazon_book_ez
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emily Zhao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-06 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: 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
+ description:
84
+ email:
85
+ - tong.zhao.92@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - README.md
93
+ - amazon_book.gemspec
94
+ - bin/amazon_book
95
+ - bin/console
96
+ - bin/setup
97
+ - config/environment.rb
98
+ - fixtures/booklist.html
99
+ - lib/AmazonBook/book.rb
100
+ - lib/AmazonBook/cli.rb
101
+ - lib/AmazonBook/scraper.rb
102
+ - lib/AmazonBook/version.rb
103
+ - lib/amazon_book.rb
104
+ - notes.md
105
+ - rakefile
106
+ - spec.md
107
+ homepage: https://github.com/emilyyz92/EmilyZhao-cli-app
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.7.6
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: This is a CLI gem that returns a list of top Amazon book best sellers and
131
+ the books' details.
132
+ test_files: []