bitcoin-money 0.1.1 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ced3834d2b76abe44f4f3655bf2f8e90f1bc4f57
4
- data.tar.gz: 0c4462c69d7d83b322e899b1dbc41a732b152e6e
3
+ metadata.gz: f8c2b56d01646ff8f96d38e4821f611286ecbc4e
4
+ data.tar.gz: 46b67a5e36c48f977d1a056bcb4a825a78c946b8
5
5
  SHA512:
6
- metadata.gz: 51e4d7e2990907afc61ddefa87ba0779d9d99b149479a50871034de31ef3af403b86c5f54abaf920fc5a35e42fee5094912d37a4925dc8993eb996e6e9a2b512
7
- data.tar.gz: 591f6d80dbaef82dd6473974b0ff811d41c3f8f626bcff264e199c90b17b85e506e773a57d1b162f076e029503acf29638cfdb0c6e16216823f475eab17fdc57
6
+ metadata.gz: 8f2873c30ecedc1f138f2487e3f7aaf47ce19b954eb0d24e12749a0b5d38cf4ae4b50cd9b22c9e044c1af13dc3c7478400a7cb5456dabe598309c28a86308872
7
+ data.tar.gz: 26678703f1d216e024df5190648c9b8a0a07988f52511e0d6edb24cd3143e7cb1ed5cddcb322f04f6c10dafaa340e7f2d4c853bd063619d03de7e3696ef8278f
data/README.md CHANGED
@@ -45,9 +45,9 @@ b.btc # => BigDecimal.new('0.012')
45
45
 
46
46
  Perform operations.
47
47
  ~~~ruby
48
- a + b # == Bitcoin.new(0.024)
49
- a * a # == Bitcoin.new(0.000144)
50
- a / c # == Bitcoin.new(1)
48
+ a + b # == Btc.new(0.024)
49
+ a * a # == Btc.new(0.000144)
50
+ a / c # == Btc.new(1)
51
51
  # also ** and -
52
52
  ~~~
53
53
 
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "bitcoin-money"
7
- spec.version = "0.1.1"
7
+ spec.version = "0.5.0"
8
8
  spec.authors = ["James Larisch"]
9
9
  spec.email = ["root@jameslarisch.com"]
10
10
  spec.summary = "Represent BTC like Money, with nice precision."
data/lib/btc.rb CHANGED
@@ -1,35 +1,50 @@
1
1
  require 'bigdecimal'
2
2
 
3
3
  class Btc
4
- SATOSHI = BigDecimal.new('0.00000001')
5
- MBTC = BigDecimal.new('0.0001')
6
4
  BTC = BigDecimal.new('1.0')
5
+ MBTC = BigDecimal.new('0.001')
6
+ UBTC = BigDecimal.new('0.000001')
7
+ SATOSHI = BigDecimal.new('0.00000001')
8
+
9
+ MIN_BTC = SATOSHI.to_f
10
+ MIN_MBTC = 0.00001
11
+ MIN_UBTC = 0.01
12
+ MIN_SATOSHI = 1
7
13
 
8
14
  def initialize(amt)
9
- raise ArgumentError, 'BTC amount must not be less than 0.00000001' unless Btc.valid_btc(amt)
15
+ raise ArgumentError, "BTC amount must not be less than #{MIN_BTC}" unless Btc.valid_btc(amt)
10
16
  @amt = BigDecimal.new(amt.to_f.to_s)
11
17
  end
12
18
 
13
19
  def self.from_mbtc(amt)
14
- raise ArgumentError, 'mBTC amount must not be less than 0.00001' unless Btc.valid_mbtc(amt)
20
+ raise ArgumentError, "mBTC amount must not be less than #{MIN_MBTC}" unless Btc.valid_mbtc(amt)
15
21
  Btc.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
16
22
  end
17
23
 
24
+ def self.from_ubtc(amt)
25
+ raise ArgumentError, "uBTC amount must not be less than #{MIN_UBTC}" unless Btc.valid_ubtc(amt)
26
+ Btc.new(BigDecimal.new(amt.to_f.to_s) * UBTC)
27
+ end
28
+
18
29
  def self.from_satoshis(amt)
19
30
  raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless Btc.valid_satoshis(amt)
20
31
  Btc.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
21
32
  end
22
33
 
23
- def self.valid_satoshis(amt)
24
- amt.to_i >= 0
34
+ def self.valid_btc(amt)
35
+ amt.to_f >= SATOSHI.to_f || amt.to_f == 0.0
25
36
  end
26
37
 
27
38
  def self.valid_mbtc(amt)
28
- amt.to_f >= MBTC.to_f || amt.to_f == 0
39
+ amt.to_f >= MIN_MBTC || amt.to_f == 0.0
29
40
  end
30
41
 
31
- def self.valid_btc(amt)
32
- amt.to_f >= SATOSHI.to_f || amt.to_f == 0
42
+ def self.valid_ubtc(amt)
43
+ amt.to_f >= MIN_UBTC || amt.to_f == 0.0
44
+ end
45
+
46
+ def self.valid_satoshis(amt)
47
+ amt.to_i >= 0
33
48
  end
34
49
 
35
50
  def btc
@@ -40,6 +55,10 @@ class Btc
40
55
  @amt / MBTC
41
56
  end
42
57
 
58
+ def ubtc
59
+ @amt / UBTC
60
+ end
61
+
43
62
  def satoshis
44
63
  @amt / SATOSHI
45
64
  end
@@ -69,6 +88,6 @@ class Btc
69
88
  end
70
89
 
71
90
  def inspect
72
- "#<Btc BTC:#{btc} mBTC:#{mbtc} Satoshis:#{satoshis}>"
91
+ "#<Btc BTC:#{btc.to_f} mBTC:#{mbtc.to_f} uBTC:#{ubtc.to_f} Satoshis:#{satoshis.to_f}>"
73
92
  end
74
93
  end
data/spec/btc_spec.rb CHANGED
@@ -2,21 +2,33 @@ require 'btc'
2
2
 
3
3
  describe Btc do
4
4
 
5
- it 'should initialize okay' do
6
- b = Btc.new(12345)
7
- expect(b.btc).to eq(BigDecimal.new('12345.0'))
5
+ describe '#new' do
6
+ it 'should initialize okay' do
7
+ b = Btc.new(12345)
8
+ expect(b.btc).to eq(BigDecimal.new('12345.0'))
9
+ end
10
+
11
+ it 'should throw error if invalid btc amount' do
12
+ expect{ Btc.new(0.00000000000001) }.to raise_error(ArgumentError)
13
+ end
8
14
  end
9
15
 
10
- it 'should throw error if invalid btc amount' do
11
- expect{ Btc.new(0.00000000000001) }.to raise_error(ArgumentError)
16
+ describe '.from_mbtc' do
17
+ it 'should throw error if invalid mbtc amount' do
18
+ expect{ Btc.from_mbtc(Btc::SATOSHI) }.to raise_error(ArgumentError)
19
+ end
12
20
  end
13
21
 
14
- it 'should throw error if invalid mbtc amount' do
15
- expect{ Btc.from_mbtc(Btc::SATOSHI) }.to raise_error(ArgumentError)
22
+ describe '.from_ubtc' do
23
+ it 'should throw error if invalid ubtc amount' do
24
+ expect{ Btc.from_ubtc(Btc::MIN_UBTC - 10) }.to raise_error(ArgumentError)
25
+ end
16
26
  end
17
27
 
18
- it 'should throw error if invalid satoshi amount' do
19
- expect{ Btc.from_satoshis(-1) }.to raise_error(ArgumentError)
28
+ describe '.from_satoshis' do
29
+ it 'should throw error if invalid satoshi amount' do
30
+ expect{ Btc.from_satoshis(-1) }.to raise_error(ArgumentError)
31
+ end
20
32
  end
21
33
 
22
34
  it 'should convert btc to mbtc and back' do
@@ -30,14 +42,19 @@ describe Btc do
30
42
  end
31
43
 
32
44
  it 'should convert mbtc to satoshis and back' do
33
- expect(Btc.from_mbtc(1).satoshis).to eq(BigDecimal.new('10000'))
34
- expect(Btc.from_satoshis(1000).mbtc).to eq(BigDecimal.new('0.1'))
45
+ expect(Btc.from_mbtc(1).satoshis).to eq(BigDecimal.new('100000'))
46
+ expect(Btc.from_satoshis(10000).mbtc).to eq(BigDecimal.new('0.1'))
47
+ end
48
+
49
+ it 'should convert btc to ubtc and back' do
50
+ expect(Btc.new(1).ubtc).to eq(BigDecimal.new('1000000'))
51
+ expect(Btc.from_ubtc(12).btc).to eq(BigDecimal('.000012'))
35
52
  end
36
53
 
37
54
  it 'should use equality correctly' do
38
- expect(Btc.new(0.1234)).to eq(Btc.from_mbtc(1234))
39
- expect(Btc.new(0.1234)).to eq(Btc.from_satoshis(12340000))
40
- expect(Btc.from_mbtc(12)).to eq(Btc.from_satoshis(120000))
55
+ expect(Btc.new(0.123)).to eq(Btc.from_mbtc(123))
56
+ expect(Btc.new(0.123)).to eq(Btc.from_satoshis(12300000))
57
+ expect(Btc.from_mbtc(12)).to eq(Btc.from_satoshis(1200000))
41
58
  end
42
59
 
43
60
  it 'should use multiplication correctly' do
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitcoin-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Larisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-17 00:00:00.000000000 Z
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.0.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.0.0
41
41
  description: Represent BTC like Money, with nice precision.
@@ -45,7 +45,7 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - ".gitignore"
48
+ - .gitignore
49
49
  - Gemfile
50
50
  - LICENSE.txt
51
51
  - README.md
@@ -63,12 +63,12 @@ require_paths:
63
63
  - lib
64
64
  required_ruby_version: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - ">="
71
+ - - '>='
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []