basic_yahoo_finance 0.5.2 → 0.6.0
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/CHANGELOG.md +6 -1
- data/README.md +23 -4
- data/lib/basic_yahoo_finance/version.rb +1 -1
- data/lib/basic_yahoo_finance.rb +41 -12
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e7e80847cd866e0bbf3de8dc8be42079bd72b8c771d03e2ef581b0d6929c2bc
|
|
4
|
+
data.tar.gz: 8fe353c9b4a494e55f157bfc3807432eaabe345a54963028b78330bda8f0a17e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 55f5dbfb66c2afa4175671e770310dbe41ef2c48fdd5e53761019b6367bf92aad4b62281c811776a9e6314adafa17fec5838e4771c186c0b5ecf4833d57acca2
|
|
7
|
+
data.tar.gz: c3fa55322e6155b1c0caaf39844170c3547482f8eda9fade825f16ccd2e54d064672eecff1551e0b1899226f813a3592da1165214dd522e897112294b4b48002
|
data/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [0.
|
|
8
|
+
## [0.6.0] - 2026-02-21
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Historical data query method (`history`) with support for single/multiple stocks and custom intervals by [@lucienLopez](https://github.com/lucienLopez)
|
|
13
|
+
- Include minitest and minitest-mock gems required by newer Ruby 3.4.x versions
|
|
9
14
|
|
|
10
15
|
### Changed
|
|
11
16
|
|
data/README.md
CHANGED
|
@@ -27,6 +27,8 @@ Or install it yourself as:
|
|
|
27
27
|
|
|
28
28
|
Instantiate the `Query` class and use the quotes method on it with either a single stock as a `String` and a single module as a `String` or by passing an `Array` of stocks such as show in the two examples below.
|
|
29
29
|
|
|
30
|
+
### Quotes
|
|
31
|
+
|
|
30
32
|
```ruby
|
|
31
33
|
# Query a stock, with the price module
|
|
32
34
|
|
|
@@ -53,20 +55,37 @@ data["AVEM"]["regularMarketPrice"]["raw"]
|
|
|
53
55
|
# 52.72
|
|
54
56
|
```
|
|
55
57
|
|
|
58
|
+
### Historical Data
|
|
59
|
+
|
|
60
|
+
Use the `history` method to retrieve historical price data for one or more stocks. It requires a symbol, a start date (`period1`) and an end date (`period2`) as Unix timestamps. An optional `interval` parameter defaults to `"1d"`.
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
query = BasicYahooFinance::Query.new
|
|
64
|
+
|
|
65
|
+
# Daily history for a single stock
|
|
66
|
+
data = query.history('AAPL', 1_700_000_000, 1_710_000_000)
|
|
67
|
+
|
|
68
|
+
# Weekly history
|
|
69
|
+
data = query.history('AAPL', 1_700_000_000, 1_710_000_000, '1wk')
|
|
70
|
+
|
|
71
|
+
# Multiple stocks
|
|
72
|
+
data = query.history(['AAPL', 'GOOG'], 1_700_000_000, 1_710_000_000)
|
|
73
|
+
```
|
|
74
|
+
|
|
56
75
|
## Development
|
|
57
76
|
|
|
58
77
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
59
78
|
|
|
60
79
|
MiniTest is used as test framework and can be run using:
|
|
61
80
|
|
|
62
|
-
$ rake test
|
|
81
|
+
$ bundle exec rake test
|
|
63
82
|
|
|
64
83
|
As linter RuboCop is used and can be run using:
|
|
65
84
|
|
|
66
|
-
$ rake rubocop
|
|
85
|
+
$ bundle exec rake rubocop
|
|
67
86
|
|
|
68
|
-
This gem is being developed with Ruby 3.
|
|
87
|
+
This gem is being developed with Ruby 3.4 but should be downward compatible with at least all supported versions of Ruby.
|
|
69
88
|
|
|
70
89
|
## Contributing
|
|
71
90
|
|
|
72
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/towards/basic_yahoo_finance. If you submit a pull request please make sure to write
|
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/towards/basic_yahoo_finance. If you submit a pull request please make sure to write test cases using MiniTest covering your changes and that there are no RuboCop offenses.
|
data/lib/basic_yahoo_finance.rb
CHANGED
|
@@ -23,27 +23,48 @@ module BasicYahooFinance
|
|
|
23
23
|
@crumb = fetch_crumb(@cookie)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def quotes(symbol)
|
|
26
|
+
def quotes(symbol)
|
|
27
27
|
hash_result = {}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
hash_result.store(sym, "HTTP Error")
|
|
28
|
+
|
|
29
|
+
with_http do |http|
|
|
30
|
+
make_symbols_array(symbol).each do |sym|
|
|
31
|
+
uri = URI("#{API_URL}/v7/finance/quote?symbols=#{sym}&crumb=#{@crumb}")
|
|
32
|
+
response = http.request(uri)
|
|
33
|
+
hash_result.store(sym, process_output(JSON.parse(response.body)))
|
|
34
|
+
rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
|
|
35
|
+
hash_result.store(sym, "HTTP Error")
|
|
36
|
+
end
|
|
38
37
|
end
|
|
39
38
|
|
|
40
|
-
|
|
39
|
+
hash_result
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def history(symbol, period1, period2, interval = "1d")
|
|
43
|
+
hash_result = {}
|
|
44
|
+
|
|
45
|
+
with_http do |http|
|
|
46
|
+
make_symbols_array(symbol).each do |sym|
|
|
47
|
+
uri = URI("#{API_URL}/v8/finance/chart/#{sym}?period1=#{period1}&period2=#{period2}&interval=#{interval}&crumb=#{@crumb}")
|
|
48
|
+
response = http.request(uri)
|
|
49
|
+
hash_result.store(sym, process_history_output(JSON.parse(response.body)))
|
|
50
|
+
rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
|
|
51
|
+
hash_result.store(sym, "HTTP Error")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
41
54
|
|
|
42
55
|
hash_result
|
|
43
56
|
end
|
|
44
57
|
|
|
45
58
|
private
|
|
46
59
|
|
|
60
|
+
def with_http
|
|
61
|
+
http = Net::HTTP::Persistent.new
|
|
62
|
+
http.override_headers["User-Agent"] = USER_AGENT
|
|
63
|
+
http.override_headers["Cookie"] = @cookie
|
|
64
|
+
yield(http)
|
|
65
|
+
http.shutdown
|
|
66
|
+
end
|
|
67
|
+
|
|
47
68
|
def fetch_cookie
|
|
48
69
|
http = Net::HTTP.get_response(URI(COOKIE_URL), { "Keep-Session-Cookies" => "true" })
|
|
49
70
|
cookies = http.get_fields("set-cookie")
|
|
@@ -72,5 +93,13 @@ module BasicYahooFinance
|
|
|
72
93
|
|
|
73
94
|
result
|
|
74
95
|
end
|
|
96
|
+
|
|
97
|
+
def process_history_output(json)
|
|
98
|
+
# Handle error from the API that the code isn't found
|
|
99
|
+
error = json.dig("chart", "error")
|
|
100
|
+
return error unless error.nil?
|
|
101
|
+
|
|
102
|
+
json
|
|
103
|
+
end
|
|
75
104
|
end
|
|
76
105
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: basic_yahoo_finance
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marc
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: net-http-persistent
|
|
@@ -61,14 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
61
61
|
requirements:
|
|
62
62
|
- - ">="
|
|
63
63
|
- !ruby/object:Gem::Version
|
|
64
|
-
version: 3.
|
|
64
|
+
version: 3.4.0
|
|
65
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
requirements:
|
|
67
67
|
- - ">="
|
|
68
68
|
- !ruby/object:Gem::Version
|
|
69
69
|
version: '0'
|
|
70
70
|
requirements: []
|
|
71
|
-
rubygems_version: 3.6.
|
|
71
|
+
rubygems_version: 3.6.9
|
|
72
72
|
specification_version: 4
|
|
73
73
|
summary: Basic Yahoo Finance API to query stock prices
|
|
74
74
|
test_files: []
|