konvert 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 +10 -0
- data/.idea/vcs.xml +6 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/konvert.gemspec +26 -0
- data/lib/konvert/version.rb +3 -0
- data/lib/konvert.rb +6 -0
- data/lib/money.rb +67 -0
- data/lib/operations.rb +50 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99e29e8e28ca257057af2d543483a9ac42941a32
|
4
|
+
data.tar.gz: a372b06dfed1b8991012e5663153e4818e829942
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ada89fbe2a5226f98dd4910f994ac347c023d1d1748c88aa1b8408d6cbc364cbae6381c3a6915f90f54a784f7e82d7c7f9db4905e70f04851a53c75317434fb2
|
7
|
+
data.tar.gz: 21e21f0b67dfff5e6a0d566d1bee29a7f4c7fd12056115d843d7be1e6e6b544bd59d2cb41aa5df16f0e517df0bf17b1df0e5e4b6dec0847d269617ea45bb7805
|
data/.gitignore
ADDED
data/.idea/vcs.xml
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Nauman Tahir
|
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,55 @@
|
|
1
|
+
# Konvert
|
2
|
+
|
3
|
+
Konvert is a Ruby command line gem that lets you convert between different currencies and perform arithmetic operations on them from the command line. Just set the currency rates and have fun..
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
install it as:
|
8
|
+
|
9
|
+
$ gem install konvert
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
1- After installing the gem, enter irb console and require konvert:
|
14
|
+
|
15
|
+
irb(main): require 'konvert'
|
16
|
+
|
17
|
+
2- Configure the currency rates with respect to a base currency (here EUR):
|
18
|
+
|
19
|
+
irb(main): Money.conversion_rates('EUR', {
|
20
|
+
'USD' => 1.11,
|
21
|
+
'Bitcoin' => 0.0047
|
22
|
+
})
|
23
|
+
|
24
|
+
3- You can perform the operations now. Examples:
|
25
|
+
1) Create a Money object
|
26
|
+
|
27
|
+
irb(main): five_euro = Konvert::Money.new(5,'EUR)
|
28
|
+
2) Convert to different currency
|
29
|
+
|
30
|
+
irb(main): five_euro.convert_to('USD')
|
31
|
+
3) Add two Money objects. They can be of same or different currencies
|
32
|
+
|
33
|
+
irb(main): fifty_eur + twenty_dollars # => 68.02 EUR
|
34
|
+
irb(main): fifty_eur - twenty_dollars # => 31.98 EUR
|
35
|
+
|
36
|
+
|
37
|
+
4) Divide or multiply a Money object with an integer
|
38
|
+
|
39
|
+
irb(main): fifty_eur / 2 # => 25 EUR
|
40
|
+
irb(main): twenty_dollars * 3 # => 60 USD
|
41
|
+
|
42
|
+
|
43
|
+
5) Perform Comparisons (same currencies or in different currencies):
|
44
|
+
|
45
|
+
irb(main): twenty_dollars == Money.new(20, 'USD') # => true
|
46
|
+
irb(main): twenty_dollars == Money.new(30, 'USD') # => false
|
47
|
+
irb(main): twenty_dollars > Money.new(5, 'USD') # => true
|
48
|
+
irb(main): twenty_dollars < fifty_eur # => true
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
55
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "konvert"
|
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
|
data/bin/setup
ADDED
data/konvert.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'konvert/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "konvert"
|
8
|
+
spec.version = Konvert::VERSION
|
9
|
+
spec.authors = ["Nauman Tahir"]
|
10
|
+
spec.email = ["nomibzu@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Currency converter gem}
|
13
|
+
spec.description = %q{Konvert is a Ruby command line gem that lets you convert between different currencies and perform arithmetic operations on them from the command line. Just set the currency rates and have fun..}
|
14
|
+
spec.homepage = "https://github.com/Nauman-nxvt/konvert"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/lib/konvert.rb
ADDED
data/lib/money.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
module Konvert
|
2
|
+
require 'pry'
|
3
|
+
require_relative '../lib/operations'
|
4
|
+
class Money
|
5
|
+
include Konvert::Operations
|
6
|
+
|
7
|
+
attr_accessor :amount
|
8
|
+
attr_accessor :currency
|
9
|
+
|
10
|
+
def self.conversion_rates(base,currency_hash)
|
11
|
+
@@base_currency = base
|
12
|
+
@@currency_hash = currency_hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.base_currency
|
16
|
+
@@base_currency
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.currency_hash
|
20
|
+
@@currency_hash
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(amount, currency)
|
24
|
+
@amount = amount
|
25
|
+
@currency = currency
|
26
|
+
end
|
27
|
+
|
28
|
+
def inspect
|
29
|
+
p sprintf('%.2f', amount) + ' ' + currency
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_to_base_amount
|
33
|
+
return unless (conversion_rates_set? && currency_defined?)
|
34
|
+
self.currency == Money.base_currency ? self.amount : self.amount / Money.currency_hash[self.currency]
|
35
|
+
end
|
36
|
+
|
37
|
+
def convert_to(to_currency)
|
38
|
+
return unless (conversion_rates_set? && currency_defined?)
|
39
|
+
if to_currency == Money.base_currency
|
40
|
+
amount = self.convert_to_base_amount
|
41
|
+
else
|
42
|
+
amount_in_base = self.convert_to_base_amount
|
43
|
+
amount = amount_in_base * Money.currency_hash[to_currency]
|
44
|
+
end
|
45
|
+
Money.new(amount, to_currency)
|
46
|
+
end
|
47
|
+
|
48
|
+
def conversion_rates_set?
|
49
|
+
if (defined?(@@base_currency)).nil? || (defined?(@@currency_hash)).nil?
|
50
|
+
p "Conversion rates not set: Please set conversion rates first."
|
51
|
+
exit(false)
|
52
|
+
#abort 'Conversion rates not set: Please set conversion rates first.'
|
53
|
+
else
|
54
|
+
true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
def currency_defined?
|
58
|
+
unless (@@currency_hash.key?(self.currency) || self.currency == @@base_currency)
|
59
|
+
p "Invalid currency: Rates for #{self.currency} not defined in currency hash"
|
60
|
+
exit(false)
|
61
|
+
#abort "Invalid currency: Rates for #{self.currency} not defined in currency hash"
|
62
|
+
else
|
63
|
+
true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/operations.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Konvert
|
2
|
+
module Operations
|
3
|
+
|
4
|
+
def perform_sum(operand1,operand2,operation)
|
5
|
+
amount1 = operand1.convert_to_base_amount
|
6
|
+
amount2 = operand2.convert_to_base_amount
|
7
|
+
sum_in_base_currency = send(operation, amount1, amount2)
|
8
|
+
result = Money.new(sum_in_base_currency, Money.base_currency)
|
9
|
+
operand1.currency == Money.base_currency ? result : result.convert_to(operand1.currency)
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(val1,val2)
|
13
|
+
val1 + val2
|
14
|
+
end
|
15
|
+
|
16
|
+
def subtract(val1,val2)
|
17
|
+
val1 - val2
|
18
|
+
end
|
19
|
+
|
20
|
+
def +(operand)
|
21
|
+
perform_sum(self,operand,'add')
|
22
|
+
end
|
23
|
+
|
24
|
+
def -(operand)
|
25
|
+
perform_sum(self,operand,'subtract')
|
26
|
+
end
|
27
|
+
|
28
|
+
def *(operand)
|
29
|
+
Money.new(self.amount * operand, self.currency)
|
30
|
+
end
|
31
|
+
|
32
|
+
def /(operand)
|
33
|
+
Money.new(self.amount / operand, self.currency)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Comparisons
|
37
|
+
|
38
|
+
def ==(operand)
|
39
|
+
return self.convert_to_base_amount == operand.convert_to_base_amount
|
40
|
+
end
|
41
|
+
|
42
|
+
def >(operand)
|
43
|
+
return self.convert_to_base_amount > operand.convert_to_base_amount
|
44
|
+
end
|
45
|
+
|
46
|
+
def <(operand)
|
47
|
+
return self.convert_to_base_amount < operand.convert_to_base_amount
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: konvert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nauman Tahir
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-20 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: Konvert is a Ruby command line gem that lets you convert between different
|
70
|
+
currencies and perform arithmetic operations on them from the command line. Just
|
71
|
+
set the currency rates and have fun..
|
72
|
+
email:
|
73
|
+
- nomibzu@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".idea/vcs.xml"
|
80
|
+
- ".rspec"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- konvert.gemspec
|
88
|
+
- lib/konvert.rb
|
89
|
+
- lib/konvert/version.rb
|
90
|
+
- lib/money.rb
|
91
|
+
- lib/operations.rb
|
92
|
+
homepage: https://github.com/Nauman-nxvt/konvert
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Currency converter gem
|
116
|
+
test_files: []
|