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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2ec971c4ec5a157fbf72658bd0c49af97c4bb995a1e4fc3042622c0dd160632
4
- data.tar.gz: 1e3493618a5d9ffae9b79bdd50d8f74bd03d05b28e9ecfd09b8520c5edfbd291
3
+ metadata.gz: 3fd2727545a83f4bf87d3dbb4403e8799365f60908ff1228391760840470d223
4
+ data.tar.gz: 40b03fad16cbe8b296c4df148a1970189c544fe2cfd70e74c11143dc6de9ba06
5
5
  SHA512:
6
- metadata.gz: 5fe97817de863cc6b1b559064525c26ab6e2aaf2d9a388a35b9725068bfcf19efeb6925aa8bb41e1d5a9fb853186b3ebb184b1dd0fa8d8b5c3c0f32110542cec
7
- data.tar.gz: ece6f51f42cb4ce6aef20ba249d34df91e4e0c1fb4f4fcc2fc7edd6d1531c74478038fd75e657d9640615ea805593a0ee5959ec8675c7859c3bb74d1ddc0703e
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 one single symbol as `String` or by passing an `Array` of symbols such as show in the two examples below.
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 one single stock
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 hashes with each stock information available under its symbol name as key in the `Hash`. For example:
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 has been developed with Ruby 3.0 but should be downward compatible with at least all supported versions of Ruby.
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.join(",")
24
+ currency_symbols
25
25
  end
26
26
 
27
27
  # Generate symobol for foreign exchange rate lookup
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BasicYahooFinance
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
- require "open-uri"
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://query1.finance.yahoo.com"
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(symbols)
19
- symbols_value = generate_symbols_value(symbols)
20
- begin
21
- url = URI.parse("#{API_URL}/v6/finance/quote?symbols=#{symbols_value}")
22
- uri = URI.open(url, "User-Agent" => "BYF/#{BasicYahooFinance::VERSION}")
23
- process_output(JSON.parse(uri.read))
24
- rescue OpenURI::HTTPError
25
- empty_symbols_hash(symbols)
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 generate_symbols_value(symbols, separator = ",")
32
- case symbols
33
- when String
34
- symbols
35
- when Array
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
- hash = {}
42
- if json["quoteResponse"]["result"].count == 1
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
- def empty_symbols_hash(symbols)
54
- hash = {}
55
- case symbols
56
- when String
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.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-23 00:00:00.000000000 Z
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.10
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