basic_yahoo_finance 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +56 -9
- data/lib/basic_yahoo_finance/util.rb +2 -2
- data/lib/basic_yahoo_finance/version.rb +1 -1
- data/lib/basic_yahoo_finance.rb +31 -39
- metadata +23 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fd2727545a83f4bf87d3dbb4403e8799365f60908ff1228391760840470d223
|
4
|
+
data.tar.gz: 40b03fad16cbe8b296c4df148a1970189c544fe2cfd70e74c11143dc6de9ba06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd7a5678def3f9507779b624518181b1db91ebce666323da0abe5705e58d760b513c50edf3de735b4cbee8f103359df9f130de6f298ee76b67381de7c6aef844
|
7
|
+
data.tar.gz: a62df3b71f400a88ecfa659a524e977bb9c66969bd8d4044cd29e011b290af3e6bc6564a73fe2d197378076b2eafb2ce26a47a6eddc763f60cd071e40f518292
|
data/CHANGELOG.md
CHANGED
@@ -5,10 +5,29 @@ 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.5.0] - 2023-05-29
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Possibility to query multiple stocks at the same time
|
13
|
+
|
14
|
+
### Changed
|
15
|
+
|
16
|
+
- Implement persistent HTTP by [@daviddigital](https://github.com/daviddigital)
|
17
|
+
- generate_currency_symbols() return an array instead of a string
|
18
|
+
- Default to "price" module if nothing specificed in quotes()
|
19
|
+
- Update to use v10 version endpoint of API by [@daviddigital](https://github.com/daviddigital)
|
20
|
+
|
21
|
+
### Fixed
|
22
|
+
|
23
|
+
- MiniTest tests for full 100% code coverage
|
24
|
+
|
8
25
|
## [0.4.0] - 2023-05-23
|
9
26
|
|
10
27
|
### Added
|
11
28
|
|
29
|
+
- SimpleCov coverage badge
|
30
|
+
- Tests for failing quotes method due to HTTPError
|
12
31
|
- Test for quotes method raising OpenURI::HTTPError
|
13
32
|
- SimpleCov for code coverage
|
14
33
|
|
data/README.md
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
# BasicYahooFinance
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/basic_yahoo_finance.svg)](https://badge.fury.io/rb/basic_yahoo_finance)
|
4
|
+
[![Coverage](https://github.com/towards/basic_yahoo_finance/raw/main/coverage/coverage.svg)](https://github.com/towards/basic_yahoo_finance)
|
4
5
|
|
5
6
|
This is a simple Ruby gem to query the Yahoo! Finance API.
|
6
7
|
|
7
8
|
Most of the available gems available on rubygems.org for this purpose are either not maintained anymore or simply do not working due to outdated API code. The goal of this gem is to be as basic as possible, hence the name, and to "simply" work. It would be typcially used to get information such as ask/bid price, close price, volume, for one or more stocks by using its symbol. The data is returned raw directly from the API as JSON output.
|
8
9
|
|
9
|
-
Lastly, this gem also has the advantage of having no dependencies to any other third-party gems.
|
10
|
-
|
11
10
|
## Installation
|
12
11
|
|
13
12
|
Add this line to your application's Gemfile:
|
@@ -26,26 +25,74 @@ Or install it yourself as:
|
|
26
25
|
|
27
26
|
## Usage
|
28
27
|
|
29
|
-
Instantiate the `Query` class and use the quotes method on it with either
|
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.
|
30
29
|
|
31
30
|
```ruby
|
32
|
-
# Query
|
31
|
+
# Query a stock, with the price module
|
33
32
|
|
34
33
|
query = BasicYahooFinance::Query.new
|
34
|
+
data = query.quotes('AVEM', 'price')
|
35
|
+
|
36
|
+
# OR the module argument can be left empty as the price module is the default
|
35
37
|
data = query.quotes('AVEM')
|
36
38
|
|
37
39
|
# Query multiple stocks
|
38
40
|
|
39
41
|
query = BasicYahooFinance::Query.new
|
40
42
|
data = query.quotes(['AVDV', 'AVUV'])
|
43
|
+
|
41
44
|
```
|
42
45
|
|
43
|
-
This will return a `Hash` of
|
46
|
+
This will return a `Hash` of the stock information available under its symbol name as key in the `Hash`. For example:
|
44
47
|
|
45
48
|
```ruby
|
46
|
-
# Get stock's actual price
|
49
|
+
# Get stock's actual price as a formatted string
|
47
50
|
|
48
|
-
data['AVEM']['regularMarketPrice']
|
51
|
+
data['AVEM']['regularMarketPrice']['fmt']
|
52
|
+
# "52.72"
|
53
|
+
|
54
|
+
# OR the raw value
|
55
|
+
data["AVEM"]["regularMarketPrice"]["raw"]
|
56
|
+
# 52.72
|
57
|
+
```
|
58
|
+
|
59
|
+
## Modules
|
60
|
+
|
61
|
+
While `price` is a common module, its not the only one. A list of known modules include:
|
62
|
+
|
63
|
+
```
|
64
|
+
assetProfile
|
65
|
+
balanceSheetHistory
|
66
|
+
balanceSheetHistoryQuarterly
|
67
|
+
calendarEvents
|
68
|
+
cashflowStatementHistory
|
69
|
+
cashflowStatementHistoryQuarterly
|
70
|
+
defaultKeyStatistics
|
71
|
+
earnings
|
72
|
+
earningsHistory
|
73
|
+
earningsTrend
|
74
|
+
esgScores
|
75
|
+
financialData
|
76
|
+
fundOwnership
|
77
|
+
incomeStatementHistory
|
78
|
+
incomeStatementHistoryQuarterly
|
79
|
+
indexTrend
|
80
|
+
industryTrend
|
81
|
+
insiderHolders
|
82
|
+
insiderTransactions
|
83
|
+
institutionOwnership
|
84
|
+
majorDirectHolders
|
85
|
+
majorHoldersBreakdown
|
86
|
+
netSharePurchaseActivity
|
87
|
+
price
|
88
|
+
recommendationTrend
|
89
|
+
secFilings
|
90
|
+
sectorTrend
|
91
|
+
summaryDetail
|
92
|
+
summaryProfile
|
93
|
+
upgradeDowngradeHistory
|
94
|
+
pageviews
|
95
|
+
quotetype
|
49
96
|
```
|
50
97
|
|
51
98
|
## Development
|
@@ -60,8 +107,8 @@ As linter RuboCop is used and can be run using:
|
|
60
107
|
|
61
108
|
$ rake rubocop
|
62
109
|
|
63
|
-
This gem
|
110
|
+
This gem is being developed with Ruby 3.2 but should be downward compatible with at least all supported versions of Ruby.
|
64
111
|
|
65
112
|
## Contributing
|
66
113
|
|
67
|
-
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 a test case covering your changes.
|
114
|
+
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 a test case using MiniTest covering your changes.
|
@@ -13,7 +13,7 @@ module BasicYahooFinance
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
# Generate currency symbols based on array of currencies and base currency
|
16
|
+
# Generate currency symbols array based on array of currencies and base currency
|
17
17
|
def self.generate_currency_symbols(currencies, base_currency)
|
18
18
|
currency_symbols = []
|
19
19
|
currencies.each do |currency|
|
@@ -21,7 +21,7 @@ module BasicYahooFinance
|
|
21
21
|
|
22
22
|
currency_symbols.push("#{currency}#{base_currency}=X")
|
23
23
|
end
|
24
|
-
currency_symbols
|
24
|
+
currency_symbols
|
25
25
|
end
|
26
26
|
|
27
27
|
# Generate symobol for foreign exchange rate lookup
|
data/lib/basic_yahoo_finance.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "json"
|
4
|
-
require "
|
4
|
+
require "net/http/persistent"
|
5
|
+
require "net/http"
|
6
|
+
|
5
7
|
# require_relative "basic_yahoo_finance/cache"
|
6
8
|
require_relative "basic_yahoo_finance/util"
|
7
9
|
require_relative "basic_yahoo_finance/version"
|
@@ -9,58 +11,48 @@ require_relative "basic_yahoo_finance/version"
|
|
9
11
|
module BasicYahooFinance
|
10
12
|
# Class to send queries to Yahoo Finance API
|
11
13
|
class Query
|
12
|
-
API_URL = "https://
|
14
|
+
API_URL = "https://query2.finance.yahoo.com"
|
13
15
|
|
14
16
|
def initialize(cache_url = nil)
|
15
17
|
@cache_url = cache_url
|
16
18
|
end
|
17
19
|
|
18
|
-
def quotes(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
def quotes(symbol, mod = "price") # rubocop:disable Metrics/MethodLength
|
21
|
+
hash_result = {}
|
22
|
+
symbols = make_symbols_array(symbol)
|
23
|
+
http = Net::HTTP::Persistent.new
|
24
|
+
http.override_headers["User-Agent"] = "BYF/#{BasicYahooFinance::VERSION}"
|
25
|
+
symbols.each do |sym|
|
26
|
+
uri = URI("#{API_URL}/v10/finance/quoteSummary/#{sym}?modules=#{mod}")
|
27
|
+
response = http.request(uri)
|
28
|
+
hash_result.store(sym, process_output(JSON.parse(response.body), mod))
|
29
|
+
rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
|
30
|
+
hash_result.store(sym, "HTTP Error")
|
26
31
|
end
|
32
|
+
|
33
|
+
http.shutdown
|
34
|
+
|
35
|
+
hash_result
|
27
36
|
end
|
28
37
|
|
29
38
|
private
|
30
39
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
symbols.join(separator)
|
40
|
+
def make_symbols_array(symbol)
|
41
|
+
if symbol.instance_of?(Array)
|
42
|
+
symbol
|
43
|
+
else
|
44
|
+
[symbol]
|
37
45
|
end
|
38
46
|
end
|
39
47
|
|
40
|
-
def process_output(json)
|
41
|
-
|
42
|
-
|
43
|
-
hash[json["quoteResponse"]["result"][0]["symbol"]] = json["quoteResponse"]["result"].pop
|
44
|
-
elsif json["quoteResponse"]["result"].count > 1
|
45
|
-
json["quoteResponse"]["result"].each do |r|
|
46
|
-
hash[r["symbol"]] = r
|
47
|
-
end
|
48
|
-
end
|
49
|
-
# TODO: compare hash keys with symbol(s) requested and add symbols which had no results from API
|
50
|
-
hash
|
51
|
-
end
|
48
|
+
def process_output(json, mod)
|
49
|
+
# Handle error from the API that the code isn't found
|
50
|
+
return json["quoteSummary"]["error"] if json["quoteSummary"] && json["quoteSummary"]["error"]
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
hash[symbols] = {}
|
58
|
-
when Array
|
59
|
-
symbols.each do |s|
|
60
|
-
hash[s] = {}
|
61
|
-
end
|
62
|
-
end
|
63
|
-
hash
|
52
|
+
result = json["quoteSummary"]&.dig("result", 0)
|
53
|
+
return nil if result.nil?
|
54
|
+
|
55
|
+
result[mod]
|
64
56
|
end
|
65
57
|
end
|
66
58
|
end
|
metadata
CHANGED
@@ -1,15 +1,35 @@
|
|
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.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-http-persistent
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.0.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.0.2
|
13
33
|
- !ruby/object:Gem::Dependency
|
14
34
|
name: minitest-emoji
|
15
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
84
|
- !ruby/object:Gem::Version
|
65
85
|
version: '0'
|
66
86
|
requirements: []
|
67
|
-
rubygems_version: 3.4.
|
87
|
+
rubygems_version: 3.4.13
|
68
88
|
signing_key:
|
69
89
|
specification_version: 4
|
70
90
|
summary: Basic Yahoo Finance API to query stock prices
|