finance-ytd 1.0.1 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8239c74c4c15c331a158037e73045cae8a53e8fb
4
- data.tar.gz: 992b8249e6c66dba2a2f6435524e41b0a05d6c57
3
+ metadata.gz: 2cf0e7f6b6c9cbd7075a489f116657c1fefc0201
4
+ data.tar.gz: 0878f6362962b17f1205c876ca1a059f078a49b2
5
5
  SHA512:
6
- metadata.gz: b52c0d3bfd63e169643f074cae7db7f1af2324e5d1234e4eb3b09ae75ab139daa74fe309d6a979d354b7190f6d6ed3db7975d8797d20c017990a52aa28eb5f36
7
- data.tar.gz: 2148b0c15f9d6e4364c61a51cc45a7f65ad33636b57c4932bbb80e637706ea4c1acca91d63603de2c09ebe38859dda93e4a82f10b58f242caa532ad82c7a4227
6
+ metadata.gz: ee1fe33b9a31be399dde8a1ab0b0f87109faa824a871be488346ce1f2d002bae4c81892fc1c7970f57857708ba53b97b71a5b839984b36992f17e4e96590e4de
7
+ data.tar.gz: ba8725cba7cfb502696d1e40e29e77547e233f7fc4750fd4bec3b300182bde61aee9e15dbf492ff962e38da74074f3523b193ebf810cbb4dcaae9fc8f7e58685
data/lib/finance-ytd.rb CHANGED
@@ -8,7 +8,7 @@ end
8
8
 
9
9
  class FinanceYtd
10
10
  attr_accessor :css, :url, :symbol, :friendly_name, :css_text, :ytd_return
11
-
11
+
12
12
  def initialize(options)
13
13
  @symbol = options[:symbol]
14
14
  @friendly_name = options[:friendly_name]
@@ -20,11 +20,12 @@ class FinanceYtd
20
20
  page = Nokogiri::HTML(open(@url))
21
21
  @css_text = page.css(@css).text.gsub(/[$,]/, '')
22
22
  end
23
-
23
+
24
24
  def calculate
25
25
  @ytd_return = @css_text.tr('+%', '').to_f / 100.0
26
+ puts @ytd_return
26
27
  end
27
-
28
+
28
29
  def to_s
29
30
  ytd_return_rounded = (@ytd_return * 100.0).round(@decimal_places)
30
31
  ytd_return_rounded_string = ('%.' + @decimal_places.to_s + 'f') % ytd_return_rounded.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + '%'
@@ -34,7 +35,7 @@ class FinanceYtd
34
35
  s.bg_green
35
36
  else
36
37
  s.bg_red
37
- end
38
+ end
38
39
  end
39
40
  end
40
41
 
@@ -48,6 +49,26 @@ class CnnEtfFinanceYtd < FinanceYtd
48
49
  end
49
50
  end
50
51
 
52
+ class CnnMarketFinanceYtd < FinanceYtd
53
+ def initialize(options)
54
+ super(options)
55
+ @css = 'td.wsod_ytd > span'
56
+ @url = 'http://money.cnn.com/data/markets/' + @symbol + '/'
57
+ match()
58
+ calculate()
59
+ end
60
+ end
61
+
62
+ class BloombergFinanceYtd < FinanceYtd
63
+ def initialize(options)
64
+ super(options)
65
+ @css = 'div.detailed-quote > div > div > div:nth-child(5) > div.cell__value'
66
+ @url = 'http://www.bloomberg.com/quote/' + @symbol
67
+ match()
68
+ calculate()
69
+ end
70
+ end
71
+
51
72
  class ApmexGoldFinanceYtd < FinanceYtd
52
73
  def initialize(options)
53
74
  super(options)
@@ -62,7 +83,7 @@ class ApmexGoldFinanceYtd < FinanceYtd
62
83
  css_text_match = /(\d+\.\d+)/.match(@css_text)
63
84
  @price_this_year = css_text_match[1].gsub(',', '').to_f
64
85
  end
65
-
86
+
66
87
  def calculate
67
88
  @ytd_return = @price_this_year / @price_last_year - 1.0
68
89
  end
@@ -82,7 +103,7 @@ class ApmexSilverFinanceYtd < FinanceYtd
82
103
  css_text_match = /(\d+\.\d+)/.match(@css_text)
83
104
  @price_this_year = css_text_match[1].gsub(',', '').to_f
84
105
  end
85
-
106
+
86
107
  def calculate
87
108
  @ytd_return = @price_this_year / @price_last_year - 1.0
88
109
  end
@@ -8,25 +8,32 @@ class FinanceYtdTest < Minitest::Test
8
8
  assert f.ytd_return.is_a? Float
9
9
  end
10
10
 
11
+ def testCnnMarketFinanceYtd(symbol, friendly_name, decimal_places)
12
+ f = CnnMarketFinanceYtd.new({ :symbol => symbol, :friendly_name => friendly_name, :decimal_places => decimal_places })
13
+ puts f
14
+ assert f.ytd_return.is_a? Float
15
+ end
16
+
11
17
  def test_
12
18
  testCnnEtfFinanceYtd('VT', 'World Stocks', 0)
13
- testCnnEtfFinanceYtd('VTI', 'U.S. Stocks', 0)
14
- testCnnEtfFinanceYtd('VXUS', 'Foreign Stocks', 0)
15
- puts
16
19
 
17
20
  f = ApmexGoldFinanceYtd.new({ :symbol => 'Gold Spot', :friendly_name => 'Gold', :decimal_places => 0, :price_last_year => 1183.90 })
18
21
  puts f
19
22
  assert f.ytd_return.is_a? Float
20
-
21
23
  f = ApmexSilverFinanceYtd.new({ :symbol => 'Silver Spot', :friendly_name => 'Silver', :decimal_places => 0, :price_last_year => 15.56 })
22
24
  puts f
23
25
  assert f.ytd_return.is_a? Float
24
26
  puts
25
27
 
26
- testCnnEtfFinanceYtd('BWZ', 'Foreign Cash', 0)
28
+ testCnnEtfFinanceYtd('VTI', 'U.S. Stocks', 0)
29
+ testCnnEtfFinanceYtd('VXUS', 'Foreign Stocks', 0)
27
30
  testCnnEtfFinanceYtd('SGDM', 'Gold Miners', 0)
28
- testCnnEtfFinanceYtd('BND', 'U.S. Bonds', 0)
29
- testCnnEtfFinanceYtd('BNDX', 'Foreign Bonds', 0)
31
+ testCnnEtfFinanceYtd('BWZ', 'Foreign Cash', 0)
32
+ testCnnMarketFinanceYtd('dow', 'Dow', 0)
33
+ testCnnMarketFinanceYtd('sandp', 'S&P 500', 0)
34
+ f = BloombergFinanceYtd.new({ :symbol => 'USGG10YR:IND', :friendly_name => '10 Year', :decimal_places => 0 })
35
+ puts f
36
+ assert f.ytd_return.is_a? Float
30
37
  testCnnEtfFinanceYtd('XBT', 'Bitcoin', 0)
31
38
  puts
32
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finance-ytd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cscribn
@@ -39,9 +39,10 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Most financial websites show the daily change of assets. The purpose
42
- of this gem is to provide year-to-date change. ETFs, mutual funds, and Bitcoin
43
- are supported via money.cnn. Precious metals are supported using apmex.com, but
44
- require users to pass in last year's ending price.
42
+ of this gem is to provide year-to-date change. Markets, ETFs, mutual funds, and
43
+ Bitcoin are supported via money.cnn. Treasuries are supported via bloomberg.com. Precious
44
+ metals are supported using apmex.com, but require users to pass in last year's ending
45
+ price.
45
46
  email:
46
47
  executables: []
47
48
  extensions: []