GoogleFinance 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/GoogleFinance-0.1.2.gem +0 -0
- data/lib/GoogleFinance/Company.rb +5 -5
- data/lib/GoogleFinance/HistoricalPrices.rb +27 -27
- data/lib/GoogleFinance/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b30307bb6718c9806a5fc4236d0e5e668f9b5bf3
|
4
|
+
data.tar.gz: b8875f943c3145c7396d661e05c8e2d28d394f2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7f76e676302cba8e7e63c929217b0f63de1539194c302a95c0612cf8d33bd412d05c9278d3390dd8897653225c420b63dbb2756f7c6e75fa33669895bf3ba8b
|
7
|
+
data.tar.gz: ca84f039b1d971532bb8a9240c5f91f6f4640d02165cad3316c7f3993f6affbca8a19ce40734f8d11d043ed9d4754b607f55fc5e7450f759fb4ca6f55e69a26e
|
Binary file
|
@@ -6,17 +6,17 @@ require 'GoogleFinance/HistoricalPrices'
|
|
6
6
|
module GoogleFinance
|
7
7
|
|
8
8
|
#
|
9
|
-
# Google Company class
|
9
|
+
# Google Company class
|
10
10
|
class Company
|
11
11
|
|
12
12
|
attr_accessor :ticker, :name, :employees, :prices
|
13
|
-
|
13
|
+
|
14
14
|
#
|
15
15
|
# get the ticker with the initialize
|
16
16
|
def initialize(ticker)
|
17
17
|
@ticker = ticker
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
#
|
21
21
|
# Get company name as in Google Finance
|
22
22
|
def name
|
@@ -37,7 +37,7 @@ module GoogleFinance
|
|
37
37
|
page = Nokogiri::HTML(open(name_url))
|
38
38
|
@employees = page.xpath("//div[@class='sfe-section']/table/tr[6]/td[@class='period']").inner_html.gsub(/\n|,|-/, "").to_i
|
39
39
|
@employees
|
40
|
-
|
40
|
+
|
41
41
|
end # employees
|
42
42
|
|
43
43
|
#
|
@@ -48,7 +48,7 @@ module GoogleFinance
|
|
48
48
|
|
49
49
|
end # get_historical_prices
|
50
50
|
|
51
|
-
|
51
|
+
|
52
52
|
end # class Company
|
53
53
|
|
54
54
|
end #module
|
@@ -1,28 +1,29 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
module GoogleFinance
|
4
|
-
|
4
|
+
|
5
5
|
class HistoricalPrices
|
6
6
|
|
7
7
|
# xpath positions on which Google Finance returns the prices
|
8
|
-
STOCK_OPEN
|
9
|
-
STOCK_HIGH
|
10
|
-
STOCK_LOW
|
11
|
-
STOCK_CLOSE
|
12
|
-
|
13
|
-
|
8
|
+
STOCK_OPEN = 0
|
9
|
+
STOCK_HIGH = 1
|
10
|
+
STOCK_LOW = 2
|
11
|
+
STOCK_CLOSE = 3
|
12
|
+
STOCK_VOLUME = 4
|
13
|
+
NUM_COLS = STOCK_VOLUME
|
14
|
+
NUM_ROWS = 100
|
14
15
|
|
15
16
|
attr_reader :stock_perf
|
16
17
|
|
17
18
|
#
|
18
|
-
#
|
19
|
+
#
|
19
20
|
def initialize(ticker, date_start, date_end)
|
20
|
-
|
21
|
+
|
21
22
|
@ticker = ticker
|
22
23
|
@date_start = Date.strptime(date_start, "%Y-%m-%d")
|
23
24
|
@date_end = Date.strptime(date_end, "%Y-%m-%d")
|
24
25
|
@stock_perf = {}
|
25
|
-
|
26
|
+
|
26
27
|
load_share_prices()
|
27
28
|
|
28
29
|
end # def initialize
|
@@ -30,7 +31,7 @@ module GoogleFinance
|
|
30
31
|
#
|
31
32
|
# Load the historical prices in the internal hash
|
32
33
|
def load_share_prices()
|
33
|
-
|
34
|
+
#
|
34
35
|
# build URL in the right format, in pages of 100 day quotes
|
35
36
|
historical_url = @@URL_HIS + @ticker + "&startdate=" + HistoricalPrices.fix_date(@date_start) +
|
36
37
|
"&enddate=" + HistoricalPrices.fix_date(@date_end) + "&num=#{NUM_ROWS}&start=0"
|
@@ -44,18 +45,17 @@ module GoogleFinance
|
|
44
45
|
# first page is already acquired, don't make another call
|
45
46
|
extract_prices_from_page(page)
|
46
47
|
for i in 1..num_calls
|
47
|
-
|
48
|
+
|
48
49
|
historical_url = @@URL_HIS + @ticker + "&startdate=" + HistoricalPrices.fix_date(@date_start) +
|
49
50
|
"&enddate=" + HistoricalPrices.fix_date(@date_end) + "&num=#{NUM_ROWS}&start=#{i * NUM_ROWS}"
|
50
51
|
page = Nokogiri::HTML(open(historical_url))
|
51
52
|
extract_prices_from_page(page)
|
52
53
|
end
|
53
|
-
|
54
|
-
|
54
|
+
|
55
55
|
end # load_share_prices
|
56
|
-
|
56
|
+
|
57
|
+
#
|
57
58
|
#
|
58
|
-
#
|
59
59
|
def extract_prices_from_page(page)
|
60
60
|
|
61
61
|
quote_dates = []
|
@@ -64,12 +64,12 @@ module GoogleFinance
|
|
64
64
|
quote_high = []
|
65
65
|
quote_low = []
|
66
66
|
quote_volume = []
|
67
|
-
|
67
|
+
|
68
68
|
# extract the dates
|
69
69
|
dates = page.xpath("//td[@class='lm']")
|
70
70
|
dates.each do |date|
|
71
71
|
quote_dates.push(HistoricalPrices.google_date_to_ruby(date.content.gsub("\n","")))
|
72
|
-
end
|
72
|
+
end
|
73
73
|
|
74
74
|
# get the stock prices
|
75
75
|
counter = 0
|
@@ -85,9 +85,9 @@ module GoogleFinance
|
|
85
85
|
elsif counter.modulo(GoogleFinance::HistoricalPrices::NUM_COLS) == GoogleFinance::HistoricalPrices::STOCK_CLOSE then
|
86
86
|
quote_close.push(val)
|
87
87
|
end
|
88
|
-
counter = counter + 1
|
88
|
+
counter = counter + 1
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
# get the volumnes
|
92
92
|
volumes = page.xpath("//td[@class='rgt rm']")
|
93
93
|
volumes.each do |volume|
|
@@ -95,7 +95,7 @@ module GoogleFinance
|
|
95
95
|
end
|
96
96
|
|
97
97
|
# we've extracted everything - build the share price hash
|
98
|
-
for i in 0..(quote_dates.length - 1)
|
98
|
+
for i in 0..(quote_dates.length - 1)
|
99
99
|
@stock_perf[quote_dates[i]] = [quote_open[i], quote_high[i], quote_low[i], quote_close[i], quote_volume[i]]
|
100
100
|
end
|
101
101
|
|
@@ -105,10 +105,10 @@ module GoogleFinance
|
|
105
105
|
#
|
106
106
|
# Input date should be in dd-mm-yyyy format
|
107
107
|
def self.fix_date(input_date)
|
108
|
-
|
108
|
+
|
109
109
|
months = %w{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec}
|
110
110
|
d = Date.strptime(input_date.to_s, "%Y-%m-%d") unless input_date.class == "Date"
|
111
|
-
|
111
|
+
|
112
112
|
# add '+' between all paramters, and for some reason, the ',' is equal to %2C but won't
|
113
113
|
# escape to it using addresable, whatever, it works this way
|
114
114
|
"#{months[d.mon - 1]}+#{d.day}%2C+#{d.year}"
|
@@ -118,16 +118,16 @@ module GoogleFinance
|
|
118
118
|
#
|
119
119
|
# Take the Gooel date format, bring it in to YYYY-mm-dd
|
120
120
|
def self.google_date_to_ruby(input_date)
|
121
|
-
|
121
|
+
|
122
122
|
dump, mon, day, year = input_date.split(/(...) ([0-9]*), (....)/)
|
123
|
-
|
123
|
+
|
124
124
|
# change month to numerical stuff
|
125
125
|
months = { "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,
|
126
126
|
"May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8,
|
127
127
|
"Sep" => 9, "Oct" => 10,"Nov" => 11, "Dec" => 12}
|
128
128
|
mon = months[mon]
|
129
|
-
mon = "0" + mon.to_s unless mon
|
130
|
-
day = "0" + day.to_s unless day.to_i
|
129
|
+
mon = "0" + mon.to_s unless mon >= 10
|
130
|
+
day = "0" + day.to_s unless day.to_i >= 10
|
131
131
|
|
132
132
|
return "#{year}-#{mon}-#{day}"
|
133
133
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: GoogleFinance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Brunet
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- ".gitignore"
|
78
78
|
- ".rspec"
|
79
79
|
- Gemfile
|
80
|
+
- GoogleFinance-0.1.2.gem
|
80
81
|
- GoogleFinance.gemspec
|
81
82
|
- LICENSE.txt
|
82
83
|
- README.md
|