book-deals 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34fdc31a4670da156d063ab13b38578277fee738
4
+ data.tar.gz: d02ca2fcd57e5e2d31da570456056adfaaaf2ed4
5
+ SHA512:
6
+ metadata.gz: 3b498d4c80f321024064fba484769c96575ec660fa505fa0888413d6df9371eebcb60a351801c8f1d9622135982d23357fe2b808bc444bd168e234861bb47caf
7
+ data.tar.gz: 1201f07ec6d7305156ff4b6c1371ff2b5057e6fbcea289b9c5413af5cb73ca64ff6e615fc12fda82916b65319c46bf72f6a62cf7efb5c2b80c7894dfbd8e71f1
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in book_deals.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Soumya Veer
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,39 @@
1
+ # BookDeals
2
+
3
+ Displays the book deals from Goodreads website.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+
11
+ gem 'book-deals'
12
+
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install book-deals
22
+
23
+ ## Usage
24
+
25
+ To execute in Command Line User Interface, enter the command:
26
+
27
+ ```ruby
28
+
29
+ $ book-deals
30
+
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/soumyaveer/book_deals-cli-gem.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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/bin/book-deals ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "book_deals"
5
+
6
+ BookDeals::Launcher.new.start
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "book_deals"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "book_deals/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "book-deals"
8
+ spec.version = BookDeals::VERSION
9
+ spec.authors = ["Soumya Veer"]
10
+ spec.email = ["veer.soumya@gmail.com"]
11
+
12
+ spec.summary = %q{Finds and displays book deals from goodreads.}
13
+ spec.description = %q{Finds and displays book deals from goodreads.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
22
+ f.match(%r{^(test|spec|features)/})
23
+ end
24
+ spec.bindir = "bin"
25
+ spec.executables = ["book-deals"]
26
+ spec.require_paths = ["lib", "lib/book_deals"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.15"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "pry"
32
+
33
+ spec.add_dependency "nokogiri"
34
+ spec.add_dependency "colorize"
35
+ end
@@ -0,0 +1,29 @@
1
+ module BookDeals
2
+ # book on the deal
3
+
4
+ class Book
5
+ attr_accessor :title, :author, :description, :deal, :purchase_url, :categories
6
+
7
+ def initialize(deal, title, author, purchase_url, description = nil)
8
+ self.title = title
9
+ self.author = author
10
+ self.purchase_url = purchase_url
11
+ self.description = description
12
+ self.categories = []
13
+ self.deal = deal
14
+ end
15
+
16
+ def to_s
17
+ <<-DETAILS.gsub /^ */, ''
18
+ #{"Book Title:".colorize(:yellow)} #{self.title} \n
19
+ #{"Author:".colorize(:yellow)} #{self.author} \n
20
+ #{"Description:".colorize(:yellow)} #{self.description} \n
21
+ #{"Deal Price:".colorize(:yellow)} #{self.deal.price} \n
22
+ #{"Original Price:".colorize(:yellow)} #{self.deal.original_price} \n
23
+ #{"Expires in:".colorize(:yellow)} #{self.deal.expires_in} \n
24
+ #{"This book can be purchased on:".colorize(:yellow)} #{self.purchase_url.colorize(:magenta)} \n
25
+ ==============================================================================
26
+ DETAILS
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ module BookDeals
2
+ # category is a collection of books
3
+
4
+ class Category
5
+ attr_accessor :name, :url, :books
6
+
7
+ def add_book(book)
8
+ self.books << book
9
+ book.categories << self
10
+ end
11
+
12
+ def initialize(name, url)
13
+ self.name = name
14
+ self.url = url
15
+ self.books = []
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module BookDeals
2
+ # manages program's input/output
3
+
4
+ class CLI
5
+ def ask
6
+ puts "\n"
7
+ self.say("Enter your selection:".colorize(:blue))
8
+ gets.chomp
9
+ end
10
+
11
+ def say(output)
12
+ puts "\n"
13
+ puts output
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module BookDeals
2
+ # current deal on the book
3
+
4
+ class Deal
5
+ attr_accessor :price, :original_price, :expires_in
6
+
7
+ def initialize(price, original_price = nil, expires_in = nil)
8
+ self.price = price
9
+ self.original_price = original_price
10
+ self.expires_in = expires_in
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,84 @@
1
+ module BookDeals
2
+ # manages user interaction with the scraper
3
+
4
+ class Launcher
5
+ attr_accessor :input_output, :scraper
6
+
7
+ def does_user_wants_to_quit?
8
+ self.input_output.say "Do you want to continue viewing deals? (y/n)".colorize(:blue)
9
+ answer = self.input_output.ask
10
+ %w(n no exit).include?(answer.downcase)
11
+ end
12
+
13
+ def display_menu
14
+ self.input_output.say "Select a Category to see the deals:".colorize(:blue)
15
+ @categories = self.scraper.scrape_categories_from_home_page
16
+ @categories.each_with_index do |category, index|
17
+ category_number = index + 1
18
+ self.input_output.say "#{category_number}. #{category.name} (Press #{category_number} to see #{category.name})"
19
+ end
20
+ end
21
+
22
+ def display_deals
23
+ self.input_output.say "Deals for Category - #{@selected_category.name}".colorize(:green)
24
+ self.input_output.say "--------------------------------------".colorize(:green)
25
+ @selected_category.books.each do |book|
26
+ self.input_output.say book.to_s
27
+ end
28
+
29
+ self.display_total_book_deals_in_category
30
+ end
31
+
32
+ def display_total_book_deals_in_category
33
+ self.input_output.say "Total #{@selected_category.books.count} deal/deals found for category #{@selected_category.name}.".colorize(:green)
34
+ self.input_output.say "=============================================================================="
35
+ end
36
+
37
+ def greet_user
38
+ self.input_output.say "Welcome to BookDeals!!".colorize(:green)
39
+ self.input_output.say "=======================".colorize(:green)
40
+ end
41
+
42
+ def good_bye
43
+ self.input_output.say "Thank you for visiting BookDeals. Hope to see you soon. Goodbye!".colorize(:green)
44
+ end
45
+
46
+ def initialize(io = CLI.new, scraper = Scraper.new)
47
+ self.input_output = io
48
+ self.scraper = scraper
49
+ @categories = []
50
+
51
+ @selected_category = nil
52
+ end
53
+
54
+ def start
55
+ loop do
56
+ self.greet_user
57
+ self.display_menu
58
+ self.select_category
59
+ break if self.does_user_wants_to_quit?
60
+ end
61
+ self.good_bye
62
+ end
63
+
64
+ def select_category
65
+ category_choice = self.input_output.ask
66
+ number_of_categories = @categories.count
67
+ if category_choice.to_i.between?(1, number_of_categories)
68
+ category = @categories[category_choice.to_i - 1]
69
+
70
+ @selected_category = self.scraper.scrape_deals_from_category_page(category)
71
+ self.display_deals
72
+ else
73
+ self.wrong_choice_selection(number_of_categories)
74
+ end
75
+ end
76
+
77
+ def wrong_choice_selection(number_of_categories)
78
+ self.input_output.say "The category selected is not present!".colorize(:red)
79
+ self.input_output.say "Please select from options 1 to #{number_of_categories}:".colorize(:red)
80
+ self.display_menu
81
+ self.select_category
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,60 @@
1
+ module BookDeals
2
+ # builds models from goodreads
3
+
4
+ class Scraper
5
+ HOME_URL = "https://www.goodreads.com"
6
+ BOOK_DEALS_URL = HOME_URL + "/ebook-deals"
7
+ HOME_PAGE_HTML_ELEMENTS = ".gr-dealsCategoryExplorer .u-unstyledListItem li a"
8
+ CATEGORY_PAGE_HTML_ELEMENTS = ".gr-deals .visibleWide .gr-dealsList__listItem"
9
+ DEALS_HTML_ELEMENT_TITLE = ".gr-book__title"
10
+ DEALS_PURCHASE_URL_HTML_ELEMENTS = DEALS_HTML_ELEMENT_TITLE + " a"
11
+ DEALS_HTML_ELEMENT_AUTHOR = ".gr-book__author a"
12
+ DEALS_HTML_ELEMENT_CATEGORIES = ".gr-deal__tag"
13
+ DEALS_HTML_ELEMENT_DESCRIPTION = ".gr-deal__blurb"
14
+ DEALS_HTML_ELEMENT_DEAL_PRICE = ".gr-deal__price__deal"
15
+ DEALS_HTML_ELEMENT_ORIGINAL_PRICE = ".gr-deal__price__original strike"
16
+ DEALS_HTML_ELEMENT_DATETIME = ".gr-deal__expirationDate"
17
+
18
+ def get_book_deals_page
19
+ Nokogiri::HTML(open(BOOK_DEALS_URL))
20
+ end
21
+
22
+ def scrape_categories_from_home_page
23
+ all_category_html_elements = self.get_book_deals_page.css(HOME_PAGE_HTML_ELEMENTS)
24
+ category_html_elements = select_public_category_html_elements(all_category_html_elements)
25
+
26
+ category_html_elements.map do |html_element|
27
+ category_name = html_element.text
28
+ category_url = HOME_URL + html_element.attr("href")
29
+ Category.new(category_name, category_url)
30
+ end
31
+ end
32
+
33
+ def scrape_deals_from_category_page(category)
34
+ deals_html_doc = Nokogiri::HTML(open(category.url))
35
+ deals_html_elements = deals_html_doc.css(CATEGORY_PAGE_HTML_ELEMENTS)
36
+
37
+ deals_html_elements.each do |html_element|
38
+ price = html_element.css(DEALS_HTML_ELEMENT_DEAL_PRICE).text
39
+ original_price = html_element.css(DEALS_HTML_ELEMENT_ORIGINAL_PRICE).text
40
+ expires_in = html_element.css(DEALS_HTML_ELEMENT_DATETIME).text.split(" ").drop(2).join(" ")
41
+
42
+ deal = Deal.new(price, original_price, expires_in)
43
+ title = html_element.css(DEALS_HTML_ELEMENT_TITLE).text
44
+ purchase_url = HOME_URL + html_element.css(DEALS_PURCHASE_URL_HTML_ELEMENTS).attr("href")
45
+ author = html_element.css(DEALS_HTML_ELEMENT_AUTHOR).text
46
+ description = html_element.css(DEALS_HTML_ELEMENT_DESCRIPTION).text
47
+
48
+ book = Book.new(deal, title, author, purchase_url, description)
49
+ category.add_book(book)
50
+ end
51
+ category
52
+ end
53
+
54
+ private
55
+
56
+ def select_public_category_html_elements(all_category_html_elements)
57
+ all_category_html_elements.reject {|html_element| html_element.text == "Recommended for You"}
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module BookDeals
2
+ VERSION = "0.1.0"
3
+ end
data/lib/book_deals.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "book_deals/version"
2
+ require_relative "./book_deals/cli"
3
+ require_relative "./book_deals/launcher"
4
+ require_relative "./book_deals/scraper"
5
+ require_relative "./book_deals/category"
6
+ require_relative "./book_deals/book"
7
+ require_relative "./book_deals/deal"
8
+ require "colorize"
9
+ require "nokogiri"
10
+ require "open-uri"
11
+
12
+ begin
13
+ require "pry"
14
+ rescue LoadError
15
+ # ignore exception as in production environment
16
+ end
data/spec.md ADDED
@@ -0,0 +1,21 @@
1
+ # Specifications for the CLI Assessment
2
+
3
+ Specs:
4
+ - [x] Have a CLI for interfacing with the application
5
+ ##### Executable in `bin` folder.
6
+ 1. created `book-deals` executable and called `Launcher` from it.
7
+
8
+ ##### `CLI` and `Launcher` interact with user and application
9
+ 1. In order to keep the code loosely coupled, I created two classes `CLI` and `Launcher`.
10
+ 2. `CLI` - interacts with the user and asks to enter a choice or displays the required output.
11
+ 3. `Launcher` - It is the starting point of the application. It interacts with `CLI` and the application and contains the logic from greeting the user to exiting the application.
12
+
13
+ - [x] Pull data from an external source
14
+ ##### `Scraper` to extract data from Goodreads website.
15
+ 1. `Scraper` extracts the data which is constructed into models by `Category`, `Book` and `Deal`.
16
+
17
+
18
+ - [x] Implement both list and detail views
19
+ ##### `Scraper` scrapes data of each category and the book deals related to that category.
20
+ 1. `Scraper` extracts the categories information from the `All Deals` page of Goodreads and displays to the user to select the category whose deals user wants to see.
21
+ 2. After user selects the category he/she wants to view, it displays all the book deals related to the category and also displays the total number of deals found for the category in the end.
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: book-deals
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Soumya Veer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-10 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.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: colorize
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: Finds and displays book deals from goodreads.
98
+ email:
99
+ - veer.soumya@gmail.com
100
+ executables:
101
+ - book-deals
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/book-deals
113
+ - bin/console
114
+ - bin/setup
115
+ - book-deals.gemspec
116
+ - lib/book_deals.rb
117
+ - lib/book_deals/book.rb
118
+ - lib/book_deals/category.rb
119
+ - lib/book_deals/cli.rb
120
+ - lib/book_deals/deal.rb
121
+ - lib/book_deals/launcher.rb
122
+ - lib/book_deals/scraper.rb
123
+ - lib/book_deals/version.rb
124
+ - spec.md
125
+ homepage: ''
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ - lib/book_deals
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.5.2
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Finds and displays book deals from goodreads.
150
+ test_files: []