Class: NeweggScraperChsbr::DataGrabber

Inherits:
Object
  • Object
show all
Defined in:
lib/newegg_scraper_chsbr/data_grabber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDataGrabber

Returns a new instance of DataGrabber.



5
6
7
8
9
10
11
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 5

def initialize()
    shipping_price = getShipping
    prices = getPrice
    names = getNames
    descHash = getCpuDesc
    @cpus = makeCpus(prices, names, shipping_price, descHash)                     
end

Instance Attribute Details

#cpusObject (readonly)

Returns the value of attribute cpus.



2
3
4
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 2

def cpus
  @cpus
end

Instance Method Details

#getCpuDescObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 13

def getCpuDesc
    scraped = NeweggScraperChsbr::Scraper.new
    pages = []
    description = []
    css_next_link = scraped.xml_obj.css  ".item-title"
    css_next_link.each_with_index do | element, index |
        if index != 0
    
            if element.attributes["href"] != nil
                pages << element.attributes["href"].text
            end
        end
    end
    description = {}
    counter = 1
    puts "Getting data.. Please wait.\n"
    pages.each_with_index do | url, index_of_pages |
        description[index_of_pages] = {}
        scraped_info = NeweggScraperChsbr::Scraper.new(url)
        descriptors = scraped_info.xml_obj.css(".product-bullets")
        descriptors.children.children.each do |info |
            info.children.each do | more_info |
                
                description[index_of_pages][counter] = more_info.text
                counter += 1 
            end 
        end
        counter = 0
        
    end
    puts "Done!\n"
    description
    


end

#getNamesObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 91

def getNames
    scraped = NeweggScraperChsbr::Scraper.new
    names = []                                          

    css_name = scraped.xml_obj.css ".item-title"       

    css_name.each_with_index do | name, index |        

        index != 0 ? names << name.text : nil           
    end 
    names
end

#getPriceObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 75

def getPrice
    scraped_info = NeweggScraperChsbr::Scraper.new
    css_price = scraped_info.xml_obj.css ".price-current"    

    prices = []                                        

    css_price.each do | piece |                        
        temp_price = split_price piece                 
        if temp_price != nil
            temp_price.flatten!                             
            prices << "#{temp_price[0]}#{temp_price[1]}"    
        end

    end 
    prices
end

#getShippingObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 103

def getShipping
    scraped = NeweggScraperChsbr::Scraper.new
    shipping = []
    css_shipping = scraped.xml_obj.css ".price-ship"        

    css_shipping.each do | name |
        shipping << name.text
    end
    shipping
end

#isCoolerOrMB?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 72

def isCoolerOrMB?(name) 
    name.include?("Water") || name.include?("Air") || name.include?("Motherboard") || name.include?("AIO") || name.include?("FLY") || name.include?("Cooler") || name.include?("Block")
end

#makeCpus(prices, names, shipping, desc_hash) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 113

def makeCpus(prices, names, shipping, desc_hash)
                                          
                                                        
    cpus = []                                                    
    prices.each_with_index do | price, index |         

        if !isCoolerOrMB?(names[index])                
            cpus << NeweggScraperChsbr::Cpu.new(names[index], prices[index - 1], shipping[index], desc_hash[index])      
                                                        
        end 
    end 
    cpus
end

#split_price(html_element) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/newegg_scraper_chsbr/data_grabber.rb', line 49

def split_price(html_element)              
    counter = 0
    until counter == 100  
        if counter < 10                    
            if html_element.text.include?(".0#{counter.to_s}")  
                                                               
                price = html_element.text.split ".0#{counter.to_s}" 
                                                                    
                                                                    
                price.delete_at 1
                return price, ".0#{counter.to_s}"                   
            end
        elsif counter >= 10                                     
            if html_element.text.include?(".#{counter.to_s}") 

                price = (html_element.text.split ".#{counter.to_s}")
                price.delete_at 1 
                return price, (".#{counter.to_s}")
            end
        end
    counter += 1
    end
end