basic_yahoo_finance 0.5.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d234393bfb2f323b2dec6d4b6b743e13033c2fd583c0f91025456eb4cdafa4d
4
- data.tar.gz: d8a058f85a0cbbc8a3f06e12d35eb331d388c20a71d15130fd1ccf50c269aa4b
3
+ metadata.gz: 4e7e80847cd866e0bbf3de8dc8be42079bd72b8c771d03e2ef581b0d6929c2bc
4
+ data.tar.gz: 8fe353c9b4a494e55f157bfc3807432eaabe345a54963028b78330bda8f0a17e
5
5
  SHA512:
6
- metadata.gz: 33df5faf5eb649bad19503d59fb414e193bca49bfb1beb84caf2b8f31a5bc57d010c801e03cd7c4333aa2a704f48816b4cda6435f326234af602da98dea09f43
7
- data.tar.gz: 2904cf1af64fc4ff92a910c9614b5b826ecf29d65e73c073465d811a2a0452fcf00ce5f5d2c27f6909730c122e64cba1a0b62e71e2fee14a9f7a43870dcd5261
6
+ metadata.gz: 55f5dbfb66c2afa4175671e770310dbe41ef2c48fdd5e53761019b6367bf92aad4b62281c811776a9e6314adafa17fec5838e4771c186c0b5ecf4833d57acca2
7
+ data.tar.gz: c3fa55322e6155b1c0caaf39844170c3547482f8eda9fade825f16ccd2e54d064672eecff1551e0b1899226f813a3592da1165214dd522e897112294b4b48002
data/CHANGELOG.md CHANGED
@@ -5,6 +5,25 @@ 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.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
14
+
15
+ ### Changed
16
+
17
+ - Update gem dependencies and Ruby version
18
+
19
+ ### Fixed
20
+
21
+ - Broken API query generating HTTP 404 Not Found error
22
+
23
+ ### Removed
24
+
25
+ - Possibility to use modules in query
26
+
8
27
  ## [0.5.1] - 2023-07-14
9
28
 
10
29
  ### Changed
data/README.md CHANGED
@@ -27,13 +27,12 @@ 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
 
33
35
  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
37
36
  data = query.quotes('AVEM')
38
37
 
39
38
  # Query multiple stocks
@@ -56,43 +55,21 @@ data["AVEM"]["regularMarketPrice"]["raw"]
56
55
  # 52.72
57
56
  ```
58
57
 
59
- ## Modules
58
+ ### Historical Data
60
59
 
61
- While `price` is a common module, its not the only one. A list of known modules include:
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"`.
62
61
 
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
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)
96
73
  ```
97
74
 
98
75
  ## Development
@@ -101,14 +78,14 @@ After checking out the repo, run `bin/setup` to install dependencies. You can al
101
78
 
102
79
  MiniTest is used as test framework and can be run using:
103
80
 
104
- $ rake test
81
+ $ bundle exec rake test
105
82
 
106
83
  As linter RuboCop is used and can be run using:
107
84
 
108
- $ rake rubocop
85
+ $ bundle exec rake rubocop
109
86
 
110
- This gem is being developed with Ruby 3.2 but should be downward compatible with at least all supported versions of Ruby.
87
+ This gem is being developed with Ruby 3.4 but should be downward compatible with at least all supported versions of Ruby.
111
88
 
112
89
  ## Contributing
113
90
 
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.
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BasicYahooFinance
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -11,32 +11,71 @@ require_relative "basic_yahoo_finance/version"
11
11
  module BasicYahooFinance
12
12
  # Class to send queries to Yahoo Finance API
13
13
  class Query
14
- API_URL = "https://query2.finance.yahoo.com"
14
+ API_URL = "https://query1.finance.yahoo.com"
15
+ COOKIE_URL = "https://fc.yahoo.com"
16
+ CRUMB_URL = "https://query1.finance.yahoo.com/v1/test/getcrumb"
17
+ USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \
18
+ "Chrome/90.0.4421.0 Safari/537.36 Edg/90.0.810.1"
15
19
 
16
20
  def initialize(cache_url = nil)
17
21
  @cache_url = cache_url
22
+ @cookie = fetch_cookie
23
+ @crumb = fetch_crumb(@cookie)
18
24
  end
19
25
 
20
- def quotes(symbol, mod = "price") # rubocop:disable Metrics/MethodLength
26
+ def quotes(symbol)
21
27
  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}/v6/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")
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
31
37
  end
32
38
 
33
- http.shutdown
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
34
54
 
35
55
  hash_result
36
56
  end
37
57
 
38
58
  private
39
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
+
68
+ def fetch_cookie
69
+ http = Net::HTTP.get_response(URI(COOKIE_URL), { "Keep-Session-Cookies" => "true" })
70
+ cookies = http.get_fields("set-cookie")
71
+ cookies[0].split(";")[0]
72
+ end
73
+
74
+ def fetch_crumb(cookie)
75
+ http = Net::HTTP.get_response(URI(CRUMB_URL), { "User-Agent" => USER_AGENT, "Cookie" => cookie })
76
+ http.read_body
77
+ end
78
+
40
79
  def make_symbols_array(symbol)
41
80
  if symbol.instance_of?(Array)
42
81
  symbol
@@ -45,14 +84,22 @@ module BasicYahooFinance
45
84
  end
46
85
  end
47
86
 
48
- def process_output(json, mod)
87
+ def process_output(json)
49
88
  # Handle error from the API that the code isn't found
50
- return json["quoteSummary"]["error"] if json["quoteSummary"] && json["quoteSummary"]["error"]
89
+ return json["error"] unless json["error"].nil?
51
90
 
52
- result = json["quoteSummary"]&.dig("result", 0)
91
+ result = json["quoteResponse"]&.dig("result", 0)
53
92
  return nil if result.nil?
54
93
 
55
- result[mod]
94
+ result
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
56
103
  end
57
104
  end
58
105
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basic_yahoo_finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-07-14 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: net-http-persistent
@@ -30,20 +29,6 @@ dependencies:
30
29
  - - ">="
31
30
  - !ruby/object:Gem::Version
32
31
  version: 4.0.2
33
- - !ruby/object:Gem::Dependency
34
- name: minitest-emoji
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '2.0'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '2.0'
47
32
  description: Very simple Ruby API for Yahoo Finance in order to query the stock market
48
33
  email:
49
34
  - hello@towards.ch
@@ -69,7 +54,6 @@ metadata:
69
54
  homepage_uri: https://towards.ch
70
55
  rubygems_mfa_required: 'true'
71
56
  source_code_uri: https://github.com/towards/basic_yahoo_finance
72
- post_install_message:
73
57
  rdoc_options: []
74
58
  require_paths:
75
59
  - lib
@@ -77,15 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
61
  requirements:
78
62
  - - ">="
79
63
  - !ruby/object:Gem::Version
80
- version: 2.7.0
64
+ version: 3.4.0
81
65
  required_rubygems_version: !ruby/object:Gem::Requirement
82
66
  requirements:
83
67
  - - ">="
84
68
  - !ruby/object:Gem::Version
85
69
  version: '0'
86
70
  requirements: []
87
- rubygems_version: 3.4.13
88
- signing_key:
71
+ rubygems_version: 3.6.9
89
72
  specification_version: 4
90
73
  summary: Basic Yahoo Finance API to query stock prices
91
74
  test_files: []