Class: OpenTransact::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/opentransact/asset.rb

Overview

The Asset is the most important concept in OpenTransact. It represents an asset type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Asset

Returns a new instance of Asset.



6
7
8
9
10
11
# File 'lib/opentransact/asset.rb', line 6

def initialize(url,options={})
  @url = url
  @transaction_url = options[:transaction_url]||@url
  @client = options[:client]
  @options = options||{}
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/opentransact/asset.rb', line 4

def client
  @client
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/opentransact/asset.rb', line 4

def options
  @options
end

#transaction_urlObject

Returns the value of attribute transaction_url.



4
5
6
# File 'lib/opentransact/asset.rb', line 4

def transaction_url
  @transaction_url
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/opentransact/asset.rb', line 4

def url
  @url
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/opentransact/asset.rb', line 43

def [](key)
  @options[key] if @options
end

#available_balanceObject



39
40
41
# File 'lib/opentransact/asset.rb', line 39

def available_balance
  @balance ||= @options["available_balance"] || info["available_balance"]
end

#balanceObject



35
36
37
# File 'lib/opentransact/asset.rb', line 35

def balance
  @balance ||= @options["balance"] || info["balance"]
end

#infoObject

Load meta data about asset from server



48
49
50
# File 'lib/opentransact/asset.rb', line 48

def info
  @info ||= client.try(:get, transaction_url)||{}
end

#nameObject



31
32
33
# File 'lib/opentransact/asset.rb', line 31

def name
  @name ||= @options["name"] || info["name"]
end

#transfer(amount, to, memo = nil) ⇒ Object

Perform a transfer (payment)

@asset.transfer 12, "[email protected]", "For implementing shopping cart"


27
28
29
# File 'lib/opentransact/asset.rb', line 27

def transfer(amount,to,memo=nil)
  client.post(transaction_url,{:amount=>amount,:to=>to,:memo=>memo}) if client
end

#transfer_url(amount, to, memo, options = {}) ⇒ Object

Create a redirect url for a simple web payment

redirect_to @asset.transfer_url 12, "[email protected]", "For implementing shopping cart"


17
18
19
20
21
# File 'lib/opentransact/asset.rb', line 17

def transfer_url(amount,to,memo,options={})
  uri = URI.parse(transaction_url)
  uri.query = URI.escape("amount=#{amount}&to=#{to}&memo=#{memo}")
  uri.to_s
end