xchange 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac10f4a1aa03608b79a6cd56de97a17c8fd36e5b
4
+ data.tar.gz: 2bf8acb13169eab8784163548f9b3c5908073139
5
+ SHA512:
6
+ metadata.gz: 13a22b98e9e494e462d340227706e822c61d7895bb56872a650d168dbd6575e1f6e0466fff21d9385d15e2f55913b1c26302a11d7cfac3f193ae1e680cbcb3f3
7
+ data.tar.gz: d1e8b0784a0da122e689bb1ed59b7ae126f49e5863f45ba3f49b4572952eefeca143b23a6dc96d6f960d43be0974dcdae9e9dc76565a9a9ced623f5dbbe7e080
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xchange.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 acaporrini
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.
@@ -0,0 +1,78 @@
1
+ # Xchange
2
+
3
+ A Library for dealing with currency conversion and operations between different currencies.
4
+
5
+ ## Installation
6
+
7
+ $ gem install xchange
8
+
9
+ ## Usage
10
+
11
+ Require Xchange
12
+
13
+ ```ruby
14
+ require 'xchange'
15
+ ```
16
+
17
+ Configure the currency rates with respect to a base currency
18
+
19
+ ```ruby
20
+ Xchange.conversion_rates('EUR', {'USD' => 1.11, 'Bitcoin' => 0.0047})
21
+ ```
22
+ Instantiate Xchange objects
23
+ ```ruby
24
+ fifty_eur = Xchange.new(50, 'EUR')
25
+ ```
26
+ Get amount and currency
27
+ ```ruby
28
+ fifty_eur.amount # => 50
29
+
30
+ fifty_eur.currency # => "EUR"
31
+
32
+ fifty_eur.inspect # => "50.00 EUR"
33
+ ```
34
+ Convert to a different currency
35
+ ```ruby
36
+ fifty_eur.convert_to('USD') # => 55.50 USD
37
+ ```
38
+ Perform operations in different currencies
39
+ ```ruby
40
+ twenty_dollars = Xchange.new(20, 'USD')
41
+
42
+ # Arithmetics:
43
+
44
+ fifty_eur + twenty_dollars # => 68.02 EUR
45
+
46
+ fifty_eur - twenty_dollars # => 31.98 EUR
47
+
48
+ fifty_eur / 2 # => 25 EUR
49
+
50
+ twenty_dollars * 3 # => 60 USD
51
+
52
+ # Comparisons (also in different currencies):
53
+
54
+ twenty_dollars == Xchange.new(20, 'USD') # => true
55
+
56
+ twenty_dollars == Xchange.new(30, 'USD') # => false
57
+
58
+ fifty_eur_in_usd = fifty_eur.convert_to('USD')
59
+
60
+ fifty_eur_in_usd == fifty_eur # => true
61
+
62
+ twenty_dollars > Xchange.new(5, 'USD') # => true
63
+
64
+ twenty_dollars < fifty_eur # => true
65
+ ```
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it ( https://github.com/acaporrini/xchange/fork )
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xchange"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,210 @@
1
+ require 'bigdecimal'
2
+
3
+ =begin rdoc
4
+
5
+ An instance of Xchange represents an amount of a specific currency.
6
+
7
+
8
+ =Usage
9
+
10
+ ====Install
11
+ gem install Xchange
12
+ ====Require Xchange
13
+
14
+ require 'xchange'
15
+
16
+ ==== Configure the currency rates with respect to a base currency
17
+
18
+ Xchange.conversion_rates('EUR', {'USD' => 1.11, 'Bitcoin' => 0.0047})
19
+
20
+ ====Instantiate Xchange objects
21
+
22
+ fifty_eur = Xchange.new(50, 'EUR')
23
+
24
+ ====Get amount and currency
25
+
26
+ fifty_eur.amount # => 50
27
+
28
+ fifty_eur.currency # => "EUR"
29
+
30
+ fifty_eur.inspect # => "50.00 EUR"
31
+
32
+ ====Convert to a different currency
33
+
34
+ fifty_eur.convert_to('USD') # => 55.50 USD
35
+
36
+ ====Perform operations in different currencies
37
+
38
+ twenty_dollars = Xchange.new(20, 'USD')
39
+
40
+ # Arithmetics:
41
+
42
+ fifty_eur + twenty_dollars # => 68.02 EUR
43
+
44
+ fifty_eur - twenty_dollars # => 31.98 EUR
45
+
46
+ fifty_eur / 2 # => 25 EUR
47
+
48
+ twenty_dollars * 3 # => 60 USD
49
+
50
+ # Comparisons (also in different currencies):
51
+
52
+ twenty_dollars == Xchange.new(20, 'USD') # => true
53
+
54
+ twenty_dollars == Xchange.new(30, 'USD') # => false
55
+
56
+ fifty_eur_in_usd = fifty_eur.convert_to('USD')
57
+
58
+ fifty_eur_in_usd == fifty_eur # => true
59
+
60
+ twenty_dollars > Xchange.new(5, 'USD') # => true
61
+
62
+ twenty_dollars < fifty_eur # => true
63
+
64
+
65
+ =end
66
+
67
+
68
+ class Xchange
69
+
70
+ # Include the Comparable Mixins to implement the comparison methods
71
+ include Comparable
72
+
73
+ # Accessor method
74
+ attr_reader :currency, :amount_bigdecimal
75
+
76
+ #Creates a new Xchange object of value given by amount parameter, with the currency given by the currency parameter
77
+ #
78
+ # @example
79
+ # Xchange.new(100, "USD") #=> "100.00 USD"
80
+ # Xchange.new(100, "EUR") #=> "100.00 EUR"
81
+ def initialize (amount, currency)
82
+
83
+ #Amount is stored in BigDecimal for better precision in currency operations
84
+ @amount_bigdecimal = BigDecimal.new(amount,0)
85
+
86
+ @currency = currency
87
+
88
+ end
89
+
90
+ #Class method that configure the currency rates with respect to a base currency
91
+ #
92
+ # @example
93
+ # Xchange.conversion_rates('EUR',{'USD' => 1.11, 'bitcoin' => 0.00047})
94
+ def self.conversion_rates(base_currency, rates)
95
+
96
+ @@base_currency = base_currency
97
+
98
+ @@rates = rates
99
+
100
+ end
101
+
102
+ #Convenience method for amount, it returns the value in float rounded by two decimals.
103
+ def amount
104
+
105
+ @amount_bigdecimal.to_f.round(2)
106
+
107
+ end
108
+
109
+ #Convenience method that overrides the output of inspect
110
+ def inspect
111
+
112
+ "#{'%.02f' % (@amount_bigdecimal)} #{@currency}"
113
+
114
+ end
115
+
116
+ #Converts an instance to another currency
117
+ #
118
+ # @example
119
+ # Xchange.new(50, 'EUR').convert_to('USD') #=> "55.50 USD"
120
+ def convert_to(new_currency)
121
+
122
+ # if the instance is already in the requested currency
123
+ if @currency == new_currency
124
+
125
+ #returns itself without conversions
126
+ return self
127
+
128
+ # if the current currency is different from the base currency used for conversions
129
+ # and also the new currency is different from the base one
130
+ elsif @currency != @@base_currency && new_currency != @@base_currency
131
+
132
+ # amount is divided by the rate of the current currency in order to convert it to the base currency
133
+ # then is multiplied by the rate of the new currency
134
+ amount = (@amount_bigdecimal/(BigDecimal(@@rates[@currency],0))) * (BigDecimal(@@rates[new_currency],0))
135
+
136
+ # current currency is different from the base currency but new currency equals the base currency
137
+ elsif @currency != @@base_currency && new_currency == @@base_currency
138
+
139
+ # amount is divided by the rate of the current currency in order to convert it to the base currency
140
+ amount = (@amount_bigdecimal / @@rates[@currency])
141
+
142
+ # current currency must be equal to the base currency and be different from the new currency
143
+ else
144
+
145
+ # amount is multiplied for the rate of the new currency
146
+ amount = @amount_bigdecimal * (BigDecimal.new(@@rates[new_currency],0))
147
+
148
+ end
149
+
150
+ # A new istance is returned with the new currency and the calculated amount
151
+ Xchange.new(amount,new_currency)
152
+
153
+ end
154
+
155
+ # <=> implementation allows the comparison between two Xchange instances through the Comparable mixin
156
+ def <=>(another_currency)
157
+
158
+ #converts the given instance in the actual currency and compares the amounts
159
+ @amount_bigdecimal <=> another_currency.convert_to(@currency).amount_bigdecimal
160
+
161
+ end
162
+
163
+ # Performs an addition between the current instance and another one and
164
+ # returns the result as a new Xchange instance
165
+ def +(another_currency)
166
+
167
+ #converts the given instance in the actual currency and sums the amounts
168
+ amount = @amount_bigdecimal + another_currency.convert_to(@currency).amount
169
+
170
+ #returns the result as a new instance
171
+ Xchange.new(amount,@currency)
172
+
173
+ end
174
+
175
+ # Performs a subtraction between the current instance and another one and
176
+ # returns the result as a new Xchange instance
177
+ def -(another_currency)
178
+
179
+ #converts the given instance in the actual currency and substracts the amounts
180
+ amount= @amount_bigdecimal - another_currency.convert_to(@currency).amount
181
+
182
+ #returns the result as a new instance
183
+ Xchange.new(amount,@currency)
184
+
185
+ end
186
+
187
+ # Multiplies the amount by a given number
188
+ def *(number)
189
+
190
+ #performs the multiplication in BigDecimal
191
+ amount= @amount_bigdecimal * BigDecimal(number,0)
192
+
193
+ #returns the result as a new instance
194
+ Xchange.new(amount,@currency)
195
+
196
+ end
197
+
198
+ # Divides the amount by a given number
199
+ def /(number)
200
+
201
+ #performs the division in BigDecimal
202
+ amount= @amount_bigdecimal / BigDecimal(number,0)
203
+
204
+ #returns the result as a new instance
205
+ Xchange.new(amount,@currency)
206
+
207
+ end
208
+
209
+
210
+ end
@@ -0,0 +1,3 @@
1
+ class Xchange
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xchange/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xchange"
8
+ spec.version = Xchange::VERSION
9
+ spec.authors = ["acaporrini"]
10
+ spec.email = ["a.caporrini@gmail.com"]
11
+ spec.summary = "A Library for dealing with currency conversion and operations between different currencies."
12
+ spec.homepage = "http://rubygems.org/gems/xchange"
13
+ spec.license = "MIT"
14
+
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec'
24
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xchange
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - acaporrini
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-25 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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: rspec
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
+ description:
56
+ email:
57
+ - a.caporrini@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/xchange.rb
73
+ - lib/xchange/version.rb
74
+ - tasks/rspec.rake
75
+ - xchange.gemspec
76
+ homepage: http://rubygems.org/gems/xchange
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.7
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A Library for dealing with currency conversion and operations between different
100
+ currencies.
101
+ test_files: []