Class: LibertyReserve::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/liberty_reserve/client.rb

Overview

The Liberty Reserve client, you must instantiate a client for each account API you are using.

Usage example :

lr = LibertyReserve::Client("U6542567", "MySecret", "My API name")
lr.get_balance("LRUSD").to_f
=> 42.0

Constant Summary collapse

API_URI =

Default Liberty Reserve API URI

"https://api.libertyreserve.com/"

Instance Method Summary collapse

Constructor Details

#initialize(account, secret, api_name, options = {}) ⇒ Client

Instantiates a client which will authenticate against the Liberty Reserve API using the supplied credentials :

  • account : The Liberty Reserve account

  • secret : The secret word defined for the particular API

  • api_name : The API name defined in its parameters

Returns a client instance to query the Liberty Reserve API.

Note : all the API calls must be explicitly allowed in the Liberty Reserve API settings.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/liberty_reserve/client.rb', line 30

def initialize(, secret, api_name, options = {})
  super
  
  defaults = {
    api_uri: API_URI,
    verify_peer: true
  }
  
  @account = 
  @secret = secret
  @api_name = api_name
  @options = defaults.merge options
end

Instance Method Details

#get_balance(currency) ⇒ Object

Returns the current balance of the account in the supplied currency (LRUSD, LREUR or LRGLD)

lr = LibertyReserve::Client("U6542567", "MySecret", "My API name")
lr.get_balance("LRUSD").to_f
=> 340.35


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/liberty_reserve/client.rb', line 78

def get_balance(currency)
   = @account

  r = send_request("balance") do |xml|
    xml.BalanceRequest :id => random_id do
      authentication_block(xml)
      
      xml.Balance do
        xml.CurrencyId currency.to_s.upcase
        xml.AccountId 
      end
    end
  end

  r["BalanceResponse"]["Balance"]["Value"].to_d
end

#get_history(currency, till = DateTime.now, from = DateTime.now.advance(days: -14), options = {}) ⇒ Object

Returns the history for an account given a currency as an array of transactions, see get_transaction for the transaction format. Direction direction can be any of :incoming, :outgoing, :any

Raises:

  • (ArgumentError)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/liberty_reserve/client.rb', line 141

def get_history(currency, till = DateTime.now, from = DateTime.now.advance(days: -14), options = {})
  defaults = {
    direction: 'any',
    page_size: 20,
    page_number: 1
  }
  
  opts = defaults.merge(options)
  
  raise ArgumentError unless [:any, :outgoing, :incoming].include?(opts[:direction].to_sym)
  
  r = send_request("history") do |xml|
    xml.HistoryRequest :id => random_id do
      authentication_block(xml)
      xml.History do
        xml.CurrencyId currency
        xml.From from.strftime("%Y-%d-%m 00:00:00")
        xml.Till till.strftime("%Y-%d-%m 23:59:59")
        xml.CorrespondingAccountId opts[:corresponding_account_id] if opts[:corresponding_account_id]
        xml.TransferType opts[:transfer_type] if opts[:transfer_type]
        xml.Source opts[:source] if opts[:source]
        xml.Direction opts[:direction].to_s
        xml.AccountId @account
        xml.Pager do |pager|
          pager.PageSize opts[:page_size]
          pager.PageNumber opts[:page_number]
        end
      end
    end
  end

  if r["HistoryResponse"]["Pager"]["TotalCount"] != "0"
    [r["HistoryResponse"]["Receipt"]].flatten.map { |t| format_transaction(t) }.compact
  else
    []
  end
end

#get_name(account) ⇒ Object

Returns the name of an account given its account number

lr = LibertyReserve::Client("U6542567", "MySecret", "My API name")
lr.get_name("U7262182")
=> "Alice"


123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/liberty_reserve/client.rb', line 123

def get_name()
  r = send_request("accountname") do |xml|
    xml.AccountNameRequest :id => random_id do
      authentication_block(xml)
      xml.AccountName do
        xml.AccountId @account
        xml.AccountToRetrieve 
      end
    end
  end

  r["AccountNameResponse"]["AccountName"]["Name"]
end

#get_transaction(transaction_id) ⇒ Object

Returns the details of a given Liberty Reserve transaction given its ID

lr = LibertyReserve::Client("U6542567", "MySecret", "My API name")
lr.get_transaction("R836232")
=> {
  currency: "LRUSD",
  receipt_id: "R836232",
  payer_account: "U3832029",
  fee: BigDecimal("1.0"),
  transferred_amount: BigDecimal("100.0"),
  net_amount: BigDecimal("99.0")
}


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/liberty_reserve/client.rb', line 56

def get_transaction(transaction_id)
   = @account

  r = send_request("history") do |xml|
    xml.HistoryRequest :id => random_id do
      authentication_block(xml)
      
      xml.History do
        xml.AccountId 
        xml.ReceiptId transaction_id
      end
    end
  end

  format_transaction(r["HistoryResponse"]["Receipt"])
end

#transfer(account, amount, currency, options = {}) ⇒ Object

Performs and account-to-account transfer to an aribitrary account, a memo can optionnally be supplied

lr = LibertyReserve::Client("U6542567", "MySecret", "My API name")
lr.transfer("U987655", 42.0, "LRUSD", :memo => "Hi there!")
=> "R345241"


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/liberty_reserve/client.rb', line 100

def transfer(, amount, currency, options = {})      
  send_request("transfer") do |xml|
    xml.TransferRequest :id => random_id do
      authentication_block(xml)

      xml.Transfer do
        xml.TransferType "transfer"
        xml.Payer @account
        xml.Payee 
        xml.CurrencyId currency
        xml.Amount amount
        xml.Anonymous "false"
        xml.Memo(options[:memo] || "")
      end
    end
  end
end