auto_service_cli 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 +9 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/auto_service_cli.gemspec +32 -0
- data/bin/auto_service +6 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/auto_service_cli/cli.rb +162 -0
- data/lib/auto_service_cli/constants.rb +4 -0
- data/lib/auto_service_cli/scraper.rb +163 -0
- data/lib/auto_service_cli/service_center.rb +68 -0
- data/lib/auto_service_cli/version.rb +3 -0
- data/lib/auto_service_cli.rb +19 -0
- data/spec.md +12 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 355c1cc539e58fa1e332f92898aa552df877911f
|
4
|
+
data.tar.gz: 5ece5d2c31b308bc8679059beb6e22069f9f317a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 26feeebf485c5edd172f49769f1e71e04621f71e44219ff9b32bb626a68d909c653ba9fb23c26aed4db5f7dd016c256c76365a7197fc33d1c523a6c88b7826f3
|
7
|
+
data.tar.gz: ac20eca2ca3f7894b75cb722bc8fa99ad9af6a146c8ed97eda6b441f739b92842124b9b042c500faea12dd94804dd2b0f753045b34c9e6663a1da2b77b63e208
|
data/.gitignore
ADDED
data/.rspec
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 aleksandr.rogachev1994@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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Aleksandr Rogachev
|
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,42 @@
|
|
1
|
+
# Auto Service CLI
|
2
|
+
|
3
|
+
This program provides CLI to find auto service centers near desired location and see details about each center. Also you can sort your list of centers by rating, distance, or name. Auto Service CLI asks user to type zip code and then finds 30 service centers near this position. Also the program allows to see all available details about each center.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'auto_service_cli'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install auto_service_cli
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use CLI type the following in command line:
|
24
|
+
|
25
|
+
$ auto_service
|
26
|
+
|
27
|
+
Follow the instructions in command line.
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
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).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/AleksandrRogachev94/auto_service_cli_gem. 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.
|
38
|
+
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
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,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'auto_service_cli/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "auto_service_cli"
|
8
|
+
spec.version = AutoServiceCLI::VERSION
|
9
|
+
spec.date = "2017-01-08"
|
10
|
+
spec.authors = ["Aleksandr Rogachev"]
|
11
|
+
spec.email = ["aleksandr.rogachev1994@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{CLI for searching of auto service centers near your location.}
|
14
|
+
spec.description = %q{This CLI allows to find service centers near desired zip code (with sorting) and see details about each center.}
|
15
|
+
spec.homepage = "https://github.com/AleksandrRogachev94/auto_service_cli"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "bin"
|
22
|
+
spec.executables << "auto_service"
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "nokogiri", "~> 1.7"
|
26
|
+
spec.add_dependency "colorize", "~> 0.8"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
32
|
+
end
|
data/bin/auto_service
ADDED
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "auto_service_cli"
|
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
|
+
Scr = AutoServiceCLI::Scraper
|
15
|
+
Cen = AutoServiceCLI::ServiceCenter
|
16
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
class AutoServiceCLI::CLI
|
2
|
+
|
3
|
+
attr_reader :zip, :cur_sort_type
|
4
|
+
attr_reader :scraper
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
self.cur_sort_type = "default"
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
puts "\n\tWelcome to auto service centers searching CLI!".green
|
12
|
+
prompt_zip
|
13
|
+
scrape_main_page
|
14
|
+
list_centers
|
15
|
+
menu
|
16
|
+
end
|
17
|
+
|
18
|
+
def prompt_zip
|
19
|
+
begin
|
20
|
+
puts "\tEnter you zip code:".magenta
|
21
|
+
zip = gets.strip
|
22
|
+
end until AutoServiceCLI::Scraper.valid_zip?(zip)
|
23
|
+
self.zip = zip
|
24
|
+
end
|
25
|
+
|
26
|
+
# Writers and Readers
|
27
|
+
|
28
|
+
def zip=(zip)
|
29
|
+
@zip = zip if AutoServiceCLI::Scraper.valid_zip?(zip)
|
30
|
+
end
|
31
|
+
|
32
|
+
def cur_sort_type=(type)
|
33
|
+
@cur_sort_type = type if AutoServiceCLI::Scraper.valid_sort_type?(type)
|
34
|
+
end
|
35
|
+
|
36
|
+
#-----------------------------------------------------------------------------------
|
37
|
+
# Menu methods
|
38
|
+
|
39
|
+
def menu
|
40
|
+
loop do
|
41
|
+
help_menu
|
42
|
+
puts "\nMake a choise:".magenta
|
43
|
+
input = gets.strip
|
44
|
+
case input
|
45
|
+
when "1"
|
46
|
+
list_centers
|
47
|
+
when "2"
|
48
|
+
sort
|
49
|
+
when "3"
|
50
|
+
get_details
|
51
|
+
when "4"
|
52
|
+
scrape_main_page
|
53
|
+
list_centers
|
54
|
+
when "10"
|
55
|
+
goodbye
|
56
|
+
break
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def list_centers
|
62
|
+
puts "-----------------------------------------------------------------------------"
|
63
|
+
puts "\tZIP: #{self.scraper.zip}\n\tSorting type: #{self.scraper.sort_type}\n".cyan
|
64
|
+
AutoServiceCLI::ServiceCenter.all.each.with_index(1) do |center,i|
|
65
|
+
print "#{i}.".cyan; print " #{center.name}"
|
66
|
+
puts center.rating.nil? ? "" : ", rating: #{center.rating}"
|
67
|
+
end
|
68
|
+
puts "-----------------------------------------------------------------------------"
|
69
|
+
end
|
70
|
+
|
71
|
+
def sort
|
72
|
+
help_sort
|
73
|
+
puts "\n\tChoose type of sorting:".magenta
|
74
|
+
input = gets.strip
|
75
|
+
case input
|
76
|
+
when "1"
|
77
|
+
self.cur_sort_type = "default"
|
78
|
+
when "2"
|
79
|
+
self.cur_sort_type = "distance"
|
80
|
+
when "3"
|
81
|
+
self.cur_sort_type = "average_rating"
|
82
|
+
when "4"
|
83
|
+
self.cur_sort_type = "name"
|
84
|
+
end
|
85
|
+
scrape_main_page
|
86
|
+
list_centers
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_details
|
90
|
+
puts "\n\tEnter the number of center:".green
|
91
|
+
input = gets.strip
|
92
|
+
|
93
|
+
if input.to_i >= 1 && input.to_i <= AutoServiceCLI::ServiceCenter.all.size
|
94
|
+
center = AutoServiceCLI::ServiceCenter.all[input.to_i - 1]
|
95
|
+
unless center.int_url.nil?
|
96
|
+
puts "\nObtaining data..."
|
97
|
+
self.scraper.scrape_center_details(center)
|
98
|
+
puts "Done"
|
99
|
+
end
|
100
|
+
|
101
|
+
puts "----------------------------------------------------------------------------------------------------------------"
|
102
|
+
puts "\n\t#{center.name.upcase}\n".red
|
103
|
+
puts "\tRating:\n#{center.rating}\n".cyan unless center.rating.nil?
|
104
|
+
puts "\tCategory:\n#{center.main_category}\n".cyan unless center.main_category.nil?
|
105
|
+
puts "\tAddress:\n#{center.address}\n".cyan unless center.address.nil?
|
106
|
+
puts"\tPhone number:\n#{center.phone_number}\n".cyan unless center.phone_number.nil?
|
107
|
+
|
108
|
+
unless center.int_url.nil?
|
109
|
+
puts "\tStatus:\n#{center.open_status}\n".cyan unless center.open_status.nil?
|
110
|
+
puts "\tSlogan:\n#{center.slogan}\n".cyan unless center.slogan.nil?
|
111
|
+
puts "\tWorking hours:\n#{center.working_hours}".cyan unless center.working_hours.nil?
|
112
|
+
puts "\tDescription:\n#{center.description}\n".cyan unless center.description.nil?
|
113
|
+
puts "\tServices:\n#{center.services}\n".cyan unless center.services.nil?
|
114
|
+
puts "\tBrands:\n#{center.brands}\n".cyan unless center.brands.nil?
|
115
|
+
puts "\tPayment methods:\n#{center.payment}\n".cyan unless center.payment.nil?
|
116
|
+
end
|
117
|
+
|
118
|
+
puts "\tSee more at:\n#{center.ext_url}\n".cyan unless center.ext_url.nil?
|
119
|
+
puts "----------------------------------------------------------------------------------------------------------------"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def scrape_main_page
|
124
|
+
puts "Obtaining data..."
|
125
|
+
AutoServiceCLI::ServiceCenter.reset_all!
|
126
|
+
case self.cur_sort_type
|
127
|
+
when "default"
|
128
|
+
@scraper = AutoServiceCLI::Scraper.new(self.zip, self.cur_sort_type)
|
129
|
+
when "distance"
|
130
|
+
@scraper = AutoServiceCLI::Scraper.new(self.zip, self.cur_sort_type)
|
131
|
+
when "average_rating"
|
132
|
+
@scraper = AutoServiceCLI::Scraper.new(self.zip, self.cur_sort_type)
|
133
|
+
when "name"
|
134
|
+
@scraper = AutoServiceCLI::Scraper.new(self.zip, self.cur_sort_type)
|
135
|
+
puts "name"
|
136
|
+
end
|
137
|
+
self.scraper.scrape_centers
|
138
|
+
puts "Done"
|
139
|
+
end
|
140
|
+
|
141
|
+
#-----------------------------------------------------------------------------------
|
142
|
+
# Helper methods
|
143
|
+
|
144
|
+
def help_menu
|
145
|
+
puts "\n1. List centers".green
|
146
|
+
puts "2. Change sorting type".green
|
147
|
+
puts "3. Show details about service center".green
|
148
|
+
puts "4. Reload centers".green
|
149
|
+
puts "10. Exit".green
|
150
|
+
end
|
151
|
+
|
152
|
+
def help_sort
|
153
|
+
puts "\n\t1. Default".green
|
154
|
+
puts "\t2. Sort by distance".green
|
155
|
+
puts "\t3. Sort by rating".green
|
156
|
+
puts "\t4. Sort by name".green
|
157
|
+
end
|
158
|
+
|
159
|
+
def goodbye
|
160
|
+
puts "\n\tThank you for using this application!".blue
|
161
|
+
end
|
162
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
class AutoServiceCLI::Scraper
|
2
|
+
attr_reader :doc, :sort_type, :zip
|
3
|
+
|
4
|
+
# Constructors and class methods.
|
5
|
+
|
6
|
+
def initialize(zip, sort_type = "default")
|
7
|
+
@sort_type = sort_type
|
8
|
+
@zip = zip
|
9
|
+
@doc = Nokogiri::HTML(open(get_url))
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.valid_zip?(zip)
|
13
|
+
zip.to_i > 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.valid_sort_type?(type)
|
17
|
+
type == "default" || type == "distance" || type == "average_rating" || type == "name"
|
18
|
+
end
|
19
|
+
|
20
|
+
# Helper methods
|
21
|
+
|
22
|
+
def get_url
|
23
|
+
raise InvalidURLData, "Invalid input data: zip - #{self.zip}, sort type - #{self.sort_type}" unless valid_url_data?
|
24
|
+
AutoServiceCLI::URL_TEMPLATE + "#{self.zip}&s=#{self.sort_type}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid_url_data?
|
28
|
+
self.class.valid_zip?(self.zip) && self.class.valid_sort_type?(self.sort_type)
|
29
|
+
end
|
30
|
+
|
31
|
+
def external_link?(url)
|
32
|
+
url.include?("www.") || url.include?("http://") || url.include?("https://")
|
33
|
+
end
|
34
|
+
|
35
|
+
#------------------------------------------------------------------------------------------------
|
36
|
+
#Scrape from the main page.
|
37
|
+
|
38
|
+
def scrape_centers
|
39
|
+
raise InvalidPage, "Invalid page!!!" if self.doc == nil
|
40
|
+
centers = self.doc.css(".organic .result .info")
|
41
|
+
|
42
|
+
centers.each do |center|
|
43
|
+
|
44
|
+
main_details = {}
|
45
|
+
obj_center = AutoServiceCLI::ServiceCenter.create(center.css(".n a").text)
|
46
|
+
|
47
|
+
url = scrape_internal_url(center); main_details[:int_url] = url unless url.nil?
|
48
|
+
url = scrape_external_url(center); main_details[:ext_url] = url unless url.nil?
|
49
|
+
rating = scrape_rating(center); main_details[:rating] = rating unless rating.nil?
|
50
|
+
address = scrape_address(center); main_details[:address] = address unless address.nil?
|
51
|
+
phone = scrape_phone_number(center); main_details[:phone_number] = phone unless phone.nil?
|
52
|
+
category = scrape_category(center); main_details[:main_category] = category unless category.nil?
|
53
|
+
|
54
|
+
obj_center.details_from_hash(main_details) # setting all details to center.
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
#------------------------------------------------------------------------------------------------
|
59
|
+
# Scrape center details from the internal url (if available)
|
60
|
+
|
61
|
+
def scrape_center_details(center)
|
62
|
+
raise InvalidPage, "Invalid page!!!" if !center.is_a? AutoServiceCLI::ServiceCenter || center.int_url.nil?
|
63
|
+
doc = Nokogiri::HTML(open(center.int_url))
|
64
|
+
raise InvalidPage "Invalid page!!!" if doc == nil
|
65
|
+
|
66
|
+
details = {}
|
67
|
+
|
68
|
+
status = scrape_status(doc); details[:open_status] = status unless status.nil?
|
69
|
+
slogan = scrape_slogan(doc); details[:slogan] = slogan unless slogan.nil?
|
70
|
+
hours = scrape_hours(doc); details[:working_hours] = hours unless hours.nil?
|
71
|
+
description = scrape_description(doc); details[:description] = description unless description.nil?
|
72
|
+
services = scrape_services(doc); details[:services] = services unless services.nil?
|
73
|
+
brands = scrape_brands(doc); details[:brands] = brands unless brands.nil?
|
74
|
+
payment = scrape_payment(doc); details[:payment] = payment unless payment.nil?
|
75
|
+
|
76
|
+
center.details_from_hash(details)
|
77
|
+
end
|
78
|
+
|
79
|
+
#---------------------------------------------------------------------------------------
|
80
|
+
# Scraping helpers
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def scrape_internal_url(center)
|
85
|
+
url = center.css(".n a").attr("href").value
|
86
|
+
(!url.empty? && !external_link?(url)) ? AutoServiceCLI::URL_BASE + url : nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def scrape_external_url(center)
|
90
|
+
url = center.css(".links a.track-visit-website")
|
91
|
+
url.empty? ? nil : url.attr("href").value
|
92
|
+
end
|
93
|
+
|
94
|
+
def scrape_rating(center)
|
95
|
+
rate = center.css(".info-primary .result-rating")
|
96
|
+
unless rate.empty?
|
97
|
+
attributes = rate.attr("class").value.split(" "); attributes.slice(1, attributes.size-1)
|
98
|
+
else
|
99
|
+
nil
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def scrape_address(center)
|
104
|
+
address_full = center.css(".info-primary .adr span").collect {|el| el.text.split(",").first}
|
105
|
+
address_full.flatten.empty? ? nil : address_full.join(", ")
|
106
|
+
end
|
107
|
+
|
108
|
+
def scrape_phone_number(center)
|
109
|
+
phone_number = center.css(".info-primary .phone.primary")
|
110
|
+
phone_number.empty? ? nil : phone_number.text
|
111
|
+
end
|
112
|
+
|
113
|
+
def scrape_category(center)
|
114
|
+
category = center.css(".info-secondary .categories a")
|
115
|
+
category.empty? ? nil : category.text
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def scrape_status(doc)
|
120
|
+
open_status = doc.css(".business-card-wrapper .status-text")
|
121
|
+
open_status.empty? ? nil : open_status.text
|
122
|
+
end
|
123
|
+
|
124
|
+
def scrape_slogan(doc)
|
125
|
+
slogan = doc.css("#business-details .slogan")
|
126
|
+
slogan.empty? ? nil : slogan.text
|
127
|
+
end
|
128
|
+
|
129
|
+
def scrape_hours(doc)
|
130
|
+
working_hours = ""
|
131
|
+
doc.css("#business-details .open-hours time").each do |time|
|
132
|
+
working_hours << time.css(".day-label").text
|
133
|
+
working_hours << ", " + time.css(".day-hours").text + "\n"
|
134
|
+
end
|
135
|
+
working_hours.empty? ? nil : working_hours
|
136
|
+
end
|
137
|
+
|
138
|
+
def scrape_description(doc)
|
139
|
+
description = doc.css("#business-details p.description").last
|
140
|
+
description = doc.css("#business-details dd.description").last if description.nil?
|
141
|
+
(!description.nil? && description.css("a").empty?) ? description.text : nil #exclude case of facebook and twitter links, without decription
|
142
|
+
end
|
143
|
+
|
144
|
+
def scrape_services(doc)
|
145
|
+
for_services = doc.css("#business-details dl")[1]
|
146
|
+
unless for_services.nil?
|
147
|
+
if for_services.css("dt").first.text == "Services/Products:"
|
148
|
+
services = for_services.css("dd").first.text
|
149
|
+
return services.empty? ? nil : services
|
150
|
+
end
|
151
|
+
end
|
152
|
+
nil
|
153
|
+
end
|
154
|
+
|
155
|
+
def scrape_brands(doc)
|
156
|
+
brands = doc.css("#business-details .brands")
|
157
|
+
brands.empty? ? nil : brands.text
|
158
|
+
end
|
159
|
+
|
160
|
+
def scrape_payment(doc)
|
161
|
+
payment = doc.css("#business-details .payment")
|
162
|
+
payment.empty? ? nil : payment.text
|
163
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class AutoServiceCLI::ServiceCenter
|
2
|
+
attr_accessor :name,
|
3
|
+
:int_url,
|
4
|
+
:ext_url,
|
5
|
+
:rating,
|
6
|
+
:main_category,
|
7
|
+
:address,
|
8
|
+
:phone_number,
|
9
|
+
:open_status,
|
10
|
+
:slogan,
|
11
|
+
:working_hours,
|
12
|
+
:description,
|
13
|
+
:services,
|
14
|
+
:brands,
|
15
|
+
:payment
|
16
|
+
@@all = []
|
17
|
+
|
18
|
+
# Constructors
|
19
|
+
|
20
|
+
def initialize(name)
|
21
|
+
@name = name
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.create(name)
|
25
|
+
self.new(name).tap { |center| center.save }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Instance methods.
|
29
|
+
|
30
|
+
def save
|
31
|
+
@@all << self
|
32
|
+
end
|
33
|
+
|
34
|
+
def details_from_hash(details)
|
35
|
+
details.each do |detail, value|
|
36
|
+
self.send("#{detail}=", value)
|
37
|
+
end
|
38
|
+
format_rating if details.include?(:rating)
|
39
|
+
end
|
40
|
+
|
41
|
+
def format_rating
|
42
|
+
case self.rating.first
|
43
|
+
when "one"
|
44
|
+
new_rating = "1"
|
45
|
+
when "two"
|
46
|
+
new_rating = "2"
|
47
|
+
when "three"
|
48
|
+
new_rating = "3"
|
49
|
+
when "four"
|
50
|
+
new_rating = "4"
|
51
|
+
when "five"
|
52
|
+
new_rating = "5"
|
53
|
+
end
|
54
|
+
|
55
|
+
new_rating << ".5" if self.rating.size == 2 && self.rating.last == "half"
|
56
|
+
self.rating = new_rating + " star(s)"
|
57
|
+
end
|
58
|
+
|
59
|
+
# Class mehods
|
60
|
+
|
61
|
+
def self.reset_all!
|
62
|
+
@@all.clear
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.all
|
66
|
+
@@all.dup.freeze
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "nokogiri"
|
3
|
+
require "colorize"
|
4
|
+
require "pry"
|
5
|
+
|
6
|
+
require_relative "auto_service_cli/constants"
|
7
|
+
require_relative "auto_service_cli/service_center"
|
8
|
+
require_relative "auto_service_cli/scraper"
|
9
|
+
require_relative "auto_service_cli/cli"
|
10
|
+
|
11
|
+
# Modules
|
12
|
+
|
13
|
+
module AutoServiceCLI
|
14
|
+
end
|
15
|
+
|
16
|
+
# Errors
|
17
|
+
|
18
|
+
class InvalidURLData < StandardError; end
|
19
|
+
class InvalidPage < StandardError; end
|
data/spec.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Specifications for the CLI Assessment
|
2
|
+
|
3
|
+
Specs:
|
4
|
+
- [x] Have a CLI for interfacing with the application
|
5
|
+
The CLI class was implemented to show users the interface. It has a #call method which is a starting point of CLI. A user should enter a zip code to get info about service centers near him. CLI class has menu method which gives to user an opportunity to make choices: list centers, choose sorting type, show details about center, reload centers, and exit.
|
6
|
+
|
7
|
+
- [x] Pull data from an external source
|
8
|
+
yellowpages.com is used to scrape the data about 30 service centers near entered zip code. Gem 'nokogiri' is used to scrape the data. The main page about service centers is used to scrape main data. When a user chooses to see the data about specific center, program scrapes data from the additional url (if available) obtained from the main page.
|
9
|
+
|
10
|
+
- [x] Implement both list and detail views
|
11
|
+
In CLI class #list_centers method was implemented. It shows 30 centers near entered zip code.
|
12
|
+
In CLI class #get_details method was implemented. It invokes the method to scrape the data from the specific url and lists the results.
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: auto_service_cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksandr Rogachev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colorize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.10'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.10'
|
97
|
+
description: This CLI allows to find service centers near desired zip code (with sorting)
|
98
|
+
and see details about each center.
|
99
|
+
email:
|
100
|
+
- aleksandr.rogachev1994@gmail.com
|
101
|
+
executables:
|
102
|
+
- auto_service
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- auto_service_cli.gemspec
|
114
|
+
- bin/auto_service
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- lib/auto_service_cli.rb
|
118
|
+
- lib/auto_service_cli/cli.rb
|
119
|
+
- lib/auto_service_cli/constants.rb
|
120
|
+
- lib/auto_service_cli/scraper.rb
|
121
|
+
- lib/auto_service_cli/service_center.rb
|
122
|
+
- lib/auto_service_cli/version.rb
|
123
|
+
- spec.md
|
124
|
+
homepage: https://github.com/AleksandrRogachev94/auto_service_cli
|
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
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.6.8
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: CLI for searching of auto service centers near your location.
|
148
|
+
test_files: []
|