fixer_api_client 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/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fixer_api_client.gemspec +36 -0
- data/lib/data/currency_symbols.json +1 -0
- data/lib/data/rates_data.json +1 -0
- data/lib/fixer_api_client.rb +27 -0
- data/lib/fixer_api_client/connection.rb +21 -0
- data/lib/fixer_api_client/currency.rb +53 -0
- data/lib/fixer_api_client/currency_data.rb +99 -0
- data/lib/fixer_api_client/numeric_overloader.rb +9 -0
- data/lib/fixer_api_client/version.rb +3 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f104cf50f2383892a037f8c421d2e4e751d112fc1d68dc4249bf523e28a23704
|
4
|
+
data.tar.gz: 3880c7c484df15354e203df6f4dcf2f89c148a19e01c7565f6d56c9385d991e9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a20a563cf5165588aadd6032ee41c798b90c8c58eaf574bbf722dc99f00a7ad33e30a182c17995d6f45da43a48465ad4e6a4ca71703a33277dda2b66456aed3
|
7
|
+
data.tar.gz: 047f6d353cbc1ba14572809eef76e3c85ff91bc6bb64ba7b86f834e3c50719a5a5baab6b0fbe4e924da07e375b496354f0a3ace5cf35b057772b673a2b3d2c6d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Jhon Quintero
|
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,73 @@
|
|
1
|
+
# FixerApiClient
|
2
|
+
|
3
|
+
- Just a gem to interact with *Fixer IO API*
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'fixer_api_client'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install fixer_api_client
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
- This gem will make currency conversions using [Fixer IO API](https://fixer.io).
|
24
|
+
|
25
|
+
- You need to add `FIXER_IO_API_KEY` and `FIXER_IO_API_URL` env vars. You can get api key [here](https://fixer.io/product). The api key is necesssary to update rates on a daily basis
|
26
|
+
- NOTE: I made this based this with a free account, so no https available for me, that's why you can pass the api url as an env var.
|
27
|
+
|
28
|
+
- The gem will inject methods to numeric values, example
|
29
|
+
```ruby
|
30
|
+
>> 1.usd
|
31
|
+
=> 1 United States Dollar
|
32
|
+
```
|
33
|
+
|
34
|
+
- You can also make conversions on the fly:
|
35
|
+
```ruby
|
36
|
+
>> 2.usd.to_cop
|
37
|
+
=> 8253.28 Colombian Peso
|
38
|
+
```
|
39
|
+
|
40
|
+
- If you want to look at available conversion methods
|
41
|
+
```ruby
|
42
|
+
FixerApiClient::available_conversions
|
43
|
+
```
|
44
|
+
|
45
|
+
- Or the available currencies
|
46
|
+
```ruby
|
47
|
+
FixerApiClient::available_currencies
|
48
|
+
```
|
49
|
+
|
50
|
+
- And if you want to check when the rates data was updated...
|
51
|
+
```ruby
|
52
|
+
FixerApiClient::rates_creation_date
|
53
|
+
```
|
54
|
+
|
55
|
+
## TODO
|
56
|
+
|
57
|
+
- Update rates on demand
|
58
|
+
- More tests...
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
+
|
64
|
+
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).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mr-sudaca/fixer_api_client.
|
69
|
+
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fixer_api_client"
|
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,36 @@
|
|
1
|
+
require_relative 'lib/fixer_api_client/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "fixer_api_client"
|
5
|
+
spec.version = FixerApiClient::VERSION
|
6
|
+
spec.authors = ["Jhon Quintero"]
|
7
|
+
spec.email = ["jhon@notgmail.co"]
|
8
|
+
|
9
|
+
spec.summary = %q{Another api client}
|
10
|
+
spec.description = %q{Another api client for Fixer API}
|
11
|
+
spec.homepage = "https://github.com/mr-sudaca/fixer_api_client"
|
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/mr-sudaca/fixer_api_client"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/mr-sudaca/fixer_api_client"
|
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
|
+
spec.add_dependency 'faraday'
|
31
|
+
spec.add_dependency 'faraday_middleware'
|
32
|
+
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
34
|
+
spec.add_development_dependency 'rake', '~> 12'
|
35
|
+
spec.add_development_dependency 'byebug'
|
36
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
{"success":true,"symbols":{"AED":"United Arab Emirates Dirham","AFN":"Afghan Afghani","ALL":"Albanian Lek","AMD":"Armenian Dram","ANG":"Netherlands Antillean Guilder","AOA":"Angolan Kwanza","ARS":"Argentine Peso","AUD":"Australian Dollar","AWG":"Aruban Florin","AZN":"Azerbaijani Manat","BAM":"Bosnia-Herzegovina Convertible Mark","BBD":"Barbadian Dollar","BDT":"Bangladeshi Taka","BGN":"Bulgarian Lev","BHD":"Bahraini Dinar","BIF":"Burundian Franc","BMD":"Bermudan Dollar","BND":"Brunei Dollar","BOB":"Bolivian Boliviano","BRL":"Brazilian Real","BSD":"Bahamian Dollar","BTC":"Bitcoin","BTN":"Bhutanese Ngultrum","BWP":"Botswanan Pula","BYN":"New Belarusian Ruble","BYR":"Belarusian Ruble","BZD":"Belize Dollar","CAD":"Canadian Dollar","CDF":"Congolese Franc","CHF":"Swiss Franc","CLF":"Chilean Unit of Account (UF)","CLP":"Chilean Peso","CNY":"Chinese Yuan","COP":"Colombian Peso","CRC":"Costa Rican Colón","CUC":"Cuban Convertible Peso","CUP":"Cuban Peso","CVE":"Cape Verdean Escudo","CZK":"Czech Republic Koruna","DJF":"Djiboutian Franc","DKK":"Danish Krone","DOP":"Dominican Peso","DZD":"Algerian Dinar","EGP":"Egyptian Pound","ERN":"Eritrean Nakfa","ETB":"Ethiopian Birr","EUR":"Euro","FJD":"Fijian Dollar","FKP":"Falkland Islands Pound","GBP":"British Pound Sterling","GEL":"Georgian Lari","GGP":"Guernsey Pound","GHS":"Ghanaian Cedi","GIP":"Gibraltar Pound","GMD":"Gambian Dalasi","GNF":"Guinean Franc","GTQ":"Guatemalan Quetzal","GYD":"Guyanaese Dollar","HKD":"Hong Kong Dollar","HNL":"Honduran Lempira","HRK":"Croatian Kuna","HTG":"Haitian Gourde","HUF":"Hungarian Forint","IDR":"Indonesian Rupiah","ILS":"Israeli New Sheqel","IMP":"Manx pound","INR":"Indian Rupee","IQD":"Iraqi Dinar","IRR":"Iranian Rial","ISK":"Icelandic Króna","JEP":"Jersey Pound","JMD":"Jamaican Dollar","JOD":"Jordanian Dinar","JPY":"Japanese Yen","KES":"Kenyan Shilling","KGS":"Kyrgystani Som","KHR":"Cambodian Riel","KMF":"Comorian Franc","KPW":"North Korean Won","KRW":"South Korean Won","KWD":"Kuwaiti Dinar","KYD":"Cayman Islands Dollar","KZT":"Kazakhstani Tenge","LAK":"Laotian Kip","LBP":"Lebanese Pound","LKR":"Sri Lankan Rupee","LRD":"Liberian Dollar","LSL":"Lesotho Loti","LTL":"Lithuanian Litas","LVL":"Latvian Lats","LYD":"Libyan Dinar","MAD":"Moroccan Dirham","MDL":"Moldovan Leu","MGA":"Malagasy Ariary","MKD":"Macedonian Denar","MMK":"Myanma Kyat","MNT":"Mongolian Tugrik","MOP":"Macanese Pataca","MRO":"Mauritanian Ouguiya","MUR":"Mauritian Rupee","MVR":"Maldivian Rufiyaa","MWK":"Malawian Kwacha","MXN":"Mexican Peso","MYR":"Malaysian Ringgit","MZN":"Mozambican Metical","NAD":"Namibian Dollar","NGN":"Nigerian Naira","NIO":"Nicaraguan Córdoba","NOK":"Norwegian Krone","NPR":"Nepalese Rupee","NZD":"New Zealand Dollar","OMR":"Omani Rial","PAB":"Panamanian Balboa","PEN":"Peruvian Nuevo Sol","PGK":"Papua New Guinean Kina","PHP":"Philippine Peso","PKR":"Pakistani Rupee","PLN":"Polish Zloty","PYG":"Paraguayan Guarani","QAR":"Qatari Rial","RON":"Romanian Leu","RSD":"Serbian Dinar","RUB":"Russian Ruble","RWF":"Rwandan Franc","SAR":"Saudi Riyal","SBD":"Solomon Islands Dollar","SCR":"Seychellois Rupee","SDG":"Sudanese Pound","SEK":"Swedish Krona","SGD":"Singapore Dollar","SHP":"Saint Helena Pound","SLL":"Sierra Leonean Leone","SOS":"Somali Shilling","SRD":"Surinamese Dollar","STD":"São Tomé and Príncipe Dobra","SVC":"Salvadoran Colón","SYP":"Syrian Pound","SZL":"Swazi Lilangeni","THB":"Thai Baht","TJS":"Tajikistani Somoni","TMT":"Turkmenistani Manat","TND":"Tunisian Dinar","TOP":"Tongan Paʻanga","TRY":"Turkish Lira","TTD":"Trinidad and Tobago Dollar","TWD":"New Taiwan Dollar","TZS":"Tanzanian Shilling","UAH":"Ukrainian Hryvnia","UGX":"Ugandan Shilling","USD":"United States Dollar","UYU":"Uruguayan Peso","UZS":"Uzbekistan Som","VEF":"Venezuelan Bolívar Fuerte","VND":"Vietnamese Dong","VUV":"Vanuatu Vatu","WST":"Samoan Tala","XAF":"CFA Franc BEAC","XAG":"Silver (troy ounce)","XAU":"Gold (troy ounce)","XCD":"East Caribbean Dollar","XDR":"Special Drawing Rights","XOF":"CFA Franc BCEAO","XPF":"CFP Franc","YER":"Yemeni Rial","ZAR":"South African Rand","ZMK":"Zambian Kwacha (pre-2013)","ZMW":"Zambian Kwacha","ZWL":"Zimbabwean Dollar"}}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"success":true,"timestamp":1584538746,"base":"EUR","date":"2020-03-18","rates":{"AED":4.009491,"AFN":83.01757,"ALL":121.742968,"AMD":535.472454,"ANG":1.953617,"AOA":552.702066,"ARS":69.054448,"AUD":1.854746,"AWG":1.964916,"AZN":1.857913,"BAM":1.944581,"BBD":2.203632,"BDT":92.586131,"BGN":1.945178,"BHD":0.412207,"BIF":2068.620358,"BMD":1.09162,"BND":1.570544,"BOB":7.514399,"BRL":5.591384,"BSD":1.091401,"BTC":0.000208,"BTN":81.017893,"BWP":12.74296,"BYN":2.689045,"BYR":21395.756741,"BZD":2.200014,"CAD":1.574826,"CDF":1861.212301,"CHF":1.054395,"CLF":0.034019,"CLP":938.793267,"CNY":7.680532,"COP":4504.723755,"CRC":618.054706,"CUC":1.09162,"CUP":28.927936,"CVE":109.762459,"CZK":27.164645,"DJF":194.003054,"DKK":7.473199,"DOP":58.893081,"DZD":132.641709,"EGP":17.189637,"ERN":16.373927,"ETB":35.786384,"EUR":1,"FJD":2.48865,"FKP":0.922139,"GBP":0.9222,"GEL":3.356785,"GGP":0.922139,"GHS":6.102151,"GIP":0.922139,"GMD":55.562463,"GNF":10274.865778,"GTQ":8.348321,"GYD":227.611237,"HKD":8.479144,"HNL":27.192221,"HRK":7.593524,"HTG":103.475468,"HUF":351.600045,"IDR":16881.415812,"ILS":4.177306,"IMP":0.922139,"INR":81.104655,"IQD":1299.028088,"IRR":45962.669904,"ISK":151.768455,"JEP":0.922139,"JMD":147.685696,"JOD":0.773958,"JPY":117.667386,"KES":113.801131,"KGS":79.575162,"KHR":4415.604241,"KMF":489.673512,"KPW":982.51443,"KRW":1375.441741,"KWD":0.338806,"KYD":0.909551,"KZT":484.160289,"LAK":9709.699001,"LBP":1651.62127,"LKR":202.676022,"LRD":216.1951,"LSL":18.110176,"LTL":3.223271,"LVL":0.66031,"LYD":1.533702,"MAD":10.531133,"MDL":19.476334,"MGA":4038.994926,"MKD":61.571301,"MMK":1559.783305,"MNT":3019.219352,"MOP":8.730039,"MRO":389.708761,"MUR":42.581338,"MVR":16.821897,"MWK":802.340768,"MXN":26.14199,"MYR":4.774204,"MZN":72.227034,"NAD":18.109968,"NGN":400.077649,"NIO":37.329937,"NOK":11.727336,"NPR":129.629028,"NZD":1.869067,"OMR":0.420155,"PAB":1.091501,"PEN":3.86789,"PGK":3.719696,"PHP":56.311777,"PKR":172.967317,"PLN":4.503364,"PYG":7182.61612,"QAR":3.974596,"RON":4.843958,"RSD":117.556579,"RUB":86.870107,"RWF":1026.123027,"SAR":4.097551,"SBD":9.025507,"SCR":14.958863,"SDG":60.339342,"SEK":11.023743,"SGD":1.574913,"SHP":0.922139,"SLL":10616.006497,"SOS":638.597557,"SRD":8.141316,"STD":24073.2526,"SVC":9.550664,"SYP":562.183136,"SZL":18.109953,"THB":35.415463,"TJS":10.632574,"TMT":3.831587,"TND":3.146598,"TOP":2.589649,"TRY":7.042391,"TTD":7.374274,"TWD":33.109957,"TZS":2515.721702,"UAH":29.809062,"UGX":4125.534729,"USD":1.09162,"UYU":48.529711,"UZS":10384.035335,"VEF":10.902557,"VND":25413.664808,"VUV":136.126323,"WST":3.046692,"XAF":652.202093,"XAG":0.089672,"XAU":0.000725,"XCD":2.950158,"XDR":0.797608,"XOF":642.418365,"XPF":118.713865,"YER":273.286917,"ZAR":18.525177,"ZMK":9825.89119,"ZMW":18.0903,"ZWL":351.501719}}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "fixer_api_client/version"
|
2
|
+
|
3
|
+
module FixerApiClient
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
def self.available_conversions
|
7
|
+
FixerApiClient::CurrencyData.symbols.map do |symbol, _|
|
8
|
+
"to_#{symbol.downcase}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.available_currencies
|
13
|
+
FixerApiClient::CurrencyData.symbols.map do |symbol, friendly_name|
|
14
|
+
"#{symbol.downcase}: #{friendly_name}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.rates_creation_date
|
19
|
+
FixerApiClient::CurrencyData.rates_creation_date
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'fixer_api_client/connection'
|
23
|
+
require 'fixer_api_client/currency_data'
|
24
|
+
require 'fixer_api_client/numeric_overloader'
|
25
|
+
require 'fixer_api_client/currency'
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'faraday'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
|
5
|
+
module FixerApiClient
|
6
|
+
class Connection
|
7
|
+
|
8
|
+
def self.connection
|
9
|
+
::Faraday.new(url: url) do |conn|
|
10
|
+
conn.request :json
|
11
|
+
conn.adapter :net_http
|
12
|
+
conn.response :json, content_type: /\bjson$/
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.url
|
17
|
+
ENV['FIXER_IO_API_URL'] || 'http://data.fixer.io/api'
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
|
2
|
+
module FixerApiClient
|
3
|
+
class Currency
|
4
|
+
BASE_CURRENCY = 'EUR'
|
5
|
+
BASE_CURRENCY_AMOUNT = 1
|
6
|
+
|
7
|
+
attr_reader :value, :symbol, :friendly_name
|
8
|
+
|
9
|
+
def initialize(value, symbol, friendly_name)
|
10
|
+
@value = value
|
11
|
+
@symbol = symbol
|
12
|
+
@friendly_name = friendly_name
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect
|
17
|
+
"#{value} #{friendly_name}"
|
18
|
+
end
|
19
|
+
|
20
|
+
FixerApiClient::CurrencyData.symbols.each do |symbol, friendly_name|
|
21
|
+
define_method "to_#{symbol.downcase}" do
|
22
|
+
new_value =
|
23
|
+
(to_base * currency_rates[symbol.upcase]).round(2)
|
24
|
+
|
25
|
+
self.class.new(new_value, symbol.downcase, friendly_name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
['+', '-', '*', '/'].each do |operator|
|
30
|
+
define_method operator do |other_value|
|
31
|
+
new_value = value.public_send(operator, other_value)
|
32
|
+
self.class.new(new_value, symbol, friendly_name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# this will convert the value we have to the base currency (EUR) because
|
39
|
+
# API free tier only allows to set EUR as base, so, let's say we have
|
40
|
+
# 1 USD, this, will convert it to 0.80 EUR
|
41
|
+
def to_base
|
42
|
+
value / current_rate
|
43
|
+
end
|
44
|
+
|
45
|
+
def current_rate
|
46
|
+
currency_rates[symbol.upcase]
|
47
|
+
end
|
48
|
+
|
49
|
+
def currency_rates
|
50
|
+
@currency_rates ||= FixerApiClient::CurrencyData.rates
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'byebug'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module FixerApiClient
|
6
|
+
class CurrencyData
|
7
|
+
|
8
|
+
def self.rates
|
9
|
+
new.rates
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.symbols
|
13
|
+
new.symbols
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.rates_creation_date
|
17
|
+
new.rates_creation_date
|
18
|
+
end
|
19
|
+
|
20
|
+
def rates_creation_date
|
21
|
+
timestamp = parse(rates_filename, 'rates')['timestamp']
|
22
|
+
timestamp && Time.strptime(timestamp.to_s,'%s').utc.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def rates
|
26
|
+
@rates ||=
|
27
|
+
if tests?
|
28
|
+
{ 'FOO' => 3, 'BAR' => 1.5 }
|
29
|
+
else
|
30
|
+
parse(rates_filename, 'rates')['rates']
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def symbols
|
35
|
+
@symbols ||=
|
36
|
+
if tests?
|
37
|
+
{ 'FOO' => 'foo currency', 'BAR' => 'bar currency' }
|
38
|
+
else
|
39
|
+
parse(currencies_filename, 'symbols')['symbols']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def parse(filename, data_path)
|
46
|
+
data =
|
47
|
+
if update?(filename)
|
48
|
+
results = connection.get("#{url_for(data_path)}?access_key=#{api_key}")
|
49
|
+
if results.status == 200
|
50
|
+
file = File.open(filename, 'w')
|
51
|
+
file.puts(results.body.to_json)
|
52
|
+
file.close
|
53
|
+
results.body.to_json
|
54
|
+
else
|
55
|
+
File.read(filename)
|
56
|
+
end
|
57
|
+
else
|
58
|
+
File.read(filename)
|
59
|
+
end
|
60
|
+
|
61
|
+
JSON.parse(data)
|
62
|
+
end
|
63
|
+
|
64
|
+
def url_for(path)
|
65
|
+
case path
|
66
|
+
when 'rates' then '/latest'
|
67
|
+
when 'symbols' then '/symbols'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def update?(filename)
|
72
|
+
!File.exists?(filename) ||
|
73
|
+
File.zero?(filename) ||
|
74
|
+
((Time.now.utc - File.mtime(filename).utc) / 60 / 60) > 24
|
75
|
+
end
|
76
|
+
|
77
|
+
def tests?
|
78
|
+
ENV['fixer_api_client_tests'].to_s == 'true'
|
79
|
+
end
|
80
|
+
|
81
|
+
def rates_filename
|
82
|
+
@rates_filename ||= File.join(File.dirname(__dir__), 'data', 'rates_data.json')
|
83
|
+
end
|
84
|
+
|
85
|
+
def currencies_filename
|
86
|
+
@currencies_filename ||= File.join(File.dirname(__dir__), 'data', 'currency_symbols.json')
|
87
|
+
end
|
88
|
+
|
89
|
+
def connection
|
90
|
+
@connection ||= FixerApiClient::Connection.connection
|
91
|
+
end
|
92
|
+
|
93
|
+
def api_key
|
94
|
+
raise 'You need an api key' unless ENV['FIXER_IO_API_KEY']
|
95
|
+
|
96
|
+
ENV['FIXER_IO_API_KEY']
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixer_api_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jhon Quintero
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-18 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: '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: faraday_middleware
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Another api client for Fixer API
|
84
|
+
email:
|
85
|
+
- jhon@notgmail.co
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- fixer_api_client.gemspec
|
100
|
+
- lib/data/currency_symbols.json
|
101
|
+
- lib/data/rates_data.json
|
102
|
+
- lib/fixer_api_client.rb
|
103
|
+
- lib/fixer_api_client/connection.rb
|
104
|
+
- lib/fixer_api_client/currency.rb
|
105
|
+
- lib/fixer_api_client/currency_data.rb
|
106
|
+
- lib/fixer_api_client/numeric_overloader.rb
|
107
|
+
- lib/fixer_api_client/version.rb
|
108
|
+
homepage: https://github.com/mr-sudaca/fixer_api_client
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata:
|
112
|
+
homepage_uri: https://github.com/mr-sudaca/fixer_api_client
|
113
|
+
source_code_uri: https://github.com/mr-sudaca/fixer_api_client
|
114
|
+
changelog_uri: https://github.com/mr-sudaca/fixer_api_client
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 2.3.0
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.1.2
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Another api client
|
134
|
+
test_files: []
|