currency_layer_api 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/.rubocop.yml +18 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/Rakefile +19 -0
- data/lib/currency_layer_api/client.rb +129 -0
- data/lib/currency_layer_api/conversion.rb +15 -0
- data/lib/currency_layer_api/currency.rb +13 -0
- data/lib/currency_layer_api/error.rb +13 -0
- data/lib/currency_layer_api/version.rb +5 -0
- data/lib/currency_layer_api.rb +12 -0
- data/sig/currency_api.rbs +4 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fc7d1dbc9af8e841971b32e23049e8ddb1cfc97a977dbad200a4aa654ea59916
|
4
|
+
data.tar.gz: 87ebf73ab937f0b045f5c46ba680c4b9f8a7904aa0f88eeb302ecb18e2505da7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 738ab73b1e090a6c1b3603690071b8a8a0ae8f77652b3b252e35f6385db4963ee855e3caeca4366bab5961970b14f3306c0f864929f1ff44c5c60fb628eb6fbc
|
7
|
+
data.tar.gz: 46caf05725a4a40153623e24560f1c47e0f8d0d6151ca9f017736ac422fbff66460c86771b7bec12391515a2a8286151af1f53581991ecf755e132c1a98a8ca5
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Style/StringLiterals:
|
7
|
+
Enabled: true
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/StringLiteralsInInterpolation:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Layout/LineLength:
|
15
|
+
Max: 120
|
16
|
+
|
17
|
+
Lint/RedundantCopDisableDirective:
|
18
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sulman@hey.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
currency_layer_api (0.1.0)
|
5
|
+
faraday (~> 2.5)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
faraday (2.5.2)
|
12
|
+
faraday-net_http (>= 2.0, < 3.1)
|
13
|
+
ruby2_keywords (>= 0.0.4)
|
14
|
+
faraday-net_http (3.0.0)
|
15
|
+
json (2.6.2)
|
16
|
+
minitest (5.16.3)
|
17
|
+
parallel (1.22.1)
|
18
|
+
parser (3.1.2.1)
|
19
|
+
ast (~> 2.4.1)
|
20
|
+
rainbow (3.1.1)
|
21
|
+
rake (13.0.6)
|
22
|
+
regexp_parser (2.5.0)
|
23
|
+
rexml (3.2.5)
|
24
|
+
rubocop (1.35.1)
|
25
|
+
json (~> 2.3)
|
26
|
+
parallel (~> 1.10)
|
27
|
+
parser (>= 3.1.2.1)
|
28
|
+
rainbow (>= 2.2.2, < 4.0)
|
29
|
+
regexp_parser (>= 1.8, < 3.0)
|
30
|
+
rexml (>= 3.2.5, < 4.0)
|
31
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
32
|
+
ruby-progressbar (~> 1.7)
|
33
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
34
|
+
rubocop-ast (1.21.0)
|
35
|
+
parser (>= 3.1.1.0)
|
36
|
+
ruby-progressbar (1.11.0)
|
37
|
+
ruby2_keywords (0.0.5)
|
38
|
+
unicode-display_width (2.2.0)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
arm64-darwin-21
|
42
|
+
x86_64-linux
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
currency_layer_api!
|
46
|
+
minitest (~> 5.16, >= 5.16.3)
|
47
|
+
rake (~> 13.0)
|
48
|
+
rubocop (~> 1.35)
|
49
|
+
|
50
|
+
BUNDLED WITH
|
51
|
+
2.3.21
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Sulman Baig
|
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,141 @@
|
|
1
|
+
# CurrencyLayerApi
|
2
|
+
|
3
|
+
Currency Layer API is a light wrapper for [currencylayer.com](https://currencylayer.com/) for Rubyists.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add currency_layer_api
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install currency_layer_api
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
### API Documentation
|
18
|
+
|
19
|
+
The API documentation can be found [here](https://currencylayer.com/documentation).
|
20
|
+
|
21
|
+
### Get Account Information
|
22
|
+
|
23
|
+
Create an account on [currencylayer.com](https://currencylayer.com/) and get your API access key.
|
24
|
+
Save the access key in an environment variable named `CURRENCY_LAYER_ACCESS_KEY`.
|
25
|
+
|
26
|
+
### Configure
|
27
|
+
|
28
|
+
Currency Layer does not allow you ssl endpoint unless you have a paid account. By default ssl is enabled. To disable ssl, set the `ssl` option to `false`.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'currency_layer_api'
|
32
|
+
client = CurrencyLayerApi::Client.new(access_key: ENV['CURRENCY_LAYER_ACCESS_KEY'], ssl: true)
|
33
|
+
```
|
34
|
+
params:
|
35
|
+
- `access_key` - Required - String - Your API access key.
|
36
|
+
- `ssl` - Optional - Boolean - Enable or disable ssl. Default is `true`.
|
37
|
+
|
38
|
+
response:
|
39
|
+
```bash
|
40
|
+
#<CurrencyLayerApi::Client:0x00007f9b0a0b0a00 @access_key="YOUR_ACCESS_KEY", @ssl=true>
|
41
|
+
```
|
42
|
+
|
43
|
+
### Get List of currencies
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
client.list
|
47
|
+
```
|
48
|
+
|
49
|
+
response:
|
50
|
+
|
51
|
+
```bash
|
52
|
+
[
|
53
|
+
#<CurrencyLayerApi::Currency:0x00007f9b0a0b0a00 @code="AED", @name="United Arab Emirates Dirham">,
|
54
|
+
#<CurrencyLayerApi::Currency:0x00007f9b0a0b09c0 @code="AFN", @name="Afghan Afghani">,
|
55
|
+
...
|
56
|
+
]
|
57
|
+
```
|
58
|
+
|
59
|
+
### Get Live Exchange Rates
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
client.live(from: "USD", to: ["AED", "EUR"])
|
63
|
+
```
|
64
|
+
params:
|
65
|
+
- `from` Required - String - The currency you want to convert from. (default: USD)
|
66
|
+
- `to` Optional - Array[String] - The currencies you want to convert to. (default: nil)
|
67
|
+
|
68
|
+
response:
|
69
|
+
|
70
|
+
```bash
|
71
|
+
[
|
72
|
+
#<CurrencyLayerApi::Conversion:0x00007f9b0a0b0a00 @from_code="USD", @to_code="AED", @value=3.6725, @date="2019-01-01">,
|
73
|
+
#<CurrencyLayerApi::Conversion:0x00007f9b0a0b09c0 @from_code="USD", @to_code="EUR", @value=0.888, @date="2019-01-01">,
|
74
|
+
]
|
75
|
+
```
|
76
|
+
|
77
|
+
### Get Historical Exchange Rates
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
client.historical(from: "USD", to: ["AED", "EUR"], date: "2019-01-01")
|
81
|
+
```
|
82
|
+
params:
|
83
|
+
- `from` Required - String - The currency you want to convert from. (default: USD)
|
84
|
+
- `to` Optional - Array[String] - The currencies you want to convert to. (default: nil)
|
85
|
+
- `date` Required - String 'YYYY-MM-DD' - The date you want to get the exchange rates for. (default: nil)
|
86
|
+
|
87
|
+
response:
|
88
|
+
|
89
|
+
```bash
|
90
|
+
[
|
91
|
+
#<CurrencyLayerApi::Conversion:0x00007f9b0a0b0a00 @from_code="USD", @to_code="AED", @value=3.6725, @date="2019-01-01">,
|
92
|
+
#<CurrencyLayerApi::Conversion:0x00007f9b0a0b09c0 @from_code="USD", @to_code="EUR", @value=0.888, @date="2019-01-01">,
|
93
|
+
]
|
94
|
+
```
|
95
|
+
|
96
|
+
### Get Conversion Rate given amount
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
client.convert(from: "USD", to: "AED", amount: 100)
|
100
|
+
```
|
101
|
+
|
102
|
+
params:
|
103
|
+
- `from` Required - String - The currency you want to convert from. (default: USD)
|
104
|
+
- `to` Required - String - The currency you want to convert to. (default: nil)
|
105
|
+
- `amount` Required - Int - The amount you want to convert. (default: nil)
|
106
|
+
- `date` Optional - String 'YYYY-MM-DD' - The date you want to get the exchange rates for. (default: nil)
|
107
|
+
|
108
|
+
response:
|
109
|
+
|
110
|
+
```bash
|
111
|
+
367.25
|
112
|
+
```
|
113
|
+
|
114
|
+
### Error Handling
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
begin
|
118
|
+
client.live(from: "USD", to: ["AED", "EUR"])
|
119
|
+
rescue CurrencyLayerApi::Error => e
|
120
|
+
puts e.code
|
121
|
+
puts e.message
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
## Development
|
126
|
+
|
127
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
128
|
+
|
129
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/currency_layer_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/currency_layer_api/blob/main/CODE_OF_CONDUCT.md).
|
134
|
+
|
135
|
+
## License
|
136
|
+
|
137
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
138
|
+
|
139
|
+
## Code of Conduct
|
140
|
+
|
141
|
+
Everyone interacting in the CurrencyLayerApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/currency_layer_api/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rubocop/rake_task"
|
5
|
+
require "rake/testtask"
|
6
|
+
|
7
|
+
RuboCop::RakeTask.new
|
8
|
+
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << "test"
|
11
|
+
t.libs << "lib"
|
12
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
13
|
+
end
|
14
|
+
|
15
|
+
task default: :rubocop
|
16
|
+
|
17
|
+
task :console do
|
18
|
+
exec "irb -I lib -r currency_layer_api.rb"
|
19
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# rubocop:disable Layout/LineLength
|
4
|
+
module CurrencyLayerApi
|
5
|
+
# Client having API methods and access_keys
|
6
|
+
class Client
|
7
|
+
BASE_URL = "api.currencylayer.com/"
|
8
|
+
|
9
|
+
attr_reader :access_key, :adapter
|
10
|
+
|
11
|
+
# #### params:
|
12
|
+
# access_key: String (Required)
|
13
|
+
# ssl: Boolean (Optional) (Default: true)
|
14
|
+
# adapter: Symbol (Optional) (Default: :net_http)
|
15
|
+
# stubs: Faraday::Adapter::Test::Stubs (Optional) (Default: nil)
|
16
|
+
# #### Example:
|
17
|
+
# CurrencyLayerApi::Client.new(access_key: "fake", ssl: false, adapter: :test, stubs: stub)
|
18
|
+
# CurrencyLayerApi::Client.new(access_key: "fake")
|
19
|
+
# #### Description:
|
20
|
+
# Creates a new instance of CurrencyLayerApi::Client with access_key provided by currencylayer.com
|
21
|
+
def initialize(access_key:, ssl: true, adapter: Faraday.default_adapter, stubs: nil)
|
22
|
+
@access_key = access_key
|
23
|
+
@adapter = adapter
|
24
|
+
@ssl = ssl
|
25
|
+
|
26
|
+
# Test stubs for requests
|
27
|
+
@stubs = stubs
|
28
|
+
end
|
29
|
+
|
30
|
+
# #### Example:
|
31
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").list
|
32
|
+
# #### Response:
|
33
|
+
# [
|
34
|
+
# #<CurrencyLayerApi::Currency:0x00007f9b0a0b0a00 @code="AED", @name="United Arab Emirates Dirham">,
|
35
|
+
# #<CurrencyLayerApi::Currency:0x00007f9b0a0b09c0 @code="AFN", @name="Afghan Afghani">,
|
36
|
+
# ]
|
37
|
+
# #### Description:
|
38
|
+
# Resturns currency object array for all currencies available in currency layer
|
39
|
+
def list
|
40
|
+
get("list").body["currencies"].map { |key, val| Currency.new(code: key, name: val) }
|
41
|
+
end
|
42
|
+
|
43
|
+
# #### Params:
|
44
|
+
# from: String (Required) (Default: "USD")
|
45
|
+
# to: Array (Optional) (Default: nil)
|
46
|
+
# #### Example:
|
47
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").live(from: "USD", to: ["AED", "EUR"])
|
48
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").live(from: "EUR")
|
49
|
+
# #### Response:
|
50
|
+
# [
|
51
|
+
# #<CurrencyLayerApi::Conversion:0x00007f9b0a0b0a00 @from_code="USD", @to_code="AED", @value=3.6725, @date="2019-01-01">,
|
52
|
+
# #<CurrencyLayerApi::Conversion:0x00007f9b0a0b09c0 @from_code="USD", @to_code="EUR", @value=0.888, @date="2019-01-01">,
|
53
|
+
# ]
|
54
|
+
# #### Description:
|
55
|
+
# Returns the current exchange rate for a given from currency and to currencies
|
56
|
+
def live(from: "USD", to: nil)
|
57
|
+
get("live", { source: from, currencies: to ? to.join(",") : nil }.compact).body["quotes"].map do |key, val|
|
58
|
+
Conversion.new(from_code: key[0..2], to_code: key[3..], value: val, date: Date.today.to_s)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# #### Params:
|
63
|
+
# date: String (Required) ('YYYY-MM-DD')
|
64
|
+
# from: String (Required) (Default: "USD")
|
65
|
+
# to: Array (Optional) (Default: nil)
|
66
|
+
# #### Example:
|
67
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").historical(date: "2019-01-01", from: "USD", to: ["AED", "EUR"])
|
68
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").historical(date: "2019-01-01", from: "EUR")
|
69
|
+
# #### Response:
|
70
|
+
# [
|
71
|
+
# #<CurrencyLayerApi::Conversion:0x00007f9b0a0b0a00 @from_code="USD", @to_code="AED", @value=3.6725, @date="2019-01-01">,
|
72
|
+
# #<CurrencyLayerApi::Conversion:0x00007f9b0a0b09c0 @from_code="USD", @to_code="EUR", @value=0.888, @date="2019-01-01">,
|
73
|
+
# ]
|
74
|
+
# #### Description:
|
75
|
+
# Returns the historical exchange rate for a given date, from currency and to currencies
|
76
|
+
def historical(date:, from: "USD", to: nil)
|
77
|
+
resp = get("historical", { date: date, source: from, currencies: to ? to.join(",") : nil }.compact)
|
78
|
+
resp.body["quotes"].map do |key, val|
|
79
|
+
Conversion.new(from_code: key[0..2], to_code: key[3..], value: val, date: resp.body["date"])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# #### Params:
|
84
|
+
# to: String (Required) (Currency code)
|
85
|
+
# from: String (Required) (Currency code)
|
86
|
+
# amount: Int (Required) (Default: 1)
|
87
|
+
# date: String (Optional) ('YYYY-MM-DD')
|
88
|
+
# #### Example:
|
89
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").convert(to: "AED", from: "USD", amount: 1)
|
90
|
+
# CurrencyLayerApi::Client.new(access_key: "fake").convert(to: "AED", from: "USD", amount: 1, date: "2019-01-01")
|
91
|
+
# #### Response:
|
92
|
+
# 3.6725
|
93
|
+
# #### Description:
|
94
|
+
# Returns the exchange rate for a given date, from currency and to currency
|
95
|
+
def convert(to:, from:, amount: 1, date: nil)
|
96
|
+
get("convert", { from: from, to: to, amount: amount, date: date }.compact).body["result"]
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
# creates a faraday connection with base url and access_key
|
102
|
+
def connection
|
103
|
+
@connection ||= Faraday.new("#{@ssl ? "https" : "http"}://#{BASE_URL}") do |conn|
|
104
|
+
conn.params["access_key"] = access_key
|
105
|
+
conn.request :json
|
106
|
+
|
107
|
+
conn.response :json, content_type: "application/json"
|
108
|
+
|
109
|
+
conn.adapter adapter, @stubs
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# makes a get request to the given path with params
|
114
|
+
def get(path, params = {})
|
115
|
+
handle_response connection.get(path, params)
|
116
|
+
end
|
117
|
+
|
118
|
+
# handles the response from the api and raises error if any
|
119
|
+
# returns the response if no error
|
120
|
+
# Error codes: https://currencylayer.com/documentation
|
121
|
+
# error.code: 101, error.message: "You have not supplied an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY]"
|
122
|
+
def handle_response(response)
|
123
|
+
return response if response.body["success"] == true
|
124
|
+
|
125
|
+
raise Error.new(response.body["error"]["info"], response.body["error"]["code"])
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
# rubocop:enable Layout/LineLength
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CurrencyLayerApi
|
4
|
+
# Conversion Class for CurrencyLayerApi
|
5
|
+
class Conversion
|
6
|
+
attr_reader :from_code, :to_code, :value, :date
|
7
|
+
|
8
|
+
def initialize(from_code:, to_code:, value:, date: nil)
|
9
|
+
@from_code = from_code
|
10
|
+
@to_code = to_code
|
11
|
+
@value = value
|
12
|
+
@date = date
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "currency_layer_api/version"
|
4
|
+
require "faraday"
|
5
|
+
|
6
|
+
# :nodoc:
|
7
|
+
module CurrencyLayerApi
|
8
|
+
autoload :Client, "currency_layer_api/client"
|
9
|
+
autoload :Error, "currency_layer_api/error"
|
10
|
+
autoload :Currency, "currency_layer_api/currency"
|
11
|
+
autoload :Conversion, "currency_layer_api/conversion"
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: currency_layer_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sulman Baig
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.35'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.35'
|
41
|
+
description: Currency Layer API is a light wrapper for currencylayer.com API for Rubyists.
|
42
|
+
It gives nice struct objects for currencies and conversions. It uses Faraday for
|
43
|
+
HTTP requests.
|
44
|
+
email:
|
45
|
+
- sulman@hey.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files:
|
49
|
+
- README.md
|
50
|
+
files:
|
51
|
+
- ".rubocop.yml"
|
52
|
+
- CHANGELOG.md
|
53
|
+
- CODE_OF_CONDUCT.md
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/currency_layer_api.rb
|
60
|
+
- lib/currency_layer_api/client.rb
|
61
|
+
- lib/currency_layer_api/conversion.rb
|
62
|
+
- lib/currency_layer_api/currency.rb
|
63
|
+
- lib/currency_layer_api/error.rb
|
64
|
+
- lib/currency_layer_api/version.rb
|
65
|
+
- sig/currency_api.rbs
|
66
|
+
homepage: https://github.com/sulmanweb/currency_layer_api
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata:
|
70
|
+
homepage_uri: https://github.com/sulmanweb/currency_layer_api
|
71
|
+
source_code_uri: https://github.com/sulmanweb/currency_layer_api
|
72
|
+
changelog_uri: https://github.com/sulmanweb/currency_layer_api/blob/main/CHANGELOG.md
|
73
|
+
rubygems_mfa_required: 'true'
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.6'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubygems_version: 3.3.21
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Currency Layer API is a light wrapper for currencylayer.com API for Rubyists.
|
93
|
+
test_files: []
|