bitcoin-money 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7bbd47f91a4b3a01fcdf4be0f27aff9a3a2a8408
4
+ data.tar.gz: 8db8423a57535504626f5b4f22b5ff62e809a210
5
+ SHA512:
6
+ metadata.gz: 98300ed09cb6b940238fde4e44eb99439e01921208499772af1b53e8427221022607081b3a9652c766f227c1ad6a333bfbec6f67ada87c313e5abf1e3f6754ba
7
+ data.tar.gz: 5bd7136a7a0d78cdf6abd198c8d173839336e3c3bf64d794274bf2f32720591667620c95ceccfc1ad0cc7b4672ee8cfaf9aab75ec0d88b88950f24aad78635cb
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bitcoin-money.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 James Larisch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Bitcoin Money!
2
+
3
+ The Money gem is a popular gem for Ruby.
4
+ Ruby needs a gem like Money, for Bitcoin!
5
+
6
+ But BTC is unique, so this simple object should be unique.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'bitcoin-money'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install bitcoin-money
21
+
22
+ ## Usage
23
+
24
+ Require it!
25
+
26
+ `require 'btc'`
27
+
28
+ Create from whichever unit of BTC.
29
+ ~~~ruby
30
+ a = Btc.new(0.012)
31
+ b = Btc.from_mbtc(12)
32
+ c = Btc.from_satoshis(120000)
33
+ a == b # true
34
+ b == c # true
35
+ # a == b == c
36
+ ~~~
37
+
38
+ Get value in any unit of BTC.
39
+ ~~~ruby
40
+ b # => #<Btc BTC:0.012 mBTC: 12.0 Satoshis: 120000.0>
41
+ b.mbtc # => BigDecimal.new('12.0')
42
+ b.satoshis # => BigDecimal.new('120000.0')
43
+ b.btc # => BigDecimal.new('0.012')
44
+ ~~~
45
+
46
+ Perform operations.
47
+ ~~~ruby
48
+ a + b # == Bitcoin.new(0.024)
49
+ a * a # == Bitcoin.new(0.000144)
50
+ a / c # == Bitcoin.new(1)
51
+ # also ** and -
52
+ ~~~
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( https://github.com/zenchilabs/bitcoin-money/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bitcoin-money"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["James Larisch"]
9
+ spec.email = ["root@jameslarisch.com"]
10
+ spec.summary = "Represent BTC like Money, with nice precision."
11
+ spec.description = "Represent BTC like Money, with nice precision."
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(spec|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "rspec", "~> 3.0.0"
22
+
23
+ end
data/lib/btc.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'bigdecimal'
2
+
3
+ class Btc
4
+ SATOSHI = BigDecimal.new('0.00000001')
5
+ MBTC = BigDecimal.new('0.0001')
6
+ BTC = BigDecimal.new('1.0')
7
+
8
+ def initialize(amt)
9
+ raise ArgumentError, 'BTC amount must not be less than 0.00000001' unless Btc.valid_btc(amt)
10
+ @amt = BigDecimal.new(amt.to_f.to_s)
11
+ end
12
+
13
+ def self.from_mbtc(amt)
14
+ raise ArgumentError, 'mBTC amount must not be less than 0.00001' unless Btc.valid_mbtc(amt)
15
+ Btc.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
16
+ end
17
+
18
+ def self.from_satoshis(amt)
19
+ raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless Btc.valid_satoshis(amt)
20
+ Btc.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
21
+ end
22
+
23
+ def self.valid_satoshis(amt)
24
+ amt.to_i >= 0
25
+ end
26
+
27
+ def self.valid_mbtc(amt)
28
+ amt.to_f >= MBTC.to_f || amt.to_f == 0
29
+ end
30
+
31
+ def self.valid_btc(amt)
32
+ amt.to_f >= SATOSHI.to_f || amt.to_f == 0
33
+ end
34
+
35
+ def btc
36
+ @amt
37
+ end
38
+
39
+ def mbtc
40
+ @amt / MBTC
41
+ end
42
+
43
+ def satoshis
44
+ @amt / SATOSHI
45
+ end
46
+
47
+ def +(other)
48
+ Btc.new(@amt + other.btc)
49
+ end
50
+
51
+ def /(other)
52
+ Btc.new(@amt / other.btc)
53
+ end
54
+
55
+ def -(other)
56
+ Btc.new(@amt - other.btc)
57
+ end
58
+
59
+ def *(other)
60
+ Btc.new(@amt * other.btc)
61
+ end
62
+
63
+ def **(exp)
64
+ Btc.new(@amt ** exp)
65
+ end
66
+
67
+ def ==(other)
68
+ @amt == other.btc
69
+ end
70
+
71
+ def inspect
72
+ "#<Btc BTC:#{btc} mBTC:#{mbtc} Satoshis:#{satoshis}>"
73
+ end
74
+ end
data/spec/btc_spec.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'btc'
2
+
3
+ describe Btc do
4
+
5
+ it 'should initialize okay' do
6
+ b = Btc.new(12345)
7
+ expect(b.btc).to eq(BigDecimal.new('12345.0'))
8
+ end
9
+
10
+ it 'should throw error if invalid btc amount' do
11
+ expect{ Btc.new(0.00000000000001) }.to raise_error(ArgumentError)
12
+ end
13
+
14
+ it 'should throw error if invalid mbtc amount' do
15
+ expect{ Btc.from_mbtc(Btc::SATOSHI) }.to raise_error(ArgumentError)
16
+ end
17
+
18
+ it 'should throw error if invalid satoshi amount' do
19
+ expect{ Btc.from_satoshis(-1) }.to raise_error(ArgumentError)
20
+ end
21
+
22
+ it 'should convert btc to mbtc and back' do
23
+ expect(Btc.new(Btc::MBTC.to_f).mbtc).to eq(BigDecimal.new('1'))
24
+ expect(Btc.from_mbtc(1).btc).to eq(BigDecimal.new(Btc::MBTC))
25
+ end
26
+
27
+ it 'should convert btc to satoshis and back' do
28
+ expect(Btc.new(Btc::SATOSHI).satoshis).to eq(BigDecimal.new('1'))
29
+ expect(Btc.from_satoshis(1).btc).to eq(BigDecimal.new(Btc::SATOSHI))
30
+ end
31
+
32
+ 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'))
35
+ end
36
+
37
+ 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))
41
+ end
42
+
43
+ it 'should use multiplication correctly' do
44
+ expect(Btc.new(0.1234) * Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') * BigDecimal.new('0.1234')))
45
+ end
46
+
47
+ it 'should use division correctly' do
48
+ expect(Btc.new(0.1234) / Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') / BigDecimal.new('0.1234')))
49
+ end
50
+
51
+ it 'should use addition correctly' do
52
+ expect(Btc.new(0.1234) + Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') + BigDecimal.new('0.1234')))
53
+ end
54
+
55
+ it 'should use subtraction correctly' do
56
+ expect(Btc.new(0.1234) - Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') - BigDecimal.new('0.1234')))
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitcoin-money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Larisch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ description: Represent BTC like Money, with nice precision.
42
+ email:
43
+ - root@jameslarisch.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bitcoin-money.gemspec
54
+ - lib/btc.rb
55
+ - spec/btc_spec.rb
56
+ homepage: ''
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Represent BTC like Money, with nice precision.
80
+ test_files:
81
+ - spec/btc_spec.rb