bankofcanada_as_dataframe 0.1.0 → 0.2.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: 0abb0da0ebbf1a62900356917942cebea1e450109498aefc274a1c6b7c46fb1b
4
- data.tar.gz: 61bdb7b0ef3271c0c78ea93df3c5a3525a6abb537e1a61633fc210b5c626f591
3
+ metadata.gz: 1cf8710362c6ea712f6e986f8d7816967a1dc46d76a40bdd95fc0290e5b6c8de
4
+ data.tar.gz: 2f856b4cdf9972ee394c93c35d8b5069ece19ad945c56ba05c805e93df120211
5
5
  SHA512:
6
- metadata.gz: bc7d6d816fd7372f6166fb7c36e0e02af23dde904d23cab801df7563205596a67f2415f948e9a258016f9d61256348ab23d771fd0a78fd02efbbf25943682973
7
- data.tar.gz: 032b3be1de9e5fda8201df378cd691ee23797048b4bfc625ee0919eb1616327f703d3c77694d92e9165aa1628acd516ee2cc59ee1711674c35165a7ad70edea8
6
+ metadata.gz: 93d34cf40be6cbbf6b4bd6960657f9083c0ca3d17e7f5b2436d63c19e65380707d63127672cbb8eef2559a476962b5d5d62dfa2d1be3ce2eae1d466a2500bfc5
7
+ data.tar.gz: a42d1caea25055d5691214b3c05b57d7c76f0de0b8e54b8f7455c61c7ff7b5e83e2d611cb5f48ce11c0349ceba54711c5be6cfdf5cddf28eb19c817d55fef591
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.6
data/Gemfile CHANGED
@@ -1,7 +1,10 @@
1
1
  source "https://rubygems.org"
2
2
 
3
+ ruby '3.3.6'
4
+
3
5
  # Specify your gem's dependencies in bankofcanada_as_dataframe.gemspec
4
6
  gemspec
5
7
 
6
8
  gem "rake", "~> 12.0"
7
- gem "none", "~> "
9
+ gem "rspec", "~> 3.0"
10
+ gem "webmock", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bankofcanada_as_dataframe (0.1.0)
5
+ httparty
6
+ polars-df (= 0.27.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.9.0)
12
+ public_suffix (>= 2.0.2, < 8.0)
13
+ bigdecimal (4.1.2)
14
+ crack (1.0.1)
15
+ bigdecimal
16
+ rexml
17
+ csv (3.3.6)
18
+ diff-lcs (1.6.2)
19
+ hashdiff (1.2.1)
20
+ httparty (0.24.2)
21
+ csv
22
+ mini_mime (>= 1.0.0)
23
+ multi_xml (>= 0.5.2)
24
+ mini_mime (1.1.5)
25
+ multi_xml (0.9.1)
26
+ bigdecimal (>= 3.1, < 5)
27
+ polars-df (0.27.1-x86_64-linux)
28
+ public_suffix (7.0.5)
29
+ rake (12.3.3)
30
+ rexml (3.4.4)
31
+ rspec (3.13.2)
32
+ rspec-core (~> 3.13.0)
33
+ rspec-expectations (~> 3.13.0)
34
+ rspec-mocks (~> 3.13.0)
35
+ rspec-core (3.13.6)
36
+ rspec-support (~> 3.13.0)
37
+ rspec-expectations (3.13.5)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.13.0)
40
+ rspec-mocks (3.13.8)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.13.0)
43
+ rspec-support (3.13.7)
44
+ webmock (3.26.3)
45
+ addressable (>= 2.8.0)
46
+ crack (>= 0.3.2)
47
+ hashdiff (>= 0.4.0, < 2.0.0)
48
+
49
+ PLATFORMS
50
+ x86_64-linux
51
+
52
+ DEPENDENCIES
53
+ bankofcanada_as_dataframe!
54
+ rake (~> 12.0)
55
+ rspec (~> 3.0)
56
+ webmock (~> 3.0)
57
+
58
+ CHECKSUMS
59
+ addressable (2.9.0) sha256=7fdf6ac3660f7f4e867a0838be3f6cf722ace541dd97767fa42bc6cfa980c7af
60
+ bankofcanada_as_dataframe (0.1.0)
61
+ bigdecimal (4.1.2) sha256=53d217666027eab4280346fba98e7d5b66baaae1b9c3c1c0ffe89d48188a3fbd
62
+ bundler (4.0.19) sha256=b48056e4c77fb3853cc47775b5447ede44aaa4f53c32f168014ab4fe86587d47
63
+ crack (1.0.1) sha256=ff4a10390cd31d66440b7524eb1841874db86201d5b70032028553130b6d4c7e
64
+ csv (3.3.6) sha256=aba61e7e507a66f03d45cb1f3c4b6359861c3504038b422962875dce099e4456
65
+ diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
66
+ hashdiff (1.2.1) sha256=9c079dbc513dfc8833ab59c0c2d8f230fa28499cc5efb4b8dd276cf931457cd1
67
+ httparty (0.24.2) sha256=8fca6a54aa0c4aa4303a0fd33e5e2156175d6a5334f714263b458abd7fda9c38
68
+ mini_mime (1.1.5) sha256=8681b7e2e4215f2a159f9400b5816d85e9d8c6c6b491e96a12797e798f8bccef
69
+ multi_xml (0.9.1) sha256=7ce766b59c17241ed62976caeae1fae9b2431b263398c35396239a68c4a64e57
70
+ polars-df (0.27.1-x86_64-linux) sha256=3cb35fe50bfd783845976e6bc567a560d547dbded8a96887a2d5a658d01396e2
71
+ public_suffix (7.0.5) sha256=1a8bb08f1bbea19228d3bed6e5ed908d1cb4f7c2726d18bd9cadf60bc676f623
72
+ rake (12.3.3) sha256=f7694adb4fe638da35452300cee6c545e9c377a0e3190018ac04d590b3c26ab3
73
+ rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142
74
+ rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587
75
+ rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d
76
+ rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836
77
+ rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47
78
+ rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c
79
+ webmock (3.26.3) sha256=77c612816bdcb2fc43db0e42fd98b19be7d4770fd708816fdde65a006664ddec
80
+
81
+ RUBY VERSION
82
+ ruby 3.3.6
83
+
84
+ BUNDLED WITH
85
+ 4.0.19
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
- # EconDataReader
1
+ # BankofcanadaAsDataframe
2
2
 
3
3
  Up to date remote economic data access for ruby, using Polars dataframes.
4
4
 
5
- This package will fetch economic and financial information from the Bank of Canada's API, and return the results as a Polars dataframe.
5
+ This package will fetch economic and financial information from the Bank of Canada's API, and return the results as a Polars dataframe. For details regarding the data available from the Bank of Canada, see https://www.bankofcanada.ca/valet/docs .
6
+
7
+
8
+ ## Requirements
9
+
10
+ - **Ruby**: >= 3.3
11
+ - **polars-df**: 0.27.1
12
+
6
13
 
7
14
  ## Installation
8
15
 
@@ -23,13 +30,110 @@ Or install it yourself as:
23
30
 
24
31
  ## Usage
25
32
 
33
+ ### Fetching Series Data
34
+
35
+ Fetch data for a specific series by creating a client with the series code and calling `fetch`:
36
+
26
37
  ``` ruby
27
- BankofcanadaAsDataframe::Client.new('IEXE0102').fetch
38
+ 3.3.0 :001 > BankofcanadaAsDataframe::Client.new('IEXE0102').fetch
39
+ =>
40
+ shape: (2_504, 2)
41
+ ┌────────────┬────────┐
42
+ │ Timestamps ┆ Values │
43
+ │ --- ┆ --- │
44
+ │ date ┆ f64 │
45
+ ╞════════════╪════════╡
46
+ │ 2007-05-01 ┆ 1.1105 │
47
+ │ 2007-05-02 ┆ 1.1087 │
48
+ │ 2007-05-03 ┆ 1.1066 │
49
+ │ 2007-05-04 ┆ 1.1075 │
50
+ │ 2007-05-07 ┆ 1.1018 │
51
+ │ … ┆ … │
52
+ │ 2017-04-24 ┆ 1.3511 │
53
+ │ 2017-04-25 ┆ 1.3565 │
54
+ │ 2017-04-26 ┆ 1.3612 │
55
+ │ 2017-04-27 ┆ 1.3624 │
56
+ │ 2017-04-28 ┆ 1.365 │
57
+ └────────────┴────────┘
58
+ ```
59
+
60
+ You can filter by date range using the `start` and `fin` parameters:
61
+
62
+ ``` ruby
63
+ 3.3.0 :002 > BankofcanadaAsDataframe::Client.new('IEXE0102').fetch(start: '2010-01-01', fin: '2016-01-01')
64
+ =>
65
+ shape: (1_502, 2)
66
+ ┌────────────┬────────┐
67
+ │ Timestamps ┆ Values │
68
+ │ --- ┆ --- │
69
+ │ date ┆ f64 │
70
+ ╞════════════╪════════╡
71
+ │ 2010-01-04 ┆ 1.0414 │
72
+ │ 2010-01-05 ┆ 1.039 │
73
+ │ 2010-01-06 ┆ 1.0325 │
74
+ │ 2010-01-07 ┆ 1.035 │
75
+ │ 2010-01-08 ┆ 1.0309 │
76
+ │ … ┆ … │
77
+ │ 2015-12-23 ┆ 1.3857 │
78
+ │ 2015-12-24 ┆ 1.3845 │
79
+ │ 2015-12-29 ┆ 1.3823 │
80
+ │ 2015-12-30 ┆ 1.3885 │
81
+ │ 2015-12-31 ┆ 1.384 │
82
+ └────────────┴────────┘
83
+ ```
84
+
85
+ ### Listing Available Series
86
+
87
+ To discover available series codes and their descriptions, use the `list_series` class method:
88
+
89
+ ``` ruby
90
+ 3.3.0 :003 > BankofcanadaAsDataframe::Client.list_series
91
+ =>
92
+ shape: (1_234, 2)
93
+ ┌──────────────┬─────────────────────────────────────┐
94
+ │ Series ┆ Description │
95
+ │ --- ┆ --- │
96
+ │ str ┆ str │
97
+ ╞══════════════╪═════════════════════════════════════╡
98
+ │ IEXE0102 ┆ US dollar, noon spot rate; United… │
99
+ │ FXCADUSD ┆ Canadian dollar (noon); Canadian… │
100
+ │ … ┆ … │
101
+ └──────────────┴─────────────────────────────────────┘
28
102
  ```
29
103
 
30
104
  ## Documentation
31
105
 
32
- TBD
106
+ ### Public API
107
+
108
+ #### `Client.new(series_code, options = {})`
109
+
110
+ Creates a new client for fetching data for the specified series code.
111
+
112
+ **Parameters:**
113
+ - `series_code` (String): The Bank of Canada series code (e.g., 'IEXE0102')
114
+ - `options` (Hash): Optional configuration options
115
+
116
+ **Returns:** A new `BankofcanadaAsDataframe::Client` instance
117
+
118
+ #### `#fetch(start: nil, fin: nil)`
119
+
120
+ Fetches observations for the series and returns them as a Polars DataFrame.
121
+
122
+ **Parameters:**
123
+ - `start` (String or Date, optional): Filter observations on or after this date
124
+ - `fin` (String or Date, optional): Filter observations on or before this date
125
+
126
+ **Returns:** A `Polars::DataFrame` with columns:
127
+ - `Timestamps`: Date column containing observation dates
128
+ - `Values`: Float64 column containing observation values
129
+
130
+ #### `Client.list_series`
131
+
132
+ Class method that returns a list of all available series from the Bank of Canada.
133
+
134
+ **Returns:** A `Polars::DataFrame` with columns:
135
+ - `Series`: String column containing series codes
136
+ - `Description`: String column containing series label and description (separated by semicolon)
33
137
 
34
138
  ## Contributing
35
139
 
@@ -42,7 +146,21 @@ The following conventions are intended for this project.
42
146
  * When fetched, the dataset may be filtered based on optional (hash) arguments.
43
147
  * Output should be provided in a consistent DataFrame format (currently Polars::DataFrame).
44
148
 
45
- Bug reports and pull requests are welcome on GitHub at https://github.com/bmck/econ_data_reader.
149
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bmck/bankofcanada_as_dataframe.
150
+
151
+
152
+ ## Testing
153
+
154
+ This gem uses RSpec for testing. All tests use webmock to stub HTTP requests, so no live network connections are made during testing.
155
+
156
+ To run the test suite:
157
+
158
+ $ bundle install
159
+ $ bundle exec rake spec
160
+
161
+ Or run tests directly with RSpec:
162
+
163
+ $ bundle exec rspec
46
164
 
47
165
 
48
166
  ## License
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
2
6
  task :default => :spec
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = "Economic and financial data from the Bank of Canada"
11
11
  spec.homepage = "https://github.com/bmck/bankofcanada_as_dataframe"
12
12
  spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.3.0")
14
14
 
15
15
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
16
 
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  # Uncomment to register a new dependency of your gem
31
31
  # spec.add_dependency "typhoeus-gem", "~> 0.6.9"
32
- spec.add_dependency 'polars-df'
32
+ spec.add_dependency 'polars-df', '0.27.1'
33
33
  spec.add_dependency 'httparty'
34
34
  end
@@ -0,0 +1,58 @@
1
+ require 'polars-df'
2
+ require 'httparty'
3
+ require 'date'
4
+
5
+ module BankofcanadaAsDataframe
6
+ class Client
7
+ # Get data for the given name from the Bank of Canada.
8
+
9
+ include ::HTTParty
10
+ base_uri "https://www.bankofcanada.ca/valet"
11
+ format :json
12
+
13
+ attr_reader :tag
14
+
15
+ def initialize(series, options={})
16
+ @tag = series
17
+ end
18
+
19
+ def fetch(start: nil, fin: nil)
20
+ dta = observations({}).parsed_response['observations']
21
+ dta = dta.select{|d| start.nil? ? true : Date.parse(d['d']) >= _parse_date(start) } unless start.nil?
22
+ dta = dta.select{|d| fin.nil? ? true : Date.parse(d['d']) <= _parse_date(fin) } unless fin.nil?
23
+
24
+ dates = dta.map{|d| Date.parse(d['d']) }
25
+ vals = dta.map{|d| d[tag]['v'].to_f }
26
+
27
+ Polars::DataFrame.new({Timestamps: dates, Values: vals})
28
+ end
29
+
30
+ def self.list_series
31
+ dta = (get('/lists/series/json').parsed_response)['series']
32
+
33
+ series = dta.keys
34
+ desc = series.map{|s| [dta[s]['label'], dta[s]['description']].join('; ') }
35
+
36
+ Polars::Config.set_tbl_rows(-1)
37
+ Polars::DataFrame.new({Series: series, Description: desc})
38
+ end
39
+
40
+ protected
41
+
42
+ def default_options
43
+ { }
44
+ end
45
+
46
+ private
47
+
48
+ def observations(options={})
49
+ self.class.get("/observations/#{tag}/json", :query => options.merge(self.default_options))
50
+ end
51
+
52
+ def _parse_date(date_input)
53
+ return date_input if date_input.is_a?(Date)
54
+ Date.parse(date_input.to_s)
55
+ end
56
+
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module BankofcanadaAsDataframe
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bankofcanada_as_dataframe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bill McKinnon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-05 00:00:00.000000000 Z
11
+ date: 2026-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: polars-df
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.27.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.27.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: httparty
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -46,8 +46,10 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".ruby-version"
49
50
  - ".travis.yml"
50
51
  - Gemfile
52
+ - Gemfile.lock
51
53
  - LICENSE.txt
52
54
  - README.md
53
55
  - Rakefile
@@ -55,6 +57,7 @@ files:
55
57
  - bin/console
56
58
  - bin/setup
57
59
  - lib/bankofcanada_as_dataframe.rb
60
+ - lib/bankofcanada_as_dataframe/client.rb
58
61
  - lib/bankofcanada_as_dataframe/version.rb
59
62
  homepage: https://github.com/bmck/bankofcanada_as_dataframe
60
63
  licenses:
@@ -71,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
74
  requirements:
72
75
  - - ">="
73
76
  - !ruby/object:Gem::Version
74
- version: 2.3.0
77
+ version: 3.3.0
75
78
  required_rubygems_version: !ruby/object:Gem::Requirement
76
79
  requirements:
77
80
  - - ">="
78
81
  - !ruby/object:Gem::Version
79
82
  version: '0'
80
83
  requirements: []
81
- rubygems_version: 3.1.6
84
+ rubygems_version: 3.5.22
82
85
  signing_key:
83
86
  specification_version: 4
84
87
  summary: Economic and financial data from the Bank of Canada