currency_converter_3000 1.0.1
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 +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/currency_converter_3000.gemspec +35 -0
- data/lib/currency_converter_3000.rb +93 -0
- data/lib/money.rb +53 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1bc9430294c40f5460521b227f098ad79c707e0
|
4
|
+
data.tar.gz: 97671239d594a97e18d61df561fa70e20301b37b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3bd433b68ce2235a4f9a90efd6660e83281c738096c207afbe844c2847109efd832a4c381048f4883e58aad0460ccb58ad25212c413fb477fc485f8dc314c65
|
7
|
+
data.tar.gz: b2f3c9ef34e7e68e7a18ce38b8dd06fb6c1fd4a9a6fa913fe21b18936c42b1bb62fe985ec770d3684b81aea47e6475ff987848252af482f250bc567b3cb66d5d
|
data/.gitignore
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) 2017 Lescenco Andrei
|
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,95 @@
|
|
1
|
+
# CurrencyConverter3000
|
2
|
+
|
3
|
+
This is a small interview project for converting currencies
|
4
|
+
|
5
|
+
Disclaimer : DO NOT USE IN A PRODUCTION ENVIRONMENT
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'currency_converter_3000'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install currency_converter_3000
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
#### Configure the currency rates with respect to a base currency (here EUR):
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
CurrencyConverter3000::Money.conversion_rates('EUR', {
|
28
|
+
'USD' => 1.11,
|
29
|
+
'Bitcoin' => 0.0047
|
30
|
+
})
|
31
|
+
```
|
32
|
+
|
33
|
+
#### Instantiate money objects
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
fifty_eur = CurrencyConverter3000::Money.new(50, 'EUR')
|
37
|
+
```
|
38
|
+
|
39
|
+
#### Get amount and currency:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
fifty_eur.amount # => 50
|
43
|
+
fifty_eur.currency # => "EUR"
|
44
|
+
fifty_eur.inspect # => "50.00 EUR"
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Convert to a different currency (returns a Money instance):
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
fifty_eur.convert_to('USD') # => 55.50 USD
|
51
|
+
```
|
52
|
+
|
53
|
+
#### Perform operations in different currencies:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
twenty_dollars = CurrencyConverter3000::Money.new(20, 'USD')
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Arithmetics:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
fifty_eur + twenty_dollars # => 68.02 EUR
|
63
|
+
fifty_eur - twenty_dollars # => 31.98 EUR
|
64
|
+
fifty_eur / 2 # => 25 EUR
|
65
|
+
twenty_dollars * 3 # => 60 USD
|
66
|
+
```
|
67
|
+
|
68
|
+
#### Comparisons (also in different currencies):
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
twenty_dollars == CurrencyConverter3000::Money.new(20, 'USD') # => true
|
72
|
+
twenty_dollars == CurrencyConverter3000::Money.new(30, 'USD') # => false
|
73
|
+
|
74
|
+
fifty_eur_in_usd = fifty_eur.convert_to('USD')
|
75
|
+
fifty_eur_in_usd == fifty_eur # => true
|
76
|
+
|
77
|
+
twenty_dollars > CurrencyConverter3000::Money.new(5, 'USD') # => true
|
78
|
+
twenty_dollars < fifty_eur # => true
|
79
|
+
```
|
80
|
+
|
81
|
+
## Development
|
82
|
+
|
83
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
84
|
+
|
85
|
+
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).
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lesce/currency_converter_3000.
|
90
|
+
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
95
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "currency_converter_3000"
|
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,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "currency_converter_3000"
|
7
|
+
spec.version = "1.0.1"
|
8
|
+
spec.authors = ["Lescenco Andrei"]
|
9
|
+
spec.email = ["lescee@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Helper for currency conversion.}
|
12
|
+
spec.homepage = "https://github.com/lesce/currency_converter_3000"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
19
|
+
else
|
20
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
21
|
+
"public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
34
|
+
spec.add_development_dependency "pry"
|
35
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module CurrencyConverter3000
|
2
|
+
class Money
|
3
|
+
attr_reader :currency
|
4
|
+
|
5
|
+
class << self
|
6
|
+
# Setup the Default Currency and the coresponding conversion rates
|
7
|
+
def conversion_rates default_currency, conversion_hash
|
8
|
+
@@default_currency = default_currency
|
9
|
+
# Hash of conversion rates from Default Currency
|
10
|
+
# Each key should represent a currency
|
11
|
+
# Each value should represent the rate of conversion
|
12
|
+
# Example :
|
13
|
+
# {
|
14
|
+
# 'USD' => 1.10,
|
15
|
+
# 'BTC' => 0.00091
|
16
|
+
# }
|
17
|
+
@@conversion_hash = conversion_hash
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize amount, currency
|
22
|
+
@amount = amount
|
23
|
+
@currency = currency
|
24
|
+
end
|
25
|
+
|
26
|
+
# These are the 4 Covered Conversion cases:
|
27
|
+
# Currency -> Default Currency
|
28
|
+
# Same Currency -> Same Currency
|
29
|
+
# Currency -> New Currency
|
30
|
+
# Default Currency -> Currency
|
31
|
+
def convert_to new_currency
|
32
|
+
if new_currency == @@default_currency && new_currency != @currency
|
33
|
+
# convert from non default currency to default currency
|
34
|
+
Money.new @amount / @@conversion_hash[@currency], new_currency
|
35
|
+
elsif new_currency == @currency
|
36
|
+
# convert from non default currency to same non default currency
|
37
|
+
self
|
38
|
+
elsif new_currency != @@default_currency && @currency != @@default_currency
|
39
|
+
# convert from non default currency to other non default currency
|
40
|
+
# Currency -> Default Currency
|
41
|
+
# Default Currency -> New Currency
|
42
|
+
convert_to(@@default_currency).convert_to(new_currency)
|
43
|
+
elsif @@conversion_hash[new_currency]
|
44
|
+
# convert from default currency to non default currency
|
45
|
+
Money.new @amount * @@conversion_hash[new_currency], new_currency
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def amount
|
50
|
+
@amount.to_f.round(2)
|
51
|
+
end
|
52
|
+
|
53
|
+
def + money_object
|
54
|
+
Money.new apply_operator(:+, money_object), @currency
|
55
|
+
end
|
56
|
+
|
57
|
+
def - money_object
|
58
|
+
Money.new apply_operator(:-, money_object), @currency
|
59
|
+
end
|
60
|
+
|
61
|
+
def * number
|
62
|
+
Money.new (amount * number).round(2), @currency
|
63
|
+
end
|
64
|
+
|
65
|
+
def / number
|
66
|
+
Money.new (amount / number).round(2), @currency
|
67
|
+
end
|
68
|
+
|
69
|
+
def == money_object
|
70
|
+
apply_operator(:==, money_object)
|
71
|
+
end
|
72
|
+
|
73
|
+
def > money_object
|
74
|
+
apply_operator(:>, money_object)
|
75
|
+
end
|
76
|
+
|
77
|
+
def < money_object
|
78
|
+
apply_operator(:<, money_object)
|
79
|
+
end
|
80
|
+
|
81
|
+
def apply_operator operator, money_object
|
82
|
+
if @currency == money_object.currency
|
83
|
+
amount.send(operator, money_object.amount)
|
84
|
+
else
|
85
|
+
amount.send(operator, money_object.convert_to(@currency).amount)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def inspect
|
90
|
+
"#{sprintf('%.2f', @amount)} #@currency"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/money.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module CurrencyConverter3000
|
2
|
+
class Money
|
3
|
+
attr_reader :amount, :currency
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def conversion_rates default_currency, conversion_hash
|
7
|
+
@@default_currency = default_currency
|
8
|
+
@@conversion_hash = conversion_hash
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize amount, currency
|
13
|
+
@amount = amount
|
14
|
+
@currency = currency
|
15
|
+
end
|
16
|
+
|
17
|
+
def convert_to currency
|
18
|
+
if @@conversion_hash[currency]
|
19
|
+
Money.new @amount * @@conversion_hash[currency], currency
|
20
|
+
else
|
21
|
+
'No conversion rate found.'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def + money_object
|
26
|
+
@amount + @@conversion_hash[money_object.currency]*money_object.amount
|
27
|
+
end
|
28
|
+
|
29
|
+
def - money_object
|
30
|
+
@amount - @@conversion_hash[money_object.currency]*money_object.amount
|
31
|
+
end
|
32
|
+
|
33
|
+
def * number
|
34
|
+
@amount * number
|
35
|
+
end
|
36
|
+
|
37
|
+
def / number
|
38
|
+
@amount / number
|
39
|
+
end
|
40
|
+
|
41
|
+
def == money_object
|
42
|
+
if @currency == money_object.currency
|
43
|
+
@amount.round(2) == money_object.amount.round(2)
|
44
|
+
else
|
45
|
+
@amount.round(2) == (@@conversion_hash[money_object.currency] * money_object.amount).round(2)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def inspect
|
50
|
+
"#{sprintf('%.2f', @amount)} #@currency"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: currency_converter_3000
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lescenco Andrei
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- lescee@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- currency_converter_3000.gemspec
|
85
|
+
- lib/currency_converter_3000.rb
|
86
|
+
- lib/money.rb
|
87
|
+
homepage: https://github.com/lesce/currency_converter_3000
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
allowed_push_host: https://rubygems.org
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Helper for currency conversion.
|
112
|
+
test_files: []
|