yfinance 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81e27766934f17e64ea6fbc03920e4376b84e3fd
4
- data.tar.gz: ea7b06fa093fba8e361e3b27136af24a23a939f5
3
+ metadata.gz: ec8af8ce71dfa3e2005b7dc2463e9fed6bd74d0b
4
+ data.tar.gz: 6a7a22cf66e59f1a1bcc021f318a64d4287a480d
5
5
  SHA512:
6
- metadata.gz: ca7256197fe68faf2d97d826d4f3abe28ba96e62e9c2c73bab27877579e199864eeb0f0b4b4882d152e1a5203547db784757fcc9e36f0217f5f7d2c43ae57ee4
7
- data.tar.gz: b265f3edac262e1bc13b65c03c825d6cdd33f852d17188d2327e1dbe24cb1680ecc7fcbb43435646198d152e7e96041ff40ea4b23510feec439a3ebcd7950890
6
+ metadata.gz: b644597c8b49388e83fa5b6bef8d1d288717da78972325cca955af5d702c0e6df7dadbb711547414674131bb5929bf15570a714490550ef28a155c7d862cb271
7
+ data.tar.gz: 7fae35e0492dc94a19fc0be530046377c4d9190a53a9a939623a2d7e22d55a628a22e7ce4cf6735b317defb65a9c20f5cde517a3aeb397f1d3b9bcc393d7d59a
data/lib/yfinance.rb CHANGED
@@ -5,6 +5,8 @@ require 'json'
5
5
 
6
6
  class Yfinance
7
7
 
8
+ attr_reader :error_message
9
+
8
10
  class YahooFinanceException < Exception
9
11
  end
10
12
 
@@ -110,6 +112,7 @@ class Yfinance
110
112
  def initialize(max_concurrency: 20, memoize: false)
111
113
  @hydra = Typhoeus::Hydra.new(max_concurrency: max_concurrency)
112
114
  Typhoeus::Config.memoize = memoize
115
+ @error_message = "Symbol not found"
113
116
  end
114
117
 
115
118
  def run
@@ -144,8 +147,9 @@ class Yfinance
144
147
 
145
148
  def quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {})
146
149
  options[:raw] ||= true
147
- ret = []
150
+ ret = Hash[symbols_array.each_slice(1).to_a]
148
151
  symb_str = symbols_array.join("+")
152
+ columns_array.unshift(:symbol)
149
153
  columns = "#{columns_array.map {|col| COLUMNS[col] }.join('')}"
150
154
  url = "http://download.finance.yahoo.com/d/quotes.csv?s=#{URI.escape(symb_str)}&f=#{columns}"
151
155
  request = Typhoeus::Request.new(url, method: :get)
@@ -153,12 +157,13 @@ class Yfinance
153
157
  begin
154
158
  result = CSV.parse(response.body, headers: columns_array)
155
159
  result.each do |row|
156
- ret << row.to_hash#OpenStruct.new(row.to_hash)
160
+ h = row.to_hash
161
+ ret[h[:symbol]] = h
162
+ return ret
157
163
  end
158
164
  rescue
159
- ret = {name: "unknown symbol"}#OpenStruct.new(name: "unknown symbol")
165
+ return {error: @error_message}
160
166
  end
161
- return ret
162
167
  end
163
168
  @hydra.queue(request)
164
169
  @hydra.run
@@ -3,37 +3,51 @@ require 'test_helper.rb'
3
3
  class YfinanceTest < Minitest::Test
4
4
  def test_it_receives_quotes_for_valid_symbols
5
5
  yf = Yfinance.new
6
- res = yf.quotes(["AAPL", "TWTR"])
6
+ res = yf.quotes(["AAPL"])
7
7
  refute_empty res
8
8
  end
9
9
 
10
+ def test_it_returns_a_hash
11
+ yf = Yfinance.new
12
+ res = yf.quotes(["MSFT", "TEP.L"])
13
+ assert res.kind_of?(Hash), "It does NOT return a hash"
14
+ end
15
+
16
+ def test_it_always_adds_the_symbol_to_the_resulting_hash
17
+ yf = Yfinance.new
18
+ res = yf.quotes(["TWTR", "BARC.L"], [:last_trade_date, :close])
19
+ assert_equal res["TWTR"][:symbol], "TWTR"
20
+ end
21
+
10
22
  def test_it_work_for_an_invalid_symbol
11
23
  yf = Yfinance.new
12
24
  res = yf.quotes(["TWTR", "MADASDFASD", "AAPL"])
13
25
  assert_equal res.length, 3
14
26
  end
15
-
27
+ #
16
28
  def test_it_works_requesting_all_data_for_valid_symbols
17
29
  yf = Yfinance.new
18
30
  res = yf.quotes_all_info(["MSFT", "TWTR", "TEP.L"])
19
- assert_equal res.length, 3
31
+ # p res
32
+ assert_equal res.length, 3
33
+ assert_equal "MSFT", res["MSFT"][:symbol]
20
34
  end
21
-
35
+ #
22
36
  def test_it_works_requesting_all_data_for_one_invalid_symbol
23
37
  yf = Yfinance.new
24
- res = yf.quotes_all_info(["asdfasdfiajsodfi"])
25
- assert_equal res[:name], "unknown symbol"
38
+ res = yf.quotes_all_info(["asdf"])
39
+ assert_equal yf.error_message, res[:error]
26
40
  end
27
41
 
28
- def test_it_works_requesting_all_data_for_a_mix_of_valid_and_invalid__symbols
42
+ def test_it_returns_an_error_for_a_mix_of_valid_and_invalid__symbols
29
43
  yf = Yfinance.new
30
- res = yf.quotes_all_info(["AAPL", "asdfasdfiajsodfi", "MSFT"])
31
- assert_equal res[:name], "unknown symbol"
44
+ res = yf.quotes_all_info(["AAPL", "bcxza", "MSFT"])
45
+ assert_equal yf.error_message, res[:error]
32
46
  end
33
-
47
+ #
34
48
  def test_historical_data_works
35
49
  yf = Yfinance.new
36
- res = Proc.new { |resp| p resp }
50
+ res = Proc.new { |resp| assert resp["MSFT"].kind_of?(Array)}
37
51
  yf.add_historical_data_query(['MSFT'], '2013-01-01', Date.today, {period: :daily}, &res)
38
52
  yf.run
39
53
  end
data/yfinance.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'yfinance'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "yfinance"
8
- spec.version = "1.0.9"
8
+ spec.version = "1.0.10"
9
9
  spec.authors = ["Alexander Potrykus"]
10
10
  spec.email = ["alexander@risklog.io"]
11
11
  spec.summary = %q{Fetches Yahoo! Finance data using parallel HTTP requests.}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yfinance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Potrykus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-16 00:00:00.000000000 Z
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler