hundred 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ebe8c595d6fc1d2c16124a045567cbd30f90982
4
+ data.tar.gz: 3d11e386e664f17d9cfbc9ffdf133b8598af3d97
5
+ SHA512:
6
+ metadata.gz: d8c35e1e44b35fba33b5d5d4075121c9538ea0bdcd89e56d1ad069499c7f7d07b48464f73556c82a574ea79b32b4c39fc8ccfccb7e7c09939245bd094f782681
7
+ data.tar.gz: af27a8f33899652684073d73e91201402ced4e178eecd72a992aa921484c29495e9e1633f9016db8700b31adcc0505c73bbc3b47bed7104acd8ffbbb742ae272
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
5
+
6
+ notifications:
7
+ email:
8
+ on_failure: change
9
+ on_success: change
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hundred.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Vitaly Pushkar
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,82 @@
1
+ # Hundred
2
+ [![Coverage Status](https://coveralls.io/repos/vitaly-pushkar/hundred/badge.svg?branch=master&service=github)](https://coveralls.io/github/vitaly-pushkar/hundred?branch=master)
3
+ [![Build Status](https://travis-ci.org/vitaly-pushkar/hundred.svg?branch=master)](https://travis-ci.org/vitaly-pushkar/hundred)
4
+
5
+ Hundred is a simple Ruby gem to perform currency conversion and arithmetics with different currencies.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hundred'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hundred
22
+
23
+ ## Usage
24
+
25
+ **Configure the currency rates with respect to a base currency (here EUR):**
26
+
27
+ ```ruby
28
+ Hundred.conversion_rates('EUR', {
29
+ 'USD' => 1.11,
30
+ 'Bitcoin' => 0.0047
31
+ })
32
+ ```
33
+
34
+ **Instantiate money objects:**
35
+
36
+ fifty_eur = Hundred.new(50, 'EUR')
37
+
38
+ **Get amount and currency:**
39
+
40
+ ```ruby
41
+ fifty_eur.amount # => 50
42
+ fifty_eur.currency # => "EUR"
43
+ fifty_eur.inspect # => "50.00 EUR"
44
+ ```
45
+
46
+ **Convert to a different currency:**
47
+
48
+ fifty_eur.convert_to('USD') # => 55.50 USD
49
+
50
+ **Perform operations in different currencies:**
51
+
52
+ twenty_dollars = Hundred.new(20, 'USD')
53
+
54
+ **Arithmetics:**
55
+
56
+ ```ruby
57
+ fifty_eur + twenty_dollars # => 68.02 EUR
58
+ fifty_eur - twenty_dollars # => 31.98 EUR
59
+ fifty_eur / 2 # => 25 EUR
60
+ twenty_dollars * 3 # => 60 USD
61
+ ```
62
+
63
+ **Comparisons:**
64
+
65
+ ```ruby
66
+ twenty_dollars == Hundred.new(20, 'USD') # => true
67
+ twenty_dollars == Hundred.new(30, 'USD') # => false
68
+
69
+ fifty_eur_in_usd = fifty_eur.convert_to('USD')
70
+ fifty_eur_in_usd == fifty_eur # => true
71
+
72
+ twenty_dollars > Hundred.new(5, 'USD') # => true
73
+ twenty_dollars < fifty_eur # => true
74
+ ```
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vitaly-pushkar/hundred.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
82
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hundred"
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
@@ -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
data/hundred.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hundred/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hundred"
8
+ spec.version = Hundred::VERSION
9
+ spec.authors = ["Vitaly Pushkar"]
10
+ spec.email = ["vitaly.pushkar@gmail.com"]
11
+
12
+ spec.summary = %q{Simple currency conversion and arithmetics Ruby gem}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/vitaly-pushkar/hundred"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "byebug"
26
+ spec.add_development_dependency "coveralls"
27
+ end
@@ -0,0 +1,3 @@
1
+ class Hundred
2
+ VERSION = "0.1.1"
3
+ end
data/lib/hundred.rb ADDED
@@ -0,0 +1,103 @@
1
+ require 'hundred/version'
2
+ require 'byebug'
3
+
4
+ class Hundred
5
+ MoneyError = Class.new(StandardError)
6
+
7
+ DEFAULT_CURRENCY = 'EUR'
8
+ DEFAULT_PRECISION = 2
9
+
10
+ @@rates = {}
11
+ @@rates.default_proc = proc do |hash, key|
12
+ if hash[DEFAULT_CURRENCY].key?(key)
13
+ hash[key] = calculate_rates_for(key, hash[DEFAULT_CURRENCY])
14
+ else
15
+ fail MoneyError, "#{key} doesn't exist"
16
+ end
17
+ end
18
+
19
+ attr_accessor :amount, :currency
20
+
21
+ def self.conversion_rates(currency, rates)
22
+ @@rates.merge!(currency => rates)
23
+ end
24
+
25
+ def initialize(amount, currency = DEFAULT_CURRENCY)
26
+ @amount = amount
27
+ @currency = currency
28
+ end
29
+
30
+ def inspect
31
+ "#{'%.2f' % amount} #{currency}"
32
+ end
33
+
34
+ def amount
35
+ @amount % 2 == 0 ? @amount.to_int : @amount
36
+ end
37
+
38
+ def convert_to(curr)
39
+ return self if currency == curr
40
+
41
+ self.class.new(amount * @@rates[currency][curr], curr)
42
+ end
43
+
44
+ def ==(other)
45
+ with_precision(convert_to(DEFAULT_CURRENCY).amount) ==
46
+ with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
47
+ end
48
+
49
+ def +(other)
50
+ new_amount = with_precision(convert_to(DEFAULT_CURRENCY).amount) +
51
+ with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
52
+
53
+ self.class.new(new_amount)
54
+ end
55
+
56
+ def -(other)
57
+ new_amount = with_precision(convert_to(DEFAULT_CURRENCY).amount) -
58
+ with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
59
+
60
+ self.class.new(new_amount)
61
+ end
62
+
63
+ def *(other)
64
+ new_amount = amount * other
65
+
66
+ self.class.new(new_amount, currency)
67
+ end
68
+
69
+ def /(other)
70
+ new_amount = amount / other
71
+
72
+ self.class.new(new_amount, currency)
73
+ end
74
+
75
+ def >(other)
76
+ with_precision(convert_to(DEFAULT_CURRENCY).amount) >
77
+ with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
78
+ end
79
+
80
+ def <(other)
81
+ with_precision(convert_to(DEFAULT_CURRENCY).amount) <
82
+ with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
83
+ end
84
+
85
+ protected
86
+
87
+ def self.calculate_rates_for(currency, default_rates)
88
+ result = default_rates.each_with_object({}) do |(curr, rate), calculated_rates|
89
+ next if curr == currency
90
+
91
+ new_rate = rate / default_rates[currency]
92
+ calculated_rates[curr] = new_rate
93
+ end
94
+
95
+ default_currency_rate = 1 / default_rates[currency]
96
+ result[DEFAULT_CURRENCY] = default_currency_rate
97
+ result
98
+ end
99
+
100
+ def with_precision(amount, precision = DEFAULT_PRECISION)
101
+ amount.round(precision)
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hundred
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Vitaly Pushkar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-08 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '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: byebug
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
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - vitaly.pushkar@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".coveralls.yml"
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - hundred.gemspec
100
+ - lib/hundred.rb
101
+ - lib/hundred/version.rb
102
+ homepage: https://github.com/vitaly-pushkar/hundred
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.5
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Simple currency conversion and arithmetics Ruby gem
126
+ test_files: []