crypto-unit 0.3.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/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +20 -0
- data/README.md +69 -0
- data/Rakefile +26 -0
- data/VERSION +1 -0
- data/crypto-unit.gemspec +60 -0
- data/lib/crypto-unit.rb +7 -0
- data/lib/crypto_unit_base.rb +164 -0
- data/lib/litoshi.rb +4 -0
- data/lib/satoshi.rb +4 -0
- data/spec/crypto_unit_base_spec.rb +96 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2de75699f62f03becfbc70ccee8a49f134a5c0a1
|
4
|
+
data.tar.gz: 0450a1a6a2d3ffad8feec23b219122e74ed1eee1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfcd8106763223c3135301730eeb04d0eafa28bee87c91fd18eba21ff96df3d2054c2d5d197c63a6c31b6a144b62121bfec5807e41f58668cedf2c41a2c176dd
|
7
|
+
data.tar.gz: a88588c40850735ba067c2c26198e02323b93bc0f9e0c93bc2589ea15e80ad5ae01fcb09a11e35e53a5d56320afec4f43af981eb886095c1b6dd51fcbe9d94f2
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.5.0)
|
5
|
+
public_suffix (~> 2.0, >= 2.0.2)
|
6
|
+
builder (3.2.2)
|
7
|
+
descendants_tracker (0.0.4)
|
8
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
faraday (0.9.2)
|
11
|
+
multipart-post (>= 1.2, < 3)
|
12
|
+
git (1.3.0)
|
13
|
+
github_api (0.11.3)
|
14
|
+
addressable (~> 2.3)
|
15
|
+
descendants_tracker (~> 0.0.1)
|
16
|
+
faraday (~> 0.8, < 0.10)
|
17
|
+
hashie (>= 1.2)
|
18
|
+
multi_json (>= 1.7.5, < 2.0)
|
19
|
+
nokogiri (~> 1.6.0)
|
20
|
+
oauth2
|
21
|
+
hashie (3.4.6)
|
22
|
+
highline (1.7.8)
|
23
|
+
jeweler (2.1.2)
|
24
|
+
builder
|
25
|
+
bundler (>= 1.0)
|
26
|
+
git (>= 1.2.5)
|
27
|
+
github_api (~> 0.11.0)
|
28
|
+
highline (>= 1.6.15)
|
29
|
+
nokogiri (>= 1.5.10)
|
30
|
+
rake
|
31
|
+
rdoc
|
32
|
+
semver
|
33
|
+
jwt (1.5.6)
|
34
|
+
mini_portile2 (2.1.0)
|
35
|
+
multi_json (1.12.1)
|
36
|
+
multi_xml (0.5.5)
|
37
|
+
multipart-post (2.0.0)
|
38
|
+
nokogiri (1.6.8.1)
|
39
|
+
mini_portile2 (~> 2.1.0)
|
40
|
+
oauth2 (1.2.0)
|
41
|
+
faraday (>= 0.8, < 0.10)
|
42
|
+
jwt (~> 1.0)
|
43
|
+
multi_json (~> 1.3)
|
44
|
+
multi_xml (~> 0.5)
|
45
|
+
rack (>= 1.2, < 3)
|
46
|
+
public_suffix (2.0.3)
|
47
|
+
rack (2.0.1)
|
48
|
+
rake (11.3.0)
|
49
|
+
rdoc (5.0.0)
|
50
|
+
rspec (3.5.0)
|
51
|
+
rspec-core (~> 3.5.0)
|
52
|
+
rspec-expectations (~> 3.5.0)
|
53
|
+
rspec-mocks (~> 3.5.0)
|
54
|
+
rspec-core (3.5.4)
|
55
|
+
rspec-support (~> 3.5.0)
|
56
|
+
rspec-expectations (3.5.0)
|
57
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
58
|
+
rspec-support (~> 3.5.0)
|
59
|
+
rspec-mocks (3.5.0)
|
60
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
61
|
+
rspec-support (~> 3.5.0)
|
62
|
+
rspec-support (3.5.0)
|
63
|
+
semver (1.0.1)
|
64
|
+
thread_safe (0.3.5)
|
65
|
+
|
66
|
+
PLATFORMS
|
67
|
+
ruby
|
68
|
+
|
69
|
+
DEPENDENCIES
|
70
|
+
bundler (~> 1.0)
|
71
|
+
jeweler (~> 2.1.2)
|
72
|
+
rspec
|
73
|
+
|
74
|
+
BUNDLED WITH
|
75
|
+
1.12.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Roman Snitko
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
Crypto Unit
|
2
|
+
================
|
3
|
+
This tiny gem allows you to make easy conversions from one Bitcoin or Litecoin denomination into another.
|
4
|
+
It provides a class, which objects would essentially represent an amount of litecoins measured
|
5
|
+
in the smallest possible denomination, which is Satoshi for BTC and Litoshi for LTC. You can then call methods on these objects
|
6
|
+
to convert it various other denominations.
|
7
|
+
|
8
|
+
### Installation
|
9
|
+
|
10
|
+
gem install crypto-unit
|
11
|
+
|
12
|
+
### Usage
|
13
|
+
|
14
|
+
Here's how it might look:
|
15
|
+
|
16
|
+
s = Satoshi.new(1) # By default, it accepts amounts in BTC denomination
|
17
|
+
s.to_i # => 100000000 (in Satoshis)
|
18
|
+
s.to_milli # => 1000.0
|
19
|
+
s.to_standart # => 1.0
|
20
|
+
|
21
|
+
When creating a Satoshi / Litoshi object, you can also specify which unit is being used to pass the amount,
|
22
|
+
for example:
|
23
|
+
|
24
|
+
s = Litoshi.new(1, from_unit: :milli)
|
25
|
+
|
26
|
+
s.to_i # => 100000
|
27
|
+
s.to_milli # => 1.0
|
28
|
+
s.to_standart # => 0.001
|
29
|
+
|
30
|
+
There's also a special method which is called `#to_unit`, it always converts to the denomination
|
31
|
+
specified in the `:to_unit` option in the constructor. It becomes really handy when you want to
|
32
|
+
specify your preferred denomination used througout the program in just one place
|
33
|
+
(to be able to change it later):
|
34
|
+
|
35
|
+
DENOMINATION = :milli
|
36
|
+
s = Litoshi.new(1, from_unit: DENOMINATION, to_unit: DENOMINATION)
|
37
|
+
s.to_unit # => 1.0 (in mLTC)
|
38
|
+
|
39
|
+
This can be shortened to just the `:unit` option:
|
40
|
+
|
41
|
+
s = Litoshi.new(1, unit: DENOMINATION)
|
42
|
+
|
43
|
+
But, of course, if you want your "from" denomination and "to" denomination to be different, then
|
44
|
+
you'd have to pass them both manually:
|
45
|
+
|
46
|
+
s = Satoshi.new(1, from_unit: :milli, to_unit: :standart)
|
47
|
+
s.to_unit # => 0.001
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
###Comparing units
|
52
|
+
|
53
|
+
CryptoUnit objects come with methods and coercions for comparing itself with both other CyptoUnit objects and also numeric values:
|
54
|
+
|
55
|
+
s1 = Litoshi.new(1)
|
56
|
+
s2 = Litoshi.new(2)
|
57
|
+
|
58
|
+
s1 > s2 # => false
|
59
|
+
s1 < s2 # => true
|
60
|
+
s1 == s2 # => false
|
61
|
+
|
62
|
+
s1 > 1 # => true
|
63
|
+
1 > s2 # => false
|
64
|
+
|
65
|
+
|
66
|
+
### Unit tests
|
67
|
+
|
68
|
+
|
69
|
+
Run `rspec spec`. Pull requests with more unit tests are welcome.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "crypto-unit"
|
18
|
+
gem.homepage = "http://github.com/mgpnd/crypto-unit"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Converts various BTC and LTC denominations}
|
21
|
+
gem.description = %Q{Converts various BTC and LTC denominations}
|
22
|
+
gem.email = "roman.snitko@gmail.com"
|
23
|
+
gem.authors = ["Roman Snitko"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
data/crypto-unit.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: crypto-unit 0.3.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "crypto-unit".freeze
|
9
|
+
s.version = "0.3.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Roman Snitko".freeze]
|
14
|
+
s.date = "2017-07-18"
|
15
|
+
s.description = "Converts various BTC and LTC denominations".freeze
|
16
|
+
s.email = "roman.snitko@gmail.com".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.md",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"crypto-unit.gemspec",
|
31
|
+
"lib/crypto-unit.rb",
|
32
|
+
"lib/crypto_unit_base.rb",
|
33
|
+
"lib/litoshi.rb",
|
34
|
+
"lib/satoshi.rb",
|
35
|
+
"spec/crypto_unit_base_spec.rb"
|
36
|
+
]
|
37
|
+
s.homepage = "http://github.com/mgpnd/crypto-unit".freeze
|
38
|
+
s.licenses = ["MIT".freeze]
|
39
|
+
s.rubygems_version = "2.6.12".freeze
|
40
|
+
s.summary = "Converts various BTC and LTC denominations".freeze
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
s.specification_version = 4
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
47
|
+
s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.1.2"])
|
48
|
+
s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
51
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.1.2"])
|
52
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
56
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.1.2"])
|
57
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/lib/crypto-unit.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
class CryptoUnit
|
2
|
+
# Says how many digits after the decimal point
|
3
|
+
# a denomination has.
|
4
|
+
UNIT_DENOMINATIONS = {
|
5
|
+
standart: 8,
|
6
|
+
milli: 5,
|
7
|
+
primary: 0,
|
8
|
+
}
|
9
|
+
|
10
|
+
MAX_VALUE = 0
|
11
|
+
NAME = ''
|
12
|
+
|
13
|
+
class TooManyDigitsAfterDecimalPoint < Exception;end
|
14
|
+
class TooLarge < Exception;end
|
15
|
+
|
16
|
+
attr_reader :value, :from_unit, :to_unit
|
17
|
+
|
18
|
+
def initialize(n=nil, from_unit: 'standart', to_unit: 'standart', unit: nil)
|
19
|
+
n = 0 if n.nil?
|
20
|
+
if unit
|
21
|
+
@from_unit = @to_unit = unit.downcase.to_sym
|
22
|
+
else
|
23
|
+
@from_unit = from_unit.downcase.to_sym
|
24
|
+
@to_unit = to_unit.downcase.to_sym
|
25
|
+
end
|
26
|
+
@value = convert_to_primary(n) if n
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_standart(as: :number)
|
30
|
+
to_denomination(UNIT_DENOMINATIONS[:standart], as: as)
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_milli(as: :number)
|
34
|
+
to_denomination(UNIT_DENOMINATIONS[:milli], as: as)
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_unit(as: :number)
|
38
|
+
to_denomination(UNIT_DENOMINATIONS[@to_unit], as: as)
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_i
|
42
|
+
return 0 if @value.nil?
|
43
|
+
@value
|
44
|
+
end
|
45
|
+
alias :primary_value :to_i
|
46
|
+
|
47
|
+
def to_s
|
48
|
+
to_unit.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
def value=(n)
|
52
|
+
n = 0 if n.nil?
|
53
|
+
@value = convert_to_primary(n)
|
54
|
+
end
|
55
|
+
|
56
|
+
def primary_value=(v)
|
57
|
+
@value = v
|
58
|
+
end
|
59
|
+
|
60
|
+
def >(i)
|
61
|
+
self.to_i > i
|
62
|
+
end
|
63
|
+
|
64
|
+
def <(i)
|
65
|
+
self.to_i < i
|
66
|
+
end
|
67
|
+
|
68
|
+
def >=(i)
|
69
|
+
self.to_i >= i
|
70
|
+
end
|
71
|
+
|
72
|
+
def <=(i)
|
73
|
+
self.to_i <= i
|
74
|
+
end
|
75
|
+
|
76
|
+
def ==(i)
|
77
|
+
self.to_i == i
|
78
|
+
end
|
79
|
+
|
80
|
+
def +(i)
|
81
|
+
if i.kind_of?(CryptoUnit)
|
82
|
+
self.class.new(self.to_i + i, from_unit: :primary)
|
83
|
+
else
|
84
|
+
self.to_i + i
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def -(i)
|
89
|
+
if i.kind_of?(CryptoUnit)
|
90
|
+
self.class.new(self.to_i - i, from_unit: :primary)
|
91
|
+
else
|
92
|
+
self.to_i - i
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# IMPORTANT: multiplication is done on primary units, not standart.
|
97
|
+
# 0.01*0.02 will be a larger value.
|
98
|
+
def *(i)
|
99
|
+
if i.kind_of?(CryptoUnit)
|
100
|
+
self.class.new(self.to_i * i, from_unit: :primary)
|
101
|
+
else
|
102
|
+
self.to_i * i
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def coerce(other)
|
107
|
+
if other.kind_of?(Integer)
|
108
|
+
[other, self.to_i]
|
109
|
+
else
|
110
|
+
raise TypeError, message: "CryptoUnit cannot be coerced into anything but Integer"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
protected
|
115
|
+
|
116
|
+
def to_denomination(digits_after_delimiter, as: :number)
|
117
|
+
sign = @value < 0 ? -1 : 1
|
118
|
+
val = @value.abs
|
119
|
+
return val if digits_after_delimiter <= 0
|
120
|
+
leading_zeros = "0"*(18-val.to_s.length)
|
121
|
+
result = leading_zeros + val.to_s
|
122
|
+
result.reverse!
|
123
|
+
result = result.slice(0..digits_after_delimiter-1) + '.' + result.slice(digits_after_delimiter..17)
|
124
|
+
result.reverse!
|
125
|
+
result = result.sub(/\A0*/, '').sub(/0*\Z/, '') # remove zeros on both sides
|
126
|
+
if as == :number
|
127
|
+
result.to_f*sign
|
128
|
+
else
|
129
|
+
if result == '.'
|
130
|
+
result = '0.0'
|
131
|
+
elsif result =~ /\A\./
|
132
|
+
result = "0#{result}"
|
133
|
+
end
|
134
|
+
sign == -1 ? "-#{result}" : result
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def convert_to_primary(n)
|
139
|
+
n = BigDecimal.new(n.to_s)
|
140
|
+
|
141
|
+
decimal_part_length = n.to_s("F").split(".")[1]
|
142
|
+
decimal_part_length = if decimal_part_length
|
143
|
+
decimal_part_length.sub(/0*\Z/, "").length
|
144
|
+
else
|
145
|
+
0
|
146
|
+
end
|
147
|
+
|
148
|
+
if decimal_part_length > UNIT_DENOMINATIONS[@from_unit]
|
149
|
+
raise TooManyDigitsAfterDecimalPoint,
|
150
|
+
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
|
151
|
+
end
|
152
|
+
|
153
|
+
n = ("%.#{UNIT_DENOMINATIONS[@from_unit]}f" % n) # otherwise we might see a scientific notation
|
154
|
+
n = n.split('.')
|
155
|
+
n[1] ||= '' # in the case where there's no decimal part
|
156
|
+
n[1] += "0"*(UNIT_DENOMINATIONS[@from_unit]-n[1].length) if n[1]
|
157
|
+
|
158
|
+
if(n.join.to_i > self.class::MAX_VALUE)
|
159
|
+
raise TooLarge, "Max value for #{self.class::NAME} is #{self.class::MAX_VALUE}"
|
160
|
+
end
|
161
|
+
|
162
|
+
n.join.to_i
|
163
|
+
end
|
164
|
+
end
|
data/lib/litoshi.rb
ADDED
data/lib/satoshi.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require_relative '../lib/crypto-unit'
|
2
|
+
require_relative '../lib/satoshi'
|
3
|
+
require_relative '../lib/litoshi'
|
4
|
+
|
5
|
+
describe CryptoUnit do
|
6
|
+
|
7
|
+
it "creates a Bignum representing value in primary units" do
|
8
|
+
expect(Satoshi.new(1.00).to_i).to eq(100000000)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "takes care of the sign before the value" do
|
12
|
+
expect(Satoshi.new(-1.00).to_i).to eq(-100000000)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "converts primary unit back to some more common denomination" do
|
16
|
+
expect(Satoshi.new(1.00).to_standart).to eq(1)
|
17
|
+
expect(Satoshi.new(1.08763).to_standart).to eq(1.08763)
|
18
|
+
expect(Satoshi.new(1.08763).to_milli).to eq(1087.63)
|
19
|
+
expect(Satoshi.new(-1.08763).to_milli).to eq(-1087.63)
|
20
|
+
expect(Satoshi.new(0.00000001).to_i).to eq(1)
|
21
|
+
expect(Satoshi.new(0.00000001).to_milli).to eq(0.00001)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "converts from various source denominations" do
|
25
|
+
expect(Satoshi.new(1, unit: 'milli').to_standart).to eq(0.001)
|
26
|
+
expect(Satoshi.new(1, unit: 'milli').to_unit).to eq(1)
|
27
|
+
expect(Satoshi.new(10000000, unit: 'milli').to_unit).to eq(10000000)
|
28
|
+
satoshi = Satoshi.new(10000000, unit: 'milli')
|
29
|
+
satoshi.primary_value = 1
|
30
|
+
expect(satoshi.to_unit).to eq(0.00001)
|
31
|
+
expect(Satoshi.new(100, unit: 'milli').to_i).to eq(10000000)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "treats nil in value as 0" do
|
35
|
+
expect(Satoshi.new < 1).to be_truthy
|
36
|
+
expect(Satoshi.new > 1).to be_falsy
|
37
|
+
expect(Satoshi.new == 0).to be_truthy
|
38
|
+
end
|
39
|
+
|
40
|
+
it "converts negative values correctly" do
|
41
|
+
expect(Satoshi.new(-1.00, unit: :milli).to_standart).to eq(-0.001)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "converts zero values correctly" do
|
45
|
+
expect(Satoshi.new(0, unit: :milli).to_unit).to eq(0)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "converts nil values correctly" do
|
49
|
+
s = Satoshi.new(nil, unit: :milli)
|
50
|
+
expect(s.value).to eq(0)
|
51
|
+
s.value = nil
|
52
|
+
expect(s.to_unit).to eq(0)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "displays one primary unit in human form, not math form" do
|
56
|
+
one_satoshi = Satoshi.new(1, from_unit: :primary, to_unit: :standart)
|
57
|
+
expect(one_satoshi.to_unit(as: :string)).not_to eq('1.0e-08')
|
58
|
+
expect(one_satoshi.to_unit(as: :string)).to eq('0.00000001')
|
59
|
+
end
|
60
|
+
|
61
|
+
it "displays zero units in human form, not math form" do
|
62
|
+
zero_satoshi = Satoshi.new(0, from_unit: :primary, to_unit: :standart)
|
63
|
+
expect(zero_satoshi.to_unit(as: :string)).to eq('0.0')
|
64
|
+
end
|
65
|
+
|
66
|
+
it "raises exception if decimal part contains more digits than allowed by from_value" do
|
67
|
+
expect( -> { Satoshi.new(0.001000888888888888888, from_unit: :standart).to_unit }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
68
|
+
expect( -> { Satoshi.new("0.001000999999999999999", from_unit: :standart).to_unit }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
69
|
+
expect( -> { Satoshi.new(0.001000999, from_unit: :standart).to_unit }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
70
|
+
expect( -> { Satoshi.new(0.00100099, from_unit: :standart).to_unit }).not_to raise_exception
|
71
|
+
expect( -> { Satoshi.new(0.123456789, from_unit: :standart) }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
72
|
+
expect( -> { Satoshi.new(0.12345678, from_unit: :standart).to_unit }).not_to raise_exception
|
73
|
+
expect( -> { Satoshi.new(nil, from_unit: :standart).to_unit }).not_to raise_exception
|
74
|
+
end
|
75
|
+
|
76
|
+
it "Satoshi dissallows to create values more than 21mil BTC" do
|
77
|
+
expect( -> { Satoshi.new(21_000_001) }).to raise_exception(CryptoUnit::TooLarge)
|
78
|
+
expect( -> { Satoshi.new(21_000_000) }).not_to raise_exception
|
79
|
+
end
|
80
|
+
|
81
|
+
it "Litoshi disallows to create values more than 84mil LTC" do
|
82
|
+
expect( -> { Litoshi.new(84_000_001) }).to raise_exception(CryptoUnit::TooLarge)
|
83
|
+
expect( -> { Litoshi.new(84_000_000) }).not_to raise_exception
|
84
|
+
end
|
85
|
+
|
86
|
+
it "returns CryptoUnit for +,- and * methods if both operands are CryptoUnit" do
|
87
|
+
s1 = Satoshi.new(0.001, from_unit: :standart)
|
88
|
+
s2 = Satoshi.new(0.002, from_unit: :standart)
|
89
|
+
expect(s1+s2).to be_kind_of(Satoshi)
|
90
|
+
expect((s1+s2).to_unit).to eq(0.003)
|
91
|
+
expect(s2-s1).to be_kind_of(Satoshi)
|
92
|
+
expect((s2-s1).to_unit).to eq(0.001)
|
93
|
+
expect(s2*s1).to be_kind_of(Satoshi)
|
94
|
+
expect((s2*s1).to_unit).to eq(200)
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crypto-unit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roman Snitko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-18 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.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jeweler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.1.2
|
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: Converts various BTC and LTC denominations
|
56
|
+
email: roman.snitko@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.md
|
62
|
+
files:
|
63
|
+
- ".document"
|
64
|
+
- ".rspec"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- VERSION
|
71
|
+
- crypto-unit.gemspec
|
72
|
+
- lib/crypto-unit.rb
|
73
|
+
- lib/crypto_unit_base.rb
|
74
|
+
- lib/litoshi.rb
|
75
|
+
- lib/satoshi.rb
|
76
|
+
- spec/crypto_unit_base_spec.rb
|
77
|
+
homepage: http://github.com/mgpnd/crypto-unit
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.12
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Converts various BTC and LTC denominations
|
101
|
+
test_files: []
|