money-convert 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/README.md +78 -0
- data/lib/money-convert/conversion_rates.rb +10 -0
- data/lib/money-convert/money-convert.rb +89 -0
- data/lib/money-convert/version.rb +3 -0
- data/lib/money-convert.rb +2 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c91b3c56e242f88c5617a8bb9cbac70df38e2106f2dbf88f7133bdf9c21c7f2e
|
|
4
|
+
data.tar.gz: afde2ee3bb80f2fc1ce580782416b7f0552e754b9316ab779ad3468eb0a0b0df
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4bb2ffa159e6bcc0100967a0a2f57149136ec966866d7b9cd6cd6317f15333675a9077c51e17f74554104c6b4d3f73aa4e4c396cf6059e9c0fbdefca4b74e678
|
|
7
|
+
data.tar.gz: c20cf866dd0a8816fc8b26ae80410560cff985f56e38500b9eceb38b2d4bf5ac26527ed8b22ffc594d3cf28eda062e5fce589bd915348bfcfa38888f59798849
|
data/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Money::Convert
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
Add this line to your application's Gemfile:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'money-convert'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle install
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install money-convert
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
# Configure the currency rates with respect to a base currency (here EUR):
|
|
23
|
+
|
|
24
|
+
Money.conversion_rates('EUR', {
|
|
25
|
+
'USD' => 1.11,
|
|
26
|
+
'Bitcoin' => 0.0047
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
# Instantiate money objects:
|
|
30
|
+
|
|
31
|
+
fifty_eur = Money.new(50, 'EUR')
|
|
32
|
+
|
|
33
|
+
# Get amount and currency:
|
|
34
|
+
|
|
35
|
+
fifty_eur.amount # => 50
|
|
36
|
+
fifty_eur.currency # => "EUR"
|
|
37
|
+
fifty_eur.inspect # => "50.00 EUR"
|
|
38
|
+
|
|
39
|
+
# Convert to a different currency (should return a Money
|
|
40
|
+
# instance, not a String):
|
|
41
|
+
|
|
42
|
+
fifty_eur.convert_to('USD') # => 55.50 USD
|
|
43
|
+
|
|
44
|
+
# Perform operations in different currencies:
|
|
45
|
+
|
|
46
|
+
twenty_dollars = Money.new(20, 'USD')
|
|
47
|
+
|
|
48
|
+
# Arithmetics:
|
|
49
|
+
|
|
50
|
+
fifty_eur + twenty_dollars # => 68.02 EUR
|
|
51
|
+
fifty_eur - twenty_dollars # => 31.98 EUR
|
|
52
|
+
fifty_eur / 2 # => 25 EUR
|
|
53
|
+
twenty_dollars * 3 # => 60 USD
|
|
54
|
+
|
|
55
|
+
# Comparisons (also in different currencies):
|
|
56
|
+
|
|
57
|
+
twenty_dollars == Money.new(20, 'USD') # => true
|
|
58
|
+
twenty_dollars == Money.new(30, 'USD') # => false
|
|
59
|
+
|
|
60
|
+
fifty_eur_in_usd = fifty_eur.convert_to('USD')
|
|
61
|
+
fifty_eur_in_usd == fifty_eur # => true
|
|
62
|
+
|
|
63
|
+
twenty_dollars > Money.new(5, 'USD') # => true
|
|
64
|
+
twenty_dollars < fifty_eur # => true
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Contributing
|
|
68
|
+
|
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/money-convert. 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]/money-convert/blob/master/CODE_OF_CONDUCT.md).
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
75
|
+
|
|
76
|
+
## Code of Conduct
|
|
77
|
+
|
|
78
|
+
Everyone interacting in the Money::Convert project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/money-convert/blob/master/CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require_relative "conversion_rates"
|
|
2
|
+
|
|
3
|
+
class MoneyConvert
|
|
4
|
+
attr_accessor :amount, :currency, :base_currency, :to_currencies
|
|
5
|
+
|
|
6
|
+
def initialize(amount, currency)
|
|
7
|
+
@amount = amount
|
|
8
|
+
@currency = currency
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def inspect
|
|
12
|
+
"#{@amount} #{@currency}"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def convert_to(to_currency)
|
|
16
|
+
return unless validate_currency(to_currency) && @@base_currency == currency
|
|
17
|
+
|
|
18
|
+
toal_amount = convert(to_currency)
|
|
19
|
+
self.class.new(toal_amount, to_currency)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def convert(to_currency)
|
|
23
|
+
(@amount.to_i * @@to_currencies.fetch(to_currency)).round(2)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def +(other)
|
|
27
|
+
return unless currency == @@base_currency
|
|
28
|
+
|
|
29
|
+
converted_amount = convert(other.currency)
|
|
30
|
+
converted_amount + amount.to_i
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def -(other)
|
|
34
|
+
return unless currency == @@base_currency
|
|
35
|
+
|
|
36
|
+
converted_amount = convert(other.currency)
|
|
37
|
+
converted_amount - amount.to_i
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def /(other)
|
|
41
|
+
return unless other.is_a?(Integer)
|
|
42
|
+
|
|
43
|
+
amount.to_i / other
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def *(other)
|
|
47
|
+
return unless other.is_a?(Integer)
|
|
48
|
+
|
|
49
|
+
amount.to_i * other
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def ==(other)
|
|
53
|
+
if amount == other.amount && currency == other.currency
|
|
54
|
+
true
|
|
55
|
+
else
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def >(other)
|
|
61
|
+
if amount > other.amount && currency == other.currency
|
|
62
|
+
true
|
|
63
|
+
else
|
|
64
|
+
false
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def <(other)
|
|
69
|
+
if amount < other.amount && currency == other.currency
|
|
70
|
+
true
|
|
71
|
+
elsif currency != other.currency
|
|
72
|
+
converted_amount = convert(currency)
|
|
73
|
+
|
|
74
|
+
true if amount < converted_amount
|
|
75
|
+
else
|
|
76
|
+
false
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def validate_currency(to_currency)
|
|
81
|
+
@@to_currencies&.keys&.include?(to_currency)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.conversion_rates(base_currency, to_currencies)
|
|
85
|
+
conversion_rates = MoneyConvert::ConversionRates.new(base_currency, to_currencies)
|
|
86
|
+
@@base_currency = conversion_rates.base_currency
|
|
87
|
+
@@to_currencies = conversion_rates.to_currencies
|
|
88
|
+
end
|
|
89
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: money-convert
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Srinivasa Varma
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-09-19 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- srinivasavarma.d@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/money-convert.rb
|
|
22
|
+
- lib/money-convert/conversion_rates.rb
|
|
23
|
+
- lib/money-convert/money-convert.rb
|
|
24
|
+
- lib/money-convert/version.rb
|
|
25
|
+
homepage: https://github.com/varmad/MoneyConvert
|
|
26
|
+
licenses:
|
|
27
|
+
- MIT
|
|
28
|
+
metadata: {}
|
|
29
|
+
post_install_message:
|
|
30
|
+
rdoc_options: []
|
|
31
|
+
require_paths:
|
|
32
|
+
- lib
|
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 2.3.0
|
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '0'
|
|
43
|
+
requirements: []
|
|
44
|
+
rubygems_version: 3.1.2
|
|
45
|
+
signing_key:
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Convert one currency to another currency.
|
|
48
|
+
test_files: []
|