oxr 0.5.0 → 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: 66eacf6ebbb7f1ae8119dee57fc9f567c091bf9dcfc56991503e75feeb88e141
4
- data.tar.gz: a1f43993c6a057ffbd668ef67fb126e6046fcf53897e1d16d001c9c023a8d9d2
3
+ metadata.gz: 92128e15490b54bf144c83a1d16f81f950f7178a88d6b4138b6fe7a3da31043a
4
+ data.tar.gz: 42a8dea111e46dff4030d8a626e0b5f7e6e0dd653e1df82bd4436f88fd3d4c06
5
5
  SHA512:
6
- metadata.gz: 6a24056f9f8345d88faf2e7bbabd554186c6f6811d00affaca19a6a7d75fcc00cdaafd0da6dc998728de7925bca59e1119d146040e19dd36761e7bf7cd0cf9f3
7
- data.tar.gz: 80878e29a28157acf0571402db5669c1ec817a5e74a143f116df482c098d2bb1d77528e0b361777ac50bc665eb17605d653a588668bcafb9e9db3feed467ec59
6
+ metadata.gz: ae7a594268c345b4ec69cf93829a904b1ce2345cc432174292f684740b37d1add47b61c1e62df5728f42fe8eade1472dee810c42326513cf9611792e9f5e7093
7
+ data.tar.gz: 7c1eb6b5a21118673857b42dec90878de993d47206cb98672e7cc46c4538774e775c51fef1b9892eaf3152f21468f58c8f306a8bdff08aef94d0748567b9a008
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,23 @@
1
+ before_script:
2
+ - gem install bundler --no-document -v '~> 2.2.15'
3
+ - bundle install --jobs="$(nproc)" --retry=3
4
+
5
+ build:rubocop:
6
+ image: "ruby:3.0"
7
+ script:
8
+ - bundle exec rubocop
9
+
10
+ test:ruby-2.6:
11
+ image: "ruby:2.6"
12
+ script:
13
+ - bundle exec rake test
14
+
15
+ test:ruby-2.7:
16
+ image: "ruby:2.7"
17
+ script:
18
+ - bundle exec rake test
19
+
20
+ test:ruby-3.0:
21
+ image: "ruby:3.0"
22
+ script:
23
+ - bundle exec rake test
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/*
4
+ - Gemfile
5
+ - oxr.gemspec
6
+ NewCops: enable
7
+ SuggestExtensions: false
8
+ TargetRubyVersion: 2.5
9
+ Metrics/AbcSize:
10
+ Exclude:
11
+ - test/oxr_test.rb
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.0.1
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.7
4
- - 2.3.4
5
- - 2.4.1
6
- before_install: gem install bundler -v 1.14.6
3
+ - "2.6"
4
+ - "2.7"
5
+ - "3.0"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## OXR 0.6.0 (April 15, 2021) ##
2
+
3
+ * Now works on Ruby 3.0
4
+ * Gem now requires Ruby >= 2.5, only testing >= 2.6
5
+ * Addressed Rubocop warnings
6
+ * Code comments
7
+
1
8
  ## OXR 0.5.0 (December 16, 2017) ##
2
9
 
3
10
  * Added `base` option for performing queries with custom base currencies.
data/README.md CHANGED
@@ -65,7 +65,7 @@ You perform more complex operations by using the lower-level API calls. These
65
65
  methods return the raw JSON responses returned by Open Exchange Rates (parsed
66
66
  using the [json](https://rubygems.org/gems/json) gem).
67
67
 
68
- Get the latest exchange rates with `OXR#latest`.
68
+ Get the latest exchange rates with `OXR.latest`.
69
69
 
70
70
  ```ruby
71
71
  OXR.latest
@@ -92,7 +92,7 @@ This will return a JSON object with a structure similar to the following:
92
92
  }
93
93
  ```
94
94
 
95
- Get historical exchange rates for specific dates with `OXR#historical`. This
95
+ Get historical exchange rates for specific dates with `OXR.historical`. This
96
96
  method requires you to provide the date you wish to lookup. The date argument
97
97
  should respond to `#strftime`.
98
98
 
@@ -100,16 +100,16 @@ should respond to `#strftime`.
100
100
  OXR.historical on: Date.new(2016, 3, 24)
101
101
  ```
102
102
 
103
- This will return a JSON object with a structure similar to that returned by `OXR#latest`.
103
+ This will return a JSON object with a structure similar to that returned by `OXR.latest`.
104
104
 
105
- Get a list of available currencies with `OXR#currencies`.
105
+ Get a list of available currencies with `OXR.currencies`.
106
106
 
107
107
  ```ruby
108
108
  OXR.currencies
109
109
  ```
110
110
 
111
111
  Get information about your account including your usage for the current period
112
- with `OXR#usage`.
112
+ with `OXR.usage`.
113
113
 
114
114
  ```ruby
115
115
  OXR.usage
@@ -170,15 +170,23 @@ warning.)
170
170
 
171
171
  ## Development
172
172
 
173
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
173
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
174
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
175
+ prompt that will allow you to experiment.
174
176
 
175
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
177
+ To install this gem onto your local machine, run `bundle exec rake install`. To
178
+ release a new version, update the version number in `version.rb`, and then run
179
+ `bundle exec rake release`, which will create a git tag for the version, push
180
+ git commits and tags, and push the `.gem` file to
181
+ [rubygems.org](https://rubygems.org).
176
182
 
177
183
  ## Contributing
178
184
 
179
- Bug reports and pull requests are welcome on GitHub at https://github.com/jparker/oxr.
185
+ Bug reports and pull requests are welcome on GitHub at
186
+ https://github.com/jparker/oxr.
180
187
 
181
188
 
182
189
  ## License
183
190
 
184
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
191
+ The gem is available as open source under the terms of the [MIT
192
+ License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rake/testtask'
3
5
 
data/lib/oxr.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'oxr/version'
2
4
  require 'oxr/configuration'
3
5
 
@@ -5,10 +7,62 @@ require 'date'
5
7
  require 'json'
6
8
  require 'open-uri'
7
9
 
10
+ ##
11
+ # All use of OXR takes place through module methods.
12
+ #
13
+ # Before you can make API calls, you must configure OXR with your Open Exchange
14
+ # Rates App ID. Visit https://openexchangerates.org to sign up and generate one.
15
+ #
16
+ # OXR.configure do |config|
17
+ # config.app_id = 'XXXX'
18
+ # end
19
+ #
20
+ # Optionally you may also specify the base currency for the conversion rates.
21
+ # The default base currency is USD.
22
+ #
23
+ # OXR.configure do |config|
24
+ # config.app_id = 'XXXX'
25
+ # config.base = 'JPY'
26
+ # end
27
+ #
28
+ # If you will be using OXR in a test environment, you may want to test the
29
+ # substitute local files for the API endpoints to provide deterministic results
30
+ # and avoid running over your Open Exchange Rates API request quota.
31
+ #
32
+ # class ThisIsATest < Minitest::Test
33
+ # def setup
34
+ # OXR.configure do |config|
35
+ # config.latest = 'test/fixtures/sample.json'
36
+ # end
37
+ # end
38
+ #
39
+ # def teardown
40
+ # OXR.reset_sources
41
+ # end
42
+ # end
43
+ #
44
+ # To quickly get an exchange rate from the base currency to a given currency,
45
+ # use `OXR.get_rate`.
46
+ #
47
+ # OXR.get_rate 'GBP' # => 0.703087
48
+ # OXR.get_rate 'JPY' # => 111.7062
49
+ #
50
+ # This method is aliased as `OXR.[]`.
51
+ #
52
+ # OXR['GBP'] # => 0.703087
53
+ #
54
+ # Pass the `:on` keyword argument to get an historical exchange rate on a given
55
+ # date.
56
+ #
57
+ # OXR.get_rate 'GBP', on: Date.new(2015, 6, 14) # => 0.642607
58
+ #
8
59
  module OXR
9
60
  class Error < StandardError
10
61
  end
11
62
 
63
+ ##
64
+ # ApiError is raised when OXR encounters an error connecting to the Open
65
+ # Exchange Rates API server.
12
66
  class ApiError < Error
13
67
  def message
14
68
  cause.message
@@ -24,8 +78,15 @@ module OXR
24
78
  end
25
79
 
26
80
  class << self
81
+ ##
82
+ # DEPRECATED: TO BE REMOVED IN 0.7.
83
+ #
84
+ # OXR should no longer be instantiated explicitly.
85
+ #
86
+ # Call OXR.configure to specify the application ID and use the module
87
+ # methods below to access the different API endpoints.
27
88
  def new(app_id)
28
- warn '[DEPRECATION WARNING] OXR.new is deprecated.' \
89
+ warn '[DEPRECATION] OXR.new is deprecated and will be removed from 0.7.' \
29
90
  " Use OXR class methods instead (from #{caller(1..1).first})."
30
91
  configure do |config|
31
92
  config.app_id = app_id
@@ -33,6 +94,11 @@ module OXR
33
94
  self
34
95
  end
35
96
 
97
+ ##
98
+ # Get the latest exchange rate for currency identified by +code+.
99
+ #
100
+ # If the optional +on+ keyword is provided, it instead returns the
101
+ # historical exchange rate on the given date.
36
102
  def get_rate(code, on: nil)
37
103
  data = if on
38
104
  historical on: on
@@ -44,31 +110,53 @@ module OXR
44
110
 
45
111
  alias [] get_rate
46
112
 
113
+ ##
114
+ # Returns a Hash mapping currency codes to full currency names.
47
115
  def currencies
48
116
  call configuration.currencies
49
117
  end
50
118
 
119
+ ##
120
+ # Returns a Hash mapping currency codes to their historical exchange rate
121
+ # on the date given by the +on+ keyword argument.
122
+ #
123
+ # The exchange rate is relative to the configured base currency.
51
124
  def historical(on:)
52
125
  call configuration.historical on
53
126
  end
54
127
 
128
+ ##
129
+ # Returns a Hash mapping currency codes to their latest exchange rate.
130
+ #
131
+ # The exchange rate is relative to the configured base currency.
55
132
  def latest
56
133
  call configuration.latest
57
134
  end
58
135
 
136
+ ##
137
+ # Returns a Hash containinng details about the Open Exchange Rates account
138
+ # and current usage statistics.
59
139
  def usage
60
140
  call configuration.usage
61
141
  end
62
142
 
143
+ ##
144
+ # Resets API endpoints to their defaults.
63
145
  def reset_sources
64
146
  configure(&:reset_sources)
65
147
  end
66
148
 
149
+ ##
150
+ # Returns the OXR configuration struct.
151
+ #
152
+ # If given a block, the configuration will be yielded to the block.
67
153
  def configure
68
154
  yield configuration if block_given?
69
155
  configuration
70
156
  end
71
157
 
158
+ ##
159
+ # Returns the OXR configuration struct.
72
160
  def configuration
73
161
  @configuration ||= Configuration.new
74
162
  end
@@ -76,7 +164,9 @@ module OXR
76
164
  private
77
165
 
78
166
  def call(endpoint)
79
- JSON.parse(open(endpoint).read)
167
+ uri = URI.parse endpoint
168
+ data = uri.scheme ? uri.read : File.read(uri.path)
169
+ JSON.parse data
80
170
  rescue OpenURI::HTTPError => e
81
171
  raise ApiError, e
82
172
  end
@@ -1,18 +1,55 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
 
3
5
  module OXR
6
+ ##
7
+ # A container for OXR configuration options.
8
+ #
9
+ # Stores the API application ID and the base currency for which to request
10
+ # exchange rates.
11
+ #
12
+ # Also stores the URI endpoints for fetching currencies, the latest exchange
13
+ # ranges, historical exchange rates, and current account usage statistics.
14
+ #
15
+ # By default, endpoints are generated dynamically, allowing them to
16
+ # automatically adapte to changes to the app_id, but they may be set to a
17
+ # fixed path, including a local file path. This is useful during testing when
18
+ # you might want deterministic results and do not want to waste real API
19
+ # requests.
4
20
  class Configuration
5
- ENDPOINT = 'https://openexchangerates.org/api/'.freeze
21
+ ENDPOINT = 'https://openexchangerates.org/api/'
22
+
23
+ ##
24
+ # Get and set the application ID that will be sent to the API server.
25
+ attr_accessor :app_id
26
+
27
+ ##
28
+ # Get and set the base currency to be used when fetching exchange rates.
29
+ attr_accessor :base
6
30
 
7
- attr_accessor :app_id, :base
31
+ ##
32
+ # Set respective API endpoints. Use these if you want to sidestep the API
33
+ # server, e.g., for testing.
8
34
  attr_writer :currencies, :historical, :latest, :usage
9
35
 
36
+ def initialize
37
+ reset_sources
38
+ end
39
+
40
+ ##
41
+ # Returns the endpoint for listing known currencies.
10
42
  def currencies
11
43
  @currencies || URI.join(ENDPOINT, 'currencies.json').tap do |uri|
12
44
  uri.query = "app_id=#{app_id}"
13
45
  end.to_s
14
46
  end
15
47
 
48
+ ##
49
+ # Returns the endpoint for historical currency exchange rates on the given
50
+ # date.
51
+ #
52
+ # Expects +date+ to respond #strftime.
16
53
  def historical(date)
17
54
  @historical || URI.join(ENDPOINT, "historical/#{date.strftime('%F')}.json").tap do |uri|
18
55
  uri.query = "app_id=#{app_id}"
@@ -20,6 +57,8 @@ module OXR
20
57
  end.to_s
21
58
  end
22
59
 
60
+ ##
61
+ # Returns the endpoint for the latest currency exchange rates.
23
62
  def latest
24
63
  @latest || URI.join(ENDPOINT, 'latest.json').tap do |uri|
25
64
  uri.query = "app_id=#{app_id}"
@@ -27,12 +66,16 @@ module OXR
27
66
  end.to_s
28
67
  end
29
68
 
69
+ ##
70
+ # Returns the endpoint for fetch current API usage statistics.
30
71
  def usage
31
72
  @usage || URI.join(ENDPOINT, 'usage.json').tap do |uri|
32
73
  uri.query = "app_id=#{app_id}"
33
74
  end.to_s
34
75
  end
35
76
 
77
+ ##
78
+ # Resets all API endpoints back to their default (dynamic generation).
36
79
  def reset_sources
37
80
  @currencies = nil
38
81
  @historical = nil
data/lib/oxr/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OXR
2
- VERSION = '0.5.0'.freeze
4
+ VERSION = '0.6.0'
3
5
  end
data/oxr.gemspec CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'oxr/version'
@@ -19,14 +18,14 @@ Gem::Specification.new do |spec|
19
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
19
  spec.require_paths = ['lib']
21
20
 
22
- spec.required_ruby_version = '>= 2.1'
21
+ spec.required_ruby_version = '>= 2.5'
23
22
 
24
- spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'bundler', '>= 1.17'
25
24
  spec.add_development_dependency 'minitest', '~> 5.0'
26
- spec.add_development_dependency 'minitest-focus'
27
25
  spec.add_development_dependency 'pry'
28
- spec.add_development_dependency 'rake', '~> 10.0'
29
- spec.add_development_dependency 'webmock', '~> 2.3'
26
+ spec.add_development_dependency 'rake', '~> 13.0'
27
+ spec.add_development_dependency 'rubocop'
28
+ spec.add_development_dependency 'webmock', '~> 3.0'
30
29
 
31
30
  spec.add_dependency 'json'
32
31
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Parker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-16 00:00:00.000000000 Z
11
+ date: 2021-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.11'
19
+ version: '1.17'
20
20
  type: :development
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: '1.11'
26
+ version: '1.17'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest-focus
42
+ name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,47 +53,47 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: pry
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '13.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: webmock
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '2.3'
89
+ version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '2.3'
96
+ version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: json
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +116,9 @@ extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
118
  - ".gitignore"
119
+ - ".gitlab-ci.yml"
120
+ - ".rubocop.yml"
121
+ - ".tool-versions"
119
122
  - ".travis.yml"
120
123
  - CHANGELOG.md
121
124
  - Gemfile
@@ -140,15 +143,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
143
  requirements:
141
144
  - - ">="
142
145
  - !ruby/object:Gem::Version
143
- version: '2.1'
146
+ version: '2.5'
144
147
  required_rubygems_version: !ruby/object:Gem::Requirement
145
148
  requirements:
146
149
  - - ">="
147
150
  - !ruby/object:Gem::Version
148
151
  version: '0'
149
152
  requirements: []
150
- rubyforge_project:
151
- rubygems_version: 2.7.3
153
+ rubygems_version: 3.2.15
152
154
  signing_key:
153
155
  specification_version: 4
154
156
  summary: Interface for Open Exchange Rates API.