Class: StockFolio::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/stockfolio/web.rb

Class Method Summary collapse

Class Method Details

.historical(symbol, startDate, endDate) ⇒ Object



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
# File 'lib/stockfolio/web.rb', line 18

def self.historical(symbol, startDate, endDate)
    s = startDate.strftime("%b+%d,+%Y")
    e = endDate.strftime("%b+%d,+%Y")
    url = "http://finance.google.com/finance/historical?q=#{symbol}&startdate=#{s}&enddate=#{e}&output=csv"

    resp = Net::HTTP.get_response(URI.parse(url))
    data = resp.body

    if data.empty?
        return nil
    end

    lines = data.split("\n")
    lines.shift
    items = []
    lines.each do |line|
        parts = line.split(",")
        i = 0
        item = {}
        item[:date]   = Date.parse(parts[0])
        item[:open]   = parts[1]
        item[:high]   = parts[2]
        item[:low]    = parts[3]
        item[:close]  = parts[4]
        item[:volume] = parts[5]
        items << item
    end

    items
end

.quote(symbol) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/stockfolio/web.rb', line 6

def self.quote(symbol)
    url = "http://finance.google.com/finance/info?client=ig&q=#{symbol}"

    resp = Net::HTTP.get_response(URI.parse(url))
    data = resp.body

    if data.empty?
        return nil
    end
    JSON.parse(data.slice(3, data.length).strip)
end

.search(term) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stockfolio/web.rb', line 49

def self.search(term)
    q = URI.escape(term, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
    url = "http://www.google.com/finance/match?matchtype=matchall&q=#{q}"
    resp = Net::HTTP.get_response(URI.parse(url))
    data = resp.body

    if data.empty?
        return nil
    end
    result = JSON.parse(data)
    result["matches"]
end