Class: BarchartData::HighLowScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/barchart_data/high_low_scraper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHighLowScraper

Returns a new instance of HighLowScraper.



9
10
11
12
# File 'lib/barchart_data/high_low_scraper.rb', line 9

def initialize
  @url = { high_low: 'http://www.barchart.com/stocks/newhilo.php?dwm=d'}
  @agent = Mechanize.new
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



7
8
9
# File 'lib/barchart_data/high_low_scraper.rb', line 7

def agent
  @agent
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/barchart_data/high_low_scraper.rb', line 7

def url
  @url
end

Instance Method Details

#data_extractionObject



14
15
16
17
# File 'lib/barchart_data/high_low_scraper.rb', line 14

def data_extraction
  page = @agent.get(@url[:high_low]).search("div[id='divContent']")
  page.css("td[align='right']").to_a
end

#insert_data(high_low) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/barchart_data/high_low_scraper.rb', line 47

def insert_data high_low
  ::HighLow.create(one_month_high: high_low[0], one_month_low: high_low[1],
                 three_month_high: high_low[2], three_month_low: high_low[3],
                 six_month_high: high_low[4], six_month_low: high_low[5],
                 twelve_month_high: high_low[6], twelve_month_low: high_low[7],
                 ytd_high: high_low[8], ytd_low: high_low[8],
                 saved_on: Time.current )
end

#strip_highs_and_lows_from(links) ⇒ Object



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
# File 'lib/barchart_data/high_low_scraper.rb', line 19

def strip_highs_and_lows_from links
  strip_links = []
  regex_the_links = []
  overall_high_low = []

  links.each do |a|
    strip_links.push(a.to_s)
  end

  strip_links.slice!(0..3)
  strip_links.shift(6)
  strip_links.each { |e| regex_the_links.push(e.match(/>\d+</).to_s) }
   regex_the_links.map! do |e|
    if e.blank?
      e = ">0<"
    else
      e = e
    end
  end

  regex_the_links.map! { |e| e.gsub(/[><]/,"") }
  extracted_values = regex_the_links.each_slice(10).to_a
  extracted_values.each do |value|
    overall_high_low << value.first.to_i
  end
  overall_high_low
end