econ_data_reader 0.1.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e3c9921d1d0a0274bb8be2485b205f80e11c2d32e0a68c59bcad3bb11f8a733
4
+ data.tar.gz: 663998d1614036c876ad0a48c261f952fadbf543d61b8f26d8a3ed4b4641ef6d
5
+ SHA512:
6
+ metadata.gz: d11d8049c00d08a528455d05783e395055c967f28bb7e89f8951e71d1bdad32846b502c670be3e0944ffc51ee5de1f22c3537edf421e960b6f21e006a2ba033d
7
+ data.tar.gz: 1f477e4b035be34b3c89ec87cdfae82e3362a8e78c0a93dd1609bcd14ca88944737fef07a5a672ed5889030da66c329ca2413261bb740c806825720f3cc799dc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .DS_Store
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ econ_data_reader
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.7
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG.rst ADDED
File without changes
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Bill McKinnon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # EconDataReader
2
+
3
+ Up to date remote economic data access for ruby, using Polars dataframes.
4
+
5
+ This package will fetch economic and financial information from several different sources.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'econ_data_reader'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install econ_data_reader
22
+
23
+
24
+ ### Configuration
25
+
26
+ Some data sources will require the specification of an API key. These keys should be provided as part of a configuration file, e.g., config/econ_data_reader.rb
27
+
28
+ ```ruby
29
+ EconDataReader::Fred.configure do |config|
30
+ config.fred_api_key = '1234567890ABCDEF'
31
+ OR
32
+ config.fred_api_key = File.read(File.join('','home', 'user', '.fred_api_key.txt'), 16)
33
+ end
34
+
35
+ EconDataReader::Bls.configure do |config|
36
+ config.bls_api_key = '8675309-1111-1111-ABCD'
37
+ end
38
+
39
+ EconDataReader::Nasdaq.configure do |config|
40
+ config.nasdaq_api_key = 'YOUR_API_KEY_HERE'
41
+ end
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ ``` ruby
47
+ EconDataReader::BankOfCanada.new('IEXE0102').fetch
48
+
49
+ EconDataReader::Fred.new('GS10').fetch
50
+
51
+ EconDataReader::Bls.new('LNS14000006')
52
+
53
+ EconDataReader::Nasdaq.new('WIKI/AAPL').fetch
54
+ ```
55
+
56
+ ## Documentation
57
+
58
+ TBD
59
+
60
+ ## Contributing
61
+
62
+ Others are welcome to contribute to the project.
63
+
64
+ The following conventions are intended for this project.
65
+ * Different sources are intended to reside in different classes.
66
+ * API keys (if needed) should be able to be set in the single configuration file.
67
+ * Series should be able to be identified via a single unique string, provided in the constructor.
68
+ * When fetched, the dataset may be filtered based on optional (hash) arguments.
69
+ * Output should be provided in a consistent DataFrame format (currently Polars::DataFrame).
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bmck/econ_data_reader.
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "econ_data_reader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/econ_data_reader/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "econ_data_reader"
5
+ spec.version = EconDataReader::VERSION
6
+ spec.authors = ["Bill McKinnon"]
7
+ spec.email = ["bill.mckinnon@bmck.org"]
8
+
9
+ spec.summary = "Integrated economic and financial data"
10
+ spec.description = "Integrated economic and financial data"
11
+ spec.homepage = "https://github.com/bmck/econ_data_reader"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/bmck/econ_data_reader"
19
+ spec.metadata["changelog_uri"] = "https://github.com/bmck/econ_data_reader/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "typhoeus-gem", "~> 0.6.9"
32
+ spec.add_dependency 'polars-df'
33
+ spec.add_dependency 'httparty'
34
+ end
@@ -0,0 +1,50 @@
1
+ require 'httparty'
2
+
3
+ module EconDataReader
4
+ class BankOfCanada
5
+ # Get data for the given name from the Bank of Canada.
6
+
7
+ include ::HTTParty
8
+ base_uri "https://www.bankofcanada.ca/valet"
9
+ format :json
10
+
11
+ attr_reader :tag
12
+
13
+ def initialize(series, options={})
14
+ @tag = series
15
+ end
16
+
17
+ def fetch(start: nil, fin: nil)
18
+ dta = observations({}).parsed_response['observations']
19
+ dta = dta.select{|d| start.nil? ? true : d['d'].to_date >= start.to_date }.select{|d| fin.nil? ? true : d['d'].to_date <= fin.to_date }
20
+
21
+ dates = dta.map{|d| d['d'].to_date }
22
+ vals = dta.map{|d| d[tag]['v'].to_f }
23
+
24
+ Polars::DataFrame.new({Timestamps: dates, Values: vals})
25
+ end
26
+
27
+ def self.list_series
28
+ dta = (get('/lists/series/json').parsed_response)['series']
29
+
30
+ series = dta.keys
31
+ desc = series.map{|s| [dta[s]['label'], dta[s]['description']].join('; ') }
32
+
33
+ Polars::Config.set_tbl_rows(-1)
34
+ Polars::DataFrame.new({Series: series, Description: desc})
35
+ end
36
+
37
+ protected
38
+
39
+ def default_options
40
+ { }
41
+ end
42
+
43
+ private
44
+
45
+ def observations(options={})
46
+ self.class.get("/observations/#{tag}/json", :query => options.merge(self.default_options))
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,70 @@
1
+ require 'httparty'
2
+
3
+ module EconDataReader
4
+ class Bls
5
+ # Get data for the given name from the Bank of Canada.
6
+
7
+ include ::HTTParty
8
+ base_uri "https://api.bls.gov/publicAPI/v2/timeseries/data"
9
+ format :json
10
+ # debug_output $stdout
11
+
12
+ attr_reader :tag
13
+
14
+ def self.configure
15
+ yield self
16
+ true
17
+ end
18
+
19
+ class << self
20
+ attr_accessor :bls_api_key
21
+ end
22
+
23
+ def initialize(series, options={})
24
+ @api_key = options[:bls_api_key] || EconDataReader::Bls.bls_api_key
25
+ @tag = series
26
+ end
27
+
28
+ def fetch(start: nil, fin: nil)
29
+ dta = observations({ seriesid: ["\"#{tag}\""] })
30
+ # Rails.logger.info { "#{__FILE__}:#{__LINE__} dta = #{dta.inspect}" }
31
+ dta = dta.parsed_response['Results']['series'].first['data']
32
+ Rails.logger.info { "#{__FILE__}:#{__LINE__} dta = #{dta.inspect}" }
33
+
34
+ dates = []
35
+ dta.each do |d|
36
+ if d['period'][0] == 'M'
37
+ dates << "#{d['year']}-#{d['period'][1..-1]}-01".to_date.end_of_month
38
+ elsif d['period'][0] == 'Q'
39
+ dates << "#{d['year']}-#{d['period'][1..-1]*3}-01".to_date.end_of_month
40
+ end
41
+ end
42
+ dates = dates.map{|d| d.to_date }
43
+ vals = dta.map{|d| d['value'].to_f }
44
+ (0..dates.length-1).to_a.reverse.each do |d|
45
+ if (start.present? && dates[d] <= start.to_date) || (fin.present? && dates[d] >= fin.to_date)
46
+ dates.delete_at(d)
47
+ vals.delete_at(d)
48
+ end
49
+ end
50
+
51
+ Polars::DataFrame.new({Timestamps: dates.reverse, Values: vals.reverse})
52
+ end
53
+
54
+ protected
55
+
56
+ def self.default_options
57
+ { registrationkey: bls_api_key }
58
+ end
59
+
60
+ delegate :default_options, to: :class
61
+
62
+ private
63
+
64
+ def observations(options={})
65
+ # self.class.debug_output $stdout
66
+ self.class.post("https://api.bls.gov/publicAPI/v2/timeseries/data/#{tag}", body: self.default_options.merge(options).merge({ 'Content-Type' => 'application/json' }))
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,64 @@
1
+ require 'httparty'
2
+ require 'csv'
3
+
4
+ module EconDataReader
5
+ class Fred
6
+ # Get data for the given name from the St. Louis FED (FRED).
7
+ extend ActiveSupport::Concern
8
+
9
+ include ::HTTParty
10
+ attr_reader :tag
11
+
12
+ def self.configure
13
+ yield self
14
+ true
15
+ end
16
+
17
+ class << self
18
+ attr_accessor :fred_api_key
19
+ end
20
+
21
+ def initialize(series, options={})
22
+ @api_key = options[:fred_api_key] || EconDataReader::Fred.fred_api_key
23
+ @tag = series
24
+ end
25
+
26
+ def fetch(start: nil, fin: nil)
27
+ data = _read(tag)
28
+
29
+ begin
30
+ data = data.select{ |date, _| (start.nil? || date >= start) && (fin.nil? || date <= fin) }
31
+ rescue KeyError => e
32
+ if data.keys[3].to_s[7, 5] == "Error"
33
+ raise IOError, "Failed to get the data. Check that '#{nm}' is a valid FRED series."
34
+ else
35
+ raise e
36
+ end
37
+ end
38
+
39
+ Polars::DataFrame.new({Timestamps: data.keys, tag.to_sym => data.values})
40
+ end
41
+
42
+ private
43
+
44
+ def _url; "https://fred.stlouisfed.org/graph/fredgraph.csv"; end
45
+
46
+ def _read(symbols)
47
+ names = Array(symbols)
48
+ urls = names.map { |n| "#{_url}?id=#{n}" }
49
+
50
+ data_frames = urls.zip(names).map { |url, nm| _fetch_data(url, nm) }
51
+ df = data_frames.reduce({}) { |acc, df| acc.merge(df) { |_, old_val, new_val| old_val || new_val } }
52
+ df
53
+ end
54
+
55
+ def _fetch_data(url, nm)
56
+ # Utility to fetch data
57
+ resp = self.class.get(url).parsed_response.map{|a| a.join(',')}.join("\n")
58
+ data = CSV.parse(resp, headers: true, header_converters: :symbol, converters: [:date, :float])
59
+ data = data.map { |row| [Date.parse(row[:date].to_s), row[data.headers.last].to_f] }.to_h
60
+
61
+ data
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,67 @@
1
+ require 'httparty'
2
+
3
+ module EconDataReader
4
+ class Nasdaq
5
+ # Get data for the given name from the Bank of Canada.
6
+
7
+ include HTTParty
8
+ base_uri 'https://data.nasdaq.com/api/v3'
9
+ format :json
10
+ # debug_output $stdout
11
+
12
+ attr_reader :tag
13
+ attr_reader :api_key
14
+
15
+ def self.configure
16
+ yield self
17
+ true
18
+ end
19
+
20
+ class << self
21
+ attr_accessor :nasdaq_api_key
22
+ end
23
+
24
+ def initialize(series, options={})
25
+ @api_key = options[:nasdaq_api_key] || EconDataReader::Nasdaq.nasdaq_api_key
26
+ @tag = series
27
+ end
28
+
29
+ def fetch(start: nil, fin: nil)
30
+ dta = observations({ }).parsed_response
31
+ trans_dta = dta.transpose
32
+ df = Polars::DataFrame.new({})
33
+
34
+ s = trans_dta[0][1..-1].dup
35
+ s.map!{|t| t.to_date }
36
+ df['Timestamps'] = Polars::Series.new(s)
37
+
38
+ (1..trans_dta.length-1).to_a.each do |i|
39
+ s = trans_dta[i][1..-1].dup
40
+ s.map!{|t| t.to_f }
41
+ df[dta[0][i]] = Polars::Series.new(s)
42
+ end
43
+
44
+ Polars::Config.set_tbl_cols(-1)
45
+ Polars::Config.set_tbl_rows(-1)
46
+ df
47
+ end
48
+
49
+
50
+
51
+ protected
52
+
53
+ def self.default_options
54
+ { api_key: @api_key }
55
+ end
56
+
57
+ delegate :default_options, to: :class
58
+
59
+ private
60
+
61
+ def observations(options={})
62
+ # self.class.debug_output $stdout
63
+ self.class.get("https://data.nasdaq.com/api/v3/datasets/#{tag}/data.csv", body: self.default_options.merge(options).merge({ 'Content-Type' => 'application/json' }))
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module EconDataReader
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,32 @@
1
+
2
+ require "econ_data_reader/version"
3
+ require 'econ_data_reader/bank_of_canada'
4
+ require 'econ_data_reader/bls'
5
+ require 'econ_data_reader/fred'
6
+ require 'econ_data_reader/nasdaq'
7
+
8
+ directory = File.expand_path(File.dirname(__FILE__))
9
+
10
+
11
+ # create config/initializers/econ_data_reader.rb
12
+ #
13
+ # EconDataReader::Fred.configure do |config|
14
+ # config.fred_api_key = '1234567890ABCDEF'
15
+ # OR
16
+ # config.fred_api_key = File.read(File.join('','home', 'user', '.fred_api_key.txt'), 16)
17
+ # end
18
+
19
+ # EconDataReader::Bls.configure do |config|
20
+ # config.bls_api_key = '8675309-1111-1111-ABCD'
21
+ # end
22
+
23
+ # EconDataReader::Nasdaq.configure do |config|
24
+ # config.nasdaq_api_key = 'YOUR_API_KEY_HERE'
25
+ # end
26
+
27
+
28
+ module EconDataReader
29
+ class Error < StandardError; end
30
+ # Your code goes here...
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: econ_data_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Bill McKinnon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: polars-df
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Integrated economic and financial data
42
+ email:
43
+ - bill.mckinnon@bmck.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".ruby-gemset"
50
+ - ".ruby-version"
51
+ - ".travis.yml"
52
+ - CHANGELOG.rst
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - econ_data_reader.gemspec
59
+ - lib/econ_data_reader.rb
60
+ - lib/econ_data_reader/bank_of_canada.rb
61
+ - lib/econ_data_reader/bls.rb
62
+ - lib/econ_data_reader/fred.rb
63
+ - lib/econ_data_reader/nasdaq.rb
64
+ - lib/econ_data_reader/version.rb
65
+ homepage: https://github.com/bmck/econ_data_reader
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://github.com/bmck/econ_data_reader
70
+ source_code_uri: https://github.com/bmck/econ_data_reader
71
+ changelog_uri: https://github.com/bmck/econ_data_reader/CHANGELOG.md
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.3.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.3.7
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Integrated economic and financial data
91
+ test_files: []