lariconv 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE +21 -0
  4. data/README.md +33 -0
  5. data/lib/lariconv.rb +67 -7
  6. metadata +23 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a90d47353f67337f935248d3fbaf1ae461c9783b1061c2ad518b05043779bf8
4
- data.tar.gz: ba924d182e982e68d5c4db84e19faeec7de2487e0e50ad0e6b78796a2d4c8a04
3
+ metadata.gz: 11f69bda9dc9a3e1dbc43ed9e1e0b3922e4e1086f054e32235b03f1ee3d1187c
4
+ data.tar.gz: defc56c19133dee86f671bcb864cac005541e7fde4911f070b9f9dc8c7f6acac
5
5
  SHA512:
6
- metadata.gz: 5d7e2f5837856d4070b6b451cb811b08955639b1b41c34275b373ce4e6f05b65b7f614cffa7bdb884433b7756dac970a27053ef499f73757919b9e18018bf543
7
- data.tar.gz: 1f597a15f4a1e6e3476b17e9e525e31629c6d37223dc10ebb11136fd676dd85be7c866be86a760b3f5e129169f9569f3132e7ec46b95cbfc201b7dd13e678279
6
+ metadata.gz: 5e63a48e06efc6d4af84e57213be099f24aaf780d9d711eb930fdd1f3d7c428c7f8f2d19444629c00220409a9e843a5594dc868147495b0f5838ee63f02979c1
7
+ data.tar.gz: ae822d0f0ecbe6ba16c687312d17640ca5980eca6f5c384c6435874b588e7d8917a4a5d174953bb6a03ca3cac3fa7444d12a2b5d4f4dcd601edd9f7b09a94996
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ `lariconv` changelog
2
+ ====================
3
+
4
+ ## 0.2.0 (August 15, 2023)
5
+
6
+ * Change `Lariconv#convert` signature
7
+ * Add exception handling
8
+ * Restrict list of currencies to reasonable
9
+
10
+
11
+ ## 0.1.0 (August 13, 2023)
12
+
13
+ * Initial gem release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Den Patin
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,33 @@
1
+ `lariconv`
2
+ ==========
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/lariconv.svg)](https://badge.fury.io/rb/lariconv)
5
+
6
+ Gem to convert different currencies to Georgian Lari based on
7
+ the National Bank of Georgia's exchange rate for a given date.
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ Lariconv.convert(amount: 1000, currency: 'GBP', date: '2022-08-15')
13
+ # => 0.32829e4 (BigDecimal)
14
+
15
+ Lariconv.convert(amount: BigDecimal('100.68'), currency: 'EUR')
16
+ # => 0.28869e3 (BigDecimal)
17
+
18
+ Lariconv.convert(amount: 1.5, currency: 'USD', date: '2023-08-15').to_f
19
+ # => 3.93 (Float)
20
+
21
+ Lariconv.convert(amount: 100, currency: 'EUR').to_s('F')
22
+ # => "288.16" (String)
23
+
24
+ Lariconv.convert(amount: 'abc', currency: 'EUR')
25
+ # => Lariconv::InvalidAmountException
26
+
27
+ Lariconv.convert(amount: 1000, currency: 'WTF')
28
+ # => Lariconv::UnavailableCurrencyException
29
+ ```
30
+
31
+ ## Copyright
32
+
33
+ Copyright (c) 2023 Den Patin. See LICENSE for details.
data/lib/lariconv.rb CHANGED
@@ -1,18 +1,78 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'bigdecimal'
4
+ require 'bigdecimal/util'
5
+ require 'date'
3
6
  require 'json'
4
7
  require 'open-uri'
5
8
 
9
+ # Link to the National Bank of Georgia's API in English
6
10
  URL = 'https://nbg.gov.ge/gw/api/ct/monetarypolicy/currencies/en/json'
7
11
 
12
+ # List of currencies _BOTH_ available to run business in BoG and with the known exchange rate from NBG API
13
+ CURRENCIES = %w[AED AMD AUD AZN BYN CAD CHF CNY DKK EUR GBP ILS INR JPY KZT NOK QAR SEK TRY UAH USD UZS].freeze
14
+ # List of currencies with the known exchange rate from NBG API but _NOT_ available to run business in BoG
15
+ NBG_CURRENCIES = %w[BGN BRL CZK EGP HKD HUF IRR ISK KGS KRW KWD MDL NZD PLN RON RSD RUB SGD TJS TMT ZAR].freeze
16
+ # List of currencies available to run business in BoG but with _NOT_ available exchange rate from NBG API
17
+ BOG_CURRENCIES = %w[BHD IDR JOD LBP MAD MXN MYR PHP PKR RUR SAR THB VND].freeze
18
+ # TODO: Add info from TBC, Liberty, Basisbank, as well as from Wise Business and Revolut Business
19
+
8
20
  class Lariconv
9
- def self.convert(amount, currency, date)
10
- url = "#{URL}?currencies=#{currency}&date=#{date}"
11
- response = JSON.parse(URI.parse(url).read)
12
- data = response.first['currencies'].first
13
- quantity = data['quantity']
14
- rate = data['rate']
15
- result = amount.to_f / quantity * rate
21
+ class InvalidAmountException < StandardError; end
22
+
23
+ class UnavailableCurrencyException < StandardError; end
24
+
25
+ def self.convert(...)
26
+ new(...).convert
27
+ end
28
+
29
+ def initialize(amount:, currency:, date: nil)
30
+ @amount = amount
31
+ @currency = currency.upcase
32
+ @date = date
33
+ end
34
+
35
+ # @param amount [Integer, Float, BigDecimal] Amount of money to convert
36
+ # @param currency [String] Currency to convert to Lari from
37
+ # @param date [String] Date to get the exchange rate for (default: today)
38
+ # @return [BigDecimal] Converted amount of money
39
+ def convert
40
+ quantity, rate = fetch_currency_data
41
+ result = (amount / quantity) * rate
16
42
  result.round(2)
17
43
  end
44
+
45
+ def amount
46
+ raise InvalidAmountException unless [Integer, Float, BigDecimal].include? @amount.class
47
+
48
+ @amount.to_d
49
+ end
50
+
51
+ def currency
52
+ raise UnavailableCurrencyException unless CURRENCIES.include? @currency
53
+
54
+ @currency
55
+ end
56
+
57
+ def date
58
+ @date ||= parse_date
59
+ end
60
+
61
+ private
62
+
63
+ def parse_date
64
+ Date.parse @date
65
+ rescue StandardError
66
+ Date.today
67
+ end
68
+
69
+ def fetch_currency_data
70
+ response = JSON.parse URI.parse(currency_url).read
71
+ data = response.first['currencies'].first
72
+ [data['quantity'].to_d, data['rate'].to_d]
73
+ end
74
+
75
+ def currency_url
76
+ "#{URL}?currencies=#{currency}&date=#{date}"
77
+ end
18
78
  end
metadata CHANGED
@@ -1,23 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lariconv
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
  - Den Patin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.12'
13
27
  description: |2
14
- `lariconv` is a gem that converts different currencies to Georgian Lari
15
- based on the National Bank of Georgia's exchange rate for a given date.
28
+ This gem converts different currencies to Georgian Lari based on
29
+ the National Bank of Georgia's exchange rate for a given date.
30
+ Compatible with the `money` gem.
16
31
  email: den@hey.com
17
32
  executables: []
18
33
  extensions: []
19
34
  extra_rdoc_files: []
20
35
  files:
36
+ - CHANGELOG.md
37
+ - LICENSE
38
+ - README.md
21
39
  - lib/lariconv.rb
22
40
  homepage: https://github.com/denpatin/lariconv
23
41
  licenses: