class Bitodeme::Resource::Withdrawal

Withdrawal resource

Attributes

address[R]

Address

amount[R]

Amount

fund_id[R]

Fund identifier

id[R]

Unique identifier

otp_value[R]

One time password value gathered from Google Authenticator

Public Class Methods

build(params) click to toggle source

Build a withdrawal object

Example:

>> fund_id    = '12b241a7-941e-43a8-878e-a467809e988e'
>> withdrawal =
>>   Bitodeme::Resource::Withdrawal.build(
>>     amount:    0.0013,
>>     address:   'morg4YKzAESEktS7H74dtaavFjuhNUi8zq',
>>     fund_id:   fund_id,
>>     otp_value: "123456" # otp value from Google Authenticator
>>   )

Arguments:

params: (Hash)
# File lib/bitodeme/resources/withdrawal.rb, line 37
def build(params)
  new(params).send(:validate)
end
create(withdrawal) click to toggle source

Craete a new withdrawal(send money)

Example:

>> fund_id    = '12b241a7-941e-43a8-878e-a467809e988e'
>> withdrawal =
>>   Bitodeme::Resource::Withdrawal.build(
>>     amount:    0.0013,
>>     address:   'morg4YKzAESEktS7H74dtaavFjuhNUi8zq',
>>     fund_id:   fund_id,
>>     otp_value: "123456" # otp value from Google Authenticator
>>   )

>> Bitodeme::Resource::Withdrawal.create(withdrawal)

Arguments:

withdrawal: (Bitodeme::Resource::Withdrawal)
# File lib/bitodeme/resources/withdrawal.rb, line 57
def create(withdrawal)
  raise_for('withdrawal', withdrawal) unless withdrawal.is_a?(Withdrawal)
  data = { withdrawal: withdrawal.to_h }
  post('/api/v1/withdrawals', data).body
end
new(params) click to toggle source
Calls superclass method Bitodeme::Resource::Base::new
# File lib/bitodeme/resources/withdrawal.rb, line 72
def initialize(params)
  super(attrs: attrs, params: params)
end

Private Class Methods

new(attrs:, params:) click to toggle source
# File lib/bitodeme/resources/base.rb, line 54
def initialize(attrs:, params:)
  attrs.each do |attr|
    val = params.fetch(attr.to_s, params.fetch(attr, nil))
    instance_variable_set("@#{attr}", val)
  end
end
raise_for(attr, val) click to toggle source
# File lib/bitodeme/resources/withdrawal.rb, line 65
def raise_for(attr, val)
  raise Bitodeme::ValidationError.new(attr, val)
end

Private Instance Methods

validate() click to toggle source
# File lib/bitodeme/resources/withdrawal.rb, line 76
def validate
  validate_address
  validate_amount
  validate_fund_id
  validate_otp_value
  self
end
validate_address() click to toggle source
# File lib/bitodeme/resources/withdrawal.rb, line 84
def validate_address
  raise_for('address') unless address.is_a?(String)
end
validate_amount() click to toggle source
# File lib/bitodeme/resources/withdrawal.rb, line 93
def validate_amount
  raise_for('amount') unless /^\d{0,8}(\.\d{1,8})?$/.match?(amount.to_s)
end
validate_fund_id() click to toggle source

TODO: Validate with UUID format

# File lib/bitodeme/resources/withdrawal.rb, line 89
def validate_fund_id
  raise_for('fund_id') unless fund_id.is_a?(String)
end
validate_otp_value() click to toggle source
# File lib/bitodeme/resources/withdrawal.rb, line 97
def validate_otp_value
  raise_for('otp_value') unless /^\d{6}$/.match?(otp_value.to_s)
end