newegg_scraper_chsbr 0.1.2 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5dcbc1bd32893df3dc6628024e9673075c32045bd52bde5d56b468c162012a2a
4
- data.tar.gz: 1e1ecf135f81b0c6dd163a8023ea6e9f85286edab0306c8a6028806d3e4804ad
3
+ metadata.gz: b6f2133128bd60c1e34d9d7ed98d65aa0bd5c305c804a4491738c22ea6b48931
4
+ data.tar.gz: f431eac85ce8616a3143537293e3b552a50056bb83b7afd84d6f0be525387b90
5
5
  SHA512:
6
- metadata.gz: e36856f48d56d2eef304700a1b4e1c41d14401d80ce42341e0d72c7eb3fc92758f016763a658928343b38b12e0d6bf0daac606a8f70737c61bc8edfb5b847eab
7
- data.tar.gz: 534721fa96cbaa73f4a664f6f99ee5c227b57abf63af64ef145481b925f60452c14f88cfa68b8cb6d73b8325a9c3f1a45cf3071047179f93846606338895b02e
6
+ metadata.gz: 32761b7f137118cd0b9ff915b745078f28ed74402ded5cfee1163196a6412e5904e53a5c8be73a8c818e637fa98884f483c714c3c62297a4c9b0dbded09d5fbd
7
+ data.tar.gz: deefa949cc5df1ea06cd1bf75a6977277b76573ac2ad9c1b5a2fc6fb01dc921bb7ed4163c9a1f1df5518f19cbaeddbc5df6b6afc7a7e58f643205bc42ef5f7f9
data/bin/newegg_scraper CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require_relative "../lib/environment.rb"
4
- NeweggScraperChsbr::CLI.new.start
4
+ NeweggScraperChsbr::CLI.new.call
data/lib/environment.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'open-uri'
2
2
  require 'nokogiri'
3
3
  require "bundler/setup"
4
+
5
+
4
6
  require_relative "./newegg_scraper_chsbr/version.rb"
5
7
  require_relative './newegg_scraper_chsbr/data_grabber.rb'
6
8
  require_relative './newegg_scraper_chsbr/scraper.rb'
@@ -8,4 +10,6 @@ require_relative './newegg_scraper_chsbr/user.rb'
8
10
  require_relative './newegg_scraper_chsbr/name.rb'
9
11
  require_relative './newegg_scraper_chsbr/price.rb'
10
12
  require_relative './newegg_scraper_chsbr/cli.rb'
11
- require_relative './newegg_scraper_chsbr/cpu.rb'
13
+ require_relative './newegg_scraper_chsbr/cpu.rb'
14
+ require_relative './newegg_scraper_chsbr/descriptors.rb'
15
+ require_relative './newegg_scraper_chsbr/shipping.rb'
@@ -4,28 +4,52 @@ class NeweggScraperChsbr::CLI
4
4
  def initialize
5
5
  NeweggScraperChsbr::DataGrabber.new
6
6
  end
7
- def start
8
- puts "Hello! I heard you were interested in purchasing some CPU's."
9
- puts "Please enter y to browse."
7
+ def call
8
+ puts "Were you interested in purchasing some CPU's?"
9
+ puts "Please enter y to do so."
10
10
  @counter = gets.strip
11
- while @counter == 'y' do
11
+ corr_input = control_bool
12
+ while corr_input
13
+ puts "Were you interested in purchasing some CPU's?"
14
+ puts "Please enter y to do so.\nPlease enter n or exit to quit."
15
+ @counter = gets.strip
16
+ corr_input = control_bool
17
+ end
18
+ while @counter == 'y'
19
+ get_price_range
12
20
  get_init_data_for_user
13
21
  NeweggScraperChsbr::Cpu.display_cpu(@user)
14
22
  @user.choose_cpu
15
23
  NeweggScraperChsbr::Cpu.display_cpu_with_extras(@user)
16
- puts "If you would like to continue please enter : #{@counter}"
24
+ puts "If you would like to restart the program please enter : #{@counter}.\nTo exit type n or exit."
17
25
  @counter = gets.strip
26
+ corr_input = control_bool
27
+ while corr_input
28
+ puts "Were you interested in purchasing some CPU's?"
29
+ puts "Please enter y to do so.\nPlease enter n or exit to quit."
30
+ @counter = gets.strip
31
+ corr_input = control_bool
32
+ end
33
+
18
34
  end
19
35
  puts "Thanks for using Chesbro's Scraper!"
20
36
  end
21
37
  def get_init_data_for_user
22
38
 
23
39
  if @counter == 'y'
24
- get_price_range
40
+
25
41
  puts "Are you searching for Intel or AMD?"
26
42
  puts "Please put i for Intel, or a for AMD."
27
43
  puts "Please put x for either."
28
44
  @cpu_make = gets.strip
45
+ not_found = cpu_make_bool
46
+ while not_found
47
+ puts "Are you searching for Intel or AMD?"
48
+ puts "Please put i for Intel, or a for AMD."
49
+ puts "Please put x for either."
50
+ @cpu_make = gets.strip
51
+ not_found = cpu_make_bool
52
+ end
29
53
  puts "\n\nYou want to search for (an) #{NeweggScraperChsbr::Cpu.cpu_maker(@cpu_make)} CPU."
30
54
  if @max_price.to_i != 0 && @min_price.to_i != 0
31
55
  puts "You entered a maximum price of $#{@max_price}, and a minimum price of $#{@min_price}."
@@ -40,28 +64,62 @@ class NeweggScraperChsbr::CLI
40
64
  end
41
65
  end
42
66
  def if_correct_info
43
- puts "\n IF this data is correct please enter y"
67
+ puts "\nIf you're satisfied with your selection, the computer will print\na list of CPUs that satisfy the criteria. Enter y to continue. Enter n to choose again."
44
68
  @counter = gets.strip
45
69
  if @counter == 'y'
46
70
  @user = NeweggScraperChsbr::User.new(@cpu_make, @min_price, @max_price)
47
71
  else
48
- get_init_data_for_user
72
+ @counter = 'y'
73
+ call
49
74
  end
50
75
  end
51
76
  def get_price_range
52
- puts "Hello! Would you like to set a budget? If no, set your maximum budget to 0"
77
+ puts "Welcome. Would you like to set a budget? If no, set your maximum budget to 0"
78
+ puts "Please enter the integer value of your maximum price."
79
+ puts "$500.50, please enter as 501, or 500 to stay below budget."
80
+ @max_price = gets.strip
81
+ while @max_price.to_i < 0 || @max_price.to_i > 10000 || @max_price.include?("$") || @max_price.include?(".") || @max_price == nil
82
+ puts "Welcome. Would you like to set a budget? If no, set your maximum budget to 0"
53
83
  puts "Please enter the integer value of your maximum price."
54
84
  puts "$500.50, please enter as 501, or 500 to stay below budget."
55
85
  @max_price = gets.strip
56
- if @max_price.to_i == 0
57
- @min_price = "0"
58
- else
86
+ end
87
+ if @max_price.to_i != 0
88
+ puts "Please enter the minimum you would spend on a CPU."
89
+ puts "Please use the same format as above."
90
+ @min_price = gets.strip
91
+
92
+ while @min_price.to_i <= 0 || @min_price.to_i >= @max_price.to_i || @min_price.include?("$") || @min_price.include?(".") || @min_price == nil
59
93
  puts "Please enter the minimum you would spend on a CPU."
60
94
  puts "Please use the same format as above."
61
95
  @min_price = gets.strip
62
96
  end
63
-
97
+ end
64
98
  end
99
+ def control_bool
100
+ if @counter.include?("y")
101
+ false
102
+ elsif @counter.include?("exit")
103
+ false
104
+ elsif @counter.include?("n")
105
+ false
106
+ else
107
+ true
108
+ end
109
+ end
110
+ def cpu_make_bool
111
+ if @cpu_make.include?("x")
112
+ @cpu_make = "x"
113
+ false
114
+ elsif @cpu_make.include?("a")
115
+ @cpu_make = "a"
116
+ false
117
+ elsif @cpu_make.include?("i")
118
+ @cpu_make = "i"
119
+ false
120
+ else
121
+ true
122
+ end
65
123
 
66
-
124
+ end
67
125
  end
@@ -5,8 +5,8 @@ class NeweggScraperChsbr::Cpu
5
5
  def initialize(name, price, shipping, desc)
6
6
  @name = NeweggScraperChsbr::Name.new(name)
7
7
  @price = NeweggScraperChsbr::Price.new(price)
8
- @desc = desc
9
- @shipping = shipping
8
+ @desc = NeweggScraperChsbr::Descriptors.new(desc)
9
+ @shipping = NeweggScraperChsbr::Shipping.new(shipping)
10
10
  @@all << self
11
11
  end
12
12
  def self.all
@@ -25,7 +25,7 @@ class NeweggScraperChsbr::Cpu
25
25
  counter = 1
26
26
  @@all.each do | computer |
27
27
  if user.max_price.to_i != 0 && user.min_price.to_i == 0
28
- binding.pry
28
+
29
29
 
30
30
  user.min_price = "1"
31
31
  end
@@ -54,12 +54,12 @@ class NeweggScraperChsbr::Cpu
54
54
  def self.display_cpu_with_extras(user)
55
55
 
56
56
  user.chosen_cpu.each do | chosen_cpu |
57
- puts "\n\n\n#{@@all[chosen_cpu - 1].name.name}\nHas a price of: #{@@all[chosen_cpu - 1].price.price}\nShipping: #{@@all[chosen_cpu - 1].shipping}\n\n Descriptive Points:\n"
57
+ puts "\n\n\n#{@@all[chosen_cpu - 1].name.name}\nHas a price of: #{@@all[chosen_cpu - 1].price.price}\nShipping: #{@@all[chosen_cpu - 1].shipping.price}\n\n Descriptive Points:\n"
58
58
  @@all[chosen_cpu - 1].printDesc
59
59
  end
60
60
  end
61
61
  def printDesc
62
- @desc.each do | key, value |
62
+ @desc.bullets.each do | key, value |
63
63
  puts " #{key + 1}: #{value}"
64
64
  end
65
65
  end
@@ -1,4 +1,3 @@
1
-
2
1
  class NeweggScraperChsbr::DataGrabber
3
2
  attr_reader :cpus
4
3
 
@@ -9,7 +8,7 @@ class NeweggScraperChsbr::DataGrabber
9
8
  names = getNames
10
9
  descHash = getCpuDesc
11
10
  @cpus = makeCpus(prices, names, shipping_price, descHash)
12
- end #end initialize
11
+ end
13
12
 
14
13
  def getCpuDesc
15
14
  scraped = NeweggScraperChsbr::Scraper.new
@@ -18,7 +17,10 @@ class NeweggScraperChsbr::DataGrabber
18
17
  css_next_link = scraped.xml_obj.css ".item-title"
19
18
  css_next_link.each_with_index do | element, index |
20
19
  if index != 0
21
- pages << element.attributes["href"].value
20
+
21
+ if element.attributes["href"] != nil
22
+ pages << element.attributes["href"].text
23
+ end
22
24
  end
23
25
  end
24
26
  description = {}
@@ -44,19 +46,19 @@ class NeweggScraperChsbr::DataGrabber
44
46
 
45
47
 
46
48
  end
47
- def split_price(html_element) # This method checks each individual text element, this is to make sure the prices are accurate.
49
+ def split_price(html_element)
48
50
  counter = 0
49
51
  until counter == 100
50
- if counter < 10 # If the counter is less than 10, is necessary because the computer doesn't know we are working with money.
51
- if html_element.text.include?(".0#{counter.to_s}") #If the text element includes any dollar ammount below .10 is a neccessary check.
52
- #Otherwise the computer would only check [.0.. .9], and we need it to check [.00.. .09]
53
- price = html_element.text.split ".0#{counter.to_s}" #Once we know it's the correct element we can assign it to the price variable.
54
- #This is only necessary to be able to delete the data we are splitting off.
55
- # (We are splitting anything after the cents amount because it was uneccessary.)
52
+ if counter < 10
53
+ if html_element.text.include?(".0#{counter.to_s}")
54
+
55
+ price = html_element.text.split ".0#{counter.to_s}"
56
+
57
+
56
58
  price.delete_at 1
57
- return price, ".0#{counter.to_s}" # Return the dollar amount, and the cents amount in an array.
59
+ return price, ".0#{counter.to_s}"
58
60
  end
59
- elsif counter >= 10 # This does the same as above, but for any number over 9.
61
+ elsif counter >= 10
60
62
  if html_element.text.include?(".#{counter.to_s}")
61
63
 
62
64
  price = (html_element.text.split ".#{counter.to_s}")
@@ -67,59 +69,35 @@ class NeweggScraperChsbr::DataGrabber
67
69
  counter += 1
68
70
  end
69
71
  end
70
- def isCoolerOrMB?(name) # This methods name means is it a cooler or a motherboard?
71
- name.include?("Water") || name.include?("Air") || name.include?("Motherboard") # this will return true if
72
- # any of the products include
73
- # words that describe motherboards and/or
74
- # cooling products.
72
+ def isCoolerOrMB?(name)
73
+ name.include?("Water") || name.include?("Air") || name.include?("Motherboard")
75
74
  end
76
75
  def getPrice
77
76
  scraped_info = NeweggScraperChsbr::Scraper.new
78
- css_price = scraped_info.xml_obj.css ".price-current" # This will set the variable equal to the
79
- # html that contains what I need to find the price.
77
+ css_price = scraped_info.xml_obj.css ".price-current"
80
78
 
81
- prices = [] # This will hold all of the prices in an array.
82
-
83
- css_price.each do | piece | # Going 1 level deep into the html obj that contains the price text element.
84
-
85
- temp_price = split_price piece # split_price will split the price, and return
86
- # two variables in an array. The first element of
87
- # the array will be the dollar amount.
88
- # The second element will be the cent ammount because
89
- # I deemed it easier to remove anything after the
90
- # floating point, it was the only pattern that was
91
- # consistent.
79
+ prices = []
92
80
 
81
+ css_price.each do | piece |
82
+ temp_price = split_price piece
93
83
  if temp_price != nil
94
- temp_price.flatten! # When returned by #split_price the dollar amount is
95
- # nested inside of another array. I use flatten!
96
- # to return the flattened array to temp_price.
97
-
98
- prices << "#{temp_price[0]}#{temp_price[1]}" # Here prices is being shoveled the
99
- # two elements, concatenated to create
100
- # a string similar to "$00.00"
84
+ temp_price.flatten!
85
+ prices << "#{temp_price[0]}#{temp_price[1]}"
101
86
  end
102
87
 
103
- end #end prices.each
88
+ end
104
89
  prices
105
90
  end
106
91
  def getNames
107
92
  scraped = NeweggScraperChsbr::Scraper.new
108
- names = [] # Names will hold the names of
109
- # objects found on the page.
93
+ names = []
110
94
 
111
- css_name = scraped.xml_obj.css ".item-title" # css_name will hold the html
112
- # object to be enumerated upon.
95
+ css_name = scraped.xml_obj.css ".item-title"
113
96
 
114
- css_name.each_with_index do | name, index | # css_name will be enumerated upon
115
- # to retrieve the strings.
97
+ css_name.each_with_index do | name, index |
116
98
 
117
- index != 0 ? names << name.text : nil # I have to skip the first name
118
- # because it does not have a price.
119
- # After I skip the first name
120
- # I will shovel the text from the
121
- # html object into the names array.
122
- end #end names.each_with_index
99
+ index != 0 ? names << name.text : nil
100
+ end
123
101
  names
124
102
  end
125
103
  def getShipping
@@ -136,23 +114,13 @@ class NeweggScraperChsbr::DataGrabber
136
114
 
137
115
 
138
116
  cpus = []
139
- prices.each_with_index do | price, index | # I will then enumerate on
140
- # each of the prices with their
141
- # index so that the enumation can
142
- # access the names array at the
143
- # same time.
117
+ prices.each_with_index do | price, index |
144
118
 
145
- if !isCoolerOrMB?(names[index]) # Here the program check if
146
- # the name is a cooler
147
- # or motherboard because
148
- # there is a price associated
149
- # with the coolers on the
150
- # website.
151
-
119
+ if !isCoolerOrMB?(names[index])
152
120
  cpus << NeweggScraperChsbr::Cpu.new(names[index], price, shipping[index], desc_hash[index])
153
121
 
154
- end #end if
155
- end #end prices.each_with_index
122
+ end
123
+ end
156
124
  cpus
157
125
  end
158
126
  end
@@ -1,3 +1,6 @@
1
1
  class NeweggScraperChsbr::Descriptors
2
-
2
+ attr_reader :bullets
3
+ def initialize(hash)
4
+ @bullets = hash
5
+ end
3
6
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class NeweggScraperChsbr::Name
4
4
  attr_accessor :name
5
- # @@all = []
5
+
6
6
  def initialize(name)
7
7
  @name = name
8
8
  end
@@ -4,7 +4,7 @@ class NeweggScraperChsbr::Scraper
4
4
 
5
5
  URL = "https://www.newegg.com/p/pl?d=cpu"
6
6
  def initialize(url = URL)
7
- @xml_obj = Nokogiri::HTML(URI.open(url)) # This opens the URL with open-uri, then parses it as an XML Object
7
+ @xml_obj = Nokogiri::HTML(URI.open(url))
8
8
  end
9
9
 
10
10
 
@@ -0,0 +1,6 @@
1
+ class NeweggScraperChsbr::Shipping
2
+ attr_reader :price
3
+ def initialize(shipping_price)
4
+ @price = shipping_price
5
+ end
6
+ end
@@ -8,13 +8,22 @@ class NeweggScraperChsbr::User
8
8
  @chosen_cpu = []
9
9
  end
10
10
  def choose_cpu
11
- puts "Please enter the number of the CPU you'd like to choose."
12
- chosen = gets.strip.to_i
13
- @chosen_cpu << chosen
14
- puts "Would you like to add another CPU?\nEnter y to do so."
15
- if gets.strip == 'y'
11
+ puts "Please enter the number of the CPU you'd like to see more about."
12
+ chosen = gets.strip
13
+ while !is_numeric?(chosen)
14
+ puts "Please enter the number of the CPU you'd like to see more about."
15
+ chosen = gets.strip
16
+ end
17
+ @chosen_cpu << chosen.to_i
18
+ puts "Would you like to see more about another CPU?\nEnter y to do so.\nEnter n to see details about the chosen CPU(s)"
19
+ input = gets.strip
20
+
21
+ if input == 'y'
16
22
  choose_cpu
17
23
  end
18
24
 
19
25
  end
26
+ def is_numeric?(obj)
27
+ obj.match(/\A[^+-]?\d+?(\^.\d+)?\Z/) == nil ? false : true
28
+ end
20
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NeweggScraperChsbr
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: newegg_scraper_chsbr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lrd134
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-16 00:00:00.000000000 Z
11
+ date: 2021-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.11.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.11.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: open-uri
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '0.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '0.1'
41
41
  description: To scrape CPU's off of Newegg and display them nicely.
42
42
  email:
43
43
  - larryc3200@gmail.com
@@ -55,6 +55,7 @@ files:
55
55
  - lib/newegg_scraper_chsbr/name.rb
56
56
  - lib/newegg_scraper_chsbr/price.rb
57
57
  - lib/newegg_scraper_chsbr/scraper.rb
58
+ - lib/newegg_scraper_chsbr/shipping.rb
58
59
  - lib/newegg_scraper_chsbr/user.rb
59
60
  - lib/newegg_scraper_chsbr/version.rb
60
61
  homepage: https://github.com/Lrd134/aa_phase_1_proj