exchangerateapinet 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +73 -0
- data/lib/exchangerateapi/client.rb +46 -0
- data/lib/exchangerateapi/version.rb +5 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f84083556736ef031f6f5cb2ced5f793a2767adb2dcc284c4caef0c1a2be4989
|
|
4
|
+
data.tar.gz: 6d7daf56e32206da3ce5d56702b0204f095579a55d47b3f73a4c9f270994edd0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 75587b674c59b556ab28e45aaf9efffe544c607f3e375128ff3dddf9925032b151c381460f96c45fd8fed4d45c5c2b29c2fb250c778aaad30cb537766ecc1c95
|
|
7
|
+
data.tar.gz: cdda2f9ed515c1ed6dc4604193920b158b7fa8ddec8f4968c6967e3ef0cddca20cef6699383418acd4c691befe3af8705a03756acb36c6f764e97c4677e43ea6
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# exchangerateapi-ruby
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/exchangerateapi-ruby)
|
|
4
|
+
[](https://rubygems.org/gems/exchangerateapi-ruby)
|
|
5
|
+
[](https://github.com/exchangerateapinet/exchangerateapi-ruby/releases)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
Small Ruby client for exchangerateapi.net with straightforward methods and no external dependencies.
|
|
9
|
+
|
|
10
|
+
- Website: [exchangerateapi.net](https://exchangerateapi.net)
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
gem install exchangerateapinet
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
require "exchangerateapi/client"
|
|
22
|
+
client = Exchangerateapi::Client.new(api_key: "YOUR_API_KEY")
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage patterns
|
|
26
|
+
|
|
27
|
+
- Get the newest rates for a base currency; optionally select specific symbols.
|
|
28
|
+
- Look up historical rates for a given date and base.
|
|
29
|
+
|
|
30
|
+
### Configure
|
|
31
|
+
|
|
32
|
+
You can tweak the base URL and provide your own HTTP timeouts by wrapping calls in `Net::HTTP.start` or using a proxy. The client constructs a query-string request and parses JSON.
|
|
33
|
+
|
|
34
|
+
### Latest
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
client.latest(base: "USD")
|
|
38
|
+
client.latest(base: "EUR", symbols: ["USD", "GBP", "JPY"])
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Historical
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
client.historical(date: "2024-01-02", base: "USD")
|
|
45
|
+
client.historical(date: "2024-01-02", base: "EUR", symbols: ["USD", "GBP", "JPY"])
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Error handling
|
|
49
|
+
|
|
50
|
+
When the API returns an error payload, the client raises `StandardError` with a short message. Wrap calls with `begin/rescue` to surface user-friendly messages or to retry.
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
begin
|
|
54
|
+
client.latest(base: "XYZ") # invalid base
|
|
55
|
+
rescue => e
|
|
56
|
+
warn "request failed: #{e.message}"
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Run the examples
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
EXCHANGERATEAPI_KEY=your_api_key ruby examples/latest.rb
|
|
64
|
+
EXCHANGERATEAPI_KEY=your_api_key ruby examples/historical.rb
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Free usage
|
|
68
|
+
|
|
69
|
+
A free tier is available for testing and light workloads. It uses an API key and includes basic access to the latest and historical endpoints with rate limits. Refer to the latest details on [exchangerateapi.net](https://exchangerateapi.net).
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Exchangerateapi
|
|
8
|
+
class Client
|
|
9
|
+
DEFAULT_BASE = "https://api.exchangerateapi.net/v1"
|
|
10
|
+
|
|
11
|
+
def initialize(api_key:, base_url: DEFAULT_BASE)
|
|
12
|
+
raise ArgumentError, "api_key is required" if api_key.to_s.strip.empty?
|
|
13
|
+
@api_key = api_key
|
|
14
|
+
@base_url = base_url.chomp("/")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def latest(base:, symbols: nil)
|
|
18
|
+
raise ArgumentError, "base is required" if base.to_s.strip.empty?
|
|
19
|
+
params = { base: base, apikey: @api_key }
|
|
20
|
+
params[:symbols] = Array(symbols).join(",") if symbols && !Array(symbols).empty?
|
|
21
|
+
get_json("/latest", params)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def historical(date:, base:, symbols: nil)
|
|
25
|
+
raise ArgumentError, "date is required" if date.to_s.strip.empty?
|
|
26
|
+
raise ArgumentError, "base is required" if base.to_s.strip.empty?
|
|
27
|
+
params = { date: date, base: base, apikey: @api_key }
|
|
28
|
+
params[:symbols] = Array(symbols).join(",") if symbols && !Array(symbols).empty?
|
|
29
|
+
get_json("/historical", params)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def get_json(path, params)
|
|
35
|
+
uri = URI.parse(@base_url + path)
|
|
36
|
+
uri.query = URI.encode_www_form(params)
|
|
37
|
+
res = Net::HTTP.get_response(uri)
|
|
38
|
+
body = JSON.parse(res.body)
|
|
39
|
+
if body.is_a?(Hash) && body["error"]
|
|
40
|
+
msg = body["error"]["message"] rescue "API error"
|
|
41
|
+
raise StandardError, msg
|
|
42
|
+
end
|
|
43
|
+
body
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: exchangerateapinet
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- exchangerateapinet
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-10-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Lightweight wrapper offering access to latest and historical endpoints.
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/exchangerateapi/client.rb
|
|
22
|
+
- lib/exchangerateapi/version.rb
|
|
23
|
+
homepage: https://github.com/exchangerateapinet/exchangerateapi-ruby
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata: {}
|
|
27
|
+
post_install_message:
|
|
28
|
+
rdoc_options: []
|
|
29
|
+
require_paths:
|
|
30
|
+
- lib
|
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '2.7'
|
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
requirements: []
|
|
42
|
+
rubygems_version: 3.0.3.1
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: Ruby client for exchangerateapi.net
|
|
46
|
+
test_files: []
|