Class: Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/sp500_analyzer/scraper.rb

Constant Summary collapse

URL =
"http://www.multpl.com/s-p-500-historical-prices/table/by-month"

Class Method Summary collapse

Class Method Details

.array_to_datapointsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sp500_analyzer/scraper.rb', line 43

def self.array_to_datapoints
  # new_data_points = []
  webpage_to_html.reverse.each_with_index do |row, index|
    price = row.css("td.right").text.gsub(",","").to_f
    date = row.css("td.left").text
    # binding.pry
    if price > 0.0 && !date.empty?
      dp = DataPoint.new
      dp.id = index
      dp.date = string_to_date(date)
      dp.price = price
      if DataPoint.all[index-1].price > 0.0
        dp.monthly_change = price - DataPoint.all[index-1].price
      end
      if !DataPoint.all[index-12].nil? && DataPoint.all[index-12].price > 0.0
        dp.yearly_change = price - DataPoint.all[index-12].price
      end
      # new_data_points << dp
    end
  end
  nil
end

.string_to_date(string) ⇒ Object



11
12
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
# File 'lib/sp500_analyzer/scraper.rb', line 11

def self.string_to_date(string)
  # month = 0
  data_array = string.split(/,|\s/).select{ |s| !s.strip.empty? }
  case data_array[0]
  when "Jan"
    month = 1
  when "Feb"
    month = 2
  when "Mar"
    month = 3
  when "Apr"
    month = 4
  when "May"
    month = 5
  when "Jun"
    month = 6
  when "Jul"
    month = 7
  when "Aug"
    month = 8
  when "Sep"
    month = 9
  when "Oct"
    month = 10
  when "Nov"
    month = 11
  when "Dec"
    month = 12
  end
  Date.new(data_array[2].to_i, month, data_array[1].to_i)
end

.webpage_to_htmlObject



7
8
9
# File 'lib/sp500_analyzer/scraper.rb', line 7

def self.webpage_to_html
  Nokogiri::HTML(open(URL)).css("table#datatable tr")
end