Class: Rippler::Money
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#issuer ⇒ Object
Returns the value of attribute issuer.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(data) ⇒ Money
constructor
Money comes in in 3 formats: Hash: => xx, ‘currency’ => ‘XXX’, ‘issuer’ => ‘ZZZZZZZ’ String: ‘value/currency(/issuer)’ Int in a String: XRP amount in drops (x1,000,000) ? Also, generic currency id without value: ? XRP or USD/bitstamp.
-
#method_missing(meth, *args) ⇒ Object
Allows methods such as xrp? usd? or btc?.
-
#rate(cross) ⇒ Object
Uniform currency rate presentation.
- #to_hash ⇒ Object
- #to_s ⇒ Object
Methods included from Utils
#Account, #Money, #Offer, #Time
Constructor Details
#initialize(data) ⇒ Money
Money comes in in 3 formats: Hash: => xx, ‘currency’ => ‘XXX’, ‘issuer’ => ‘ZZZZZZZ’ String: ‘value/currency(/issuer)’ Int in a String: XRP amount in drops (x1,000,000) ? Also, generic currency id without value: ? XRP or USD/bitstamp
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rippler/money.rb', line 13 def initialize data case data when Hash @value = data['value'].to_f @currency = data['currency'] @issuer = data['issuer'] when String @value, @currency, @issuer = *data.split('/') if @value.to_f == 0 # No value, must be generic currency: XRP or USD/bitstamp @currency, @issuer = @value, @currency else if @currency @value = @value.to_f else @value = @value.to_i/1000000.0 @currency = "XRP" end end when Integer @value = data.to_i/1000000.0 @currency = "XRP" end @value = @value.to_i if @value.to_i == @value @issuer = Account(@issuer) if @issuer end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Allows methods such as xrp? usd? or btc?
57 58 59 60 61 62 63 64 |
# File 'lib/rippler/money.rb', line 57 def method_missing meth, *args curr = meth.to_s.upcase.match(/^(...)\?$/)[1] if curr currency == curr else super end end |
Instance Attribute Details
#currency ⇒ Object
Returns the value of attribute currency.
5 6 7 |
# File 'lib/rippler/money.rb', line 5 def currency @currency end |
#issuer ⇒ Object
Returns the value of attribute issuer.
5 6 7 |
# File 'lib/rippler/money.rb', line 5 def issuer @issuer end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/rippler/money.rb', line 5 def value @value end |
Instance Method Details
#rate(cross) ⇒ Object
Uniform currency rate presentation
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rippler/money.rb', line 42 def rate cross first, second = if self.xrp? || cross.dym? [self, cross] elsif cross.xrp? || self.btc? [cross, self] else [self, cross] end r = first.value.to_f/second.value.to_f r = r.to_i == r ? r.to_i : r.round(2) "#{r} #{first.currency}/#{second.currency}" end |
#to_hash ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/rippler/money.rb', line 74 def to_hash if @issuer {'value' => @value.to_s, 'currency' => @currency, 'issuer' => @issuer.address} else {'value' => @value.to_s, 'currency' => @currency} end end |
#to_s ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/rippler/money.rb', line 66 def to_s if @issuer "#{@value}/#{@currency}/#{@issuer.name}" else "#{@value}/#{@currency}" end end |