Class: LibertyReserveLink::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/liberty_reserve_link/client.rb

Defined Under Namespace

Classes: InvalidCredentialException

Constant Summary collapse

FORMAT =
:json
API_HOST =
"https://api2.libertyreserve.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credential) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
# File 'lib/liberty_reserve_link/client.rb', line 23

def initialize(credential)
  @credential = credential
  check_credential
  @token = LibertyReserveLink::Token.new @credential
end

Instance Attribute Details

#credentialObject (readonly)

Returns the value of attribute credential.



21
22
23
# File 'lib/liberty_reserve_link/client.rb', line 21

def credential
  @credential
end

Instance Method Details

#api_path(operation) ⇒ Object



33
34
35
# File 'lib/liberty_reserve_link/client.rb', line 33

def api_path(operation)
  "/json/#{operation}"
end

#balance(currency = nil) ⇒ Object



53
54
55
56
# File 'lib/liberty_reserve_link/client.rb', line 53

def balance(currency=nil)
  id = random_id
  parse_balance currency, Client.post(api_path("balance"), :query => common_json.merge({:id => id, :token => @token.balance(id)}))
end

#check_credentialObject



29
30
31
# File 'lib/liberty_reserve_link/client.rb', line 29

def check_credential
  raise InvalidCredentialException unless @credential.valid?
end

#common_jsonObject



37
38
39
# File 'lib/liberty_reserve_link/client.rb', line 37

def common_json
  {:account => @credential., :api => @credential.name}
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)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/liberty_reserve_link/client.rb', line 89

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

#name(account) ⇒ Object

auth error here



80
81
82
83
# File 'lib/liberty_reserve_link/client.rb', line 80

def name()
  id = random_id
  JSON.parse Client.post(api_path("accountname"), :query => common_json.merge({:id => id, :token => @token.balance(), :search => })).to_json
end

#parse_balance(currency, resp) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/liberty_reserve_link/client.rb', line 41

def parse_balance(currency, resp)
  if resp["Balance"]
    if currency
      resp["Balance"][currency]
    else
      resp["Balance"]
    end
  else
    nil
  end
end

#transaction(transaction_id) ⇒ Object



74
75
76
77
# File 'lib/liberty_reserve_link/client.rb', line 74

def transaction(transaction_id)
  id = random_id
  JSON.parse Client.post(api_path("findtransaction"), :query => common_json.merge({:token => @token.find_transaction(id, transaction_id), :id => id, :batch => transaction_id.to_s})).to_json
end

#transfer(receiver, amount, currency, memo) ⇒ Object



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

def transfer(receiver, amount, currency, memo)
  id = random_id
  query = common_json.merge({
      :id => id,
      :token => @token.transfer(id, receiver, currency, amount),
      :reference => "Reference",
      :type => "transfer",
      :payee => receiver,
      :currency => currency,
      :amount => (amount.is_a?(String) ? amount.to_f.round(2) : amount),
      :memo => memo,
      :purpose => "service"
  })
  JSON.parse Client.post(api_path("transfer"), :query => query).to_json
end