Class: Elements::Api
- Inherits:
-
Object
- Object
- Elements::Api
- Defined in:
- lib/elements.rb
Overview
Api class defaults to the V2 of the elements API
Direct Known Subclasses
Class Method Summary collapse
-
.add_forex_offer(exchange_rate) ⇒ Object
Add Forex Offer is a class method that add the foreign exchange rate for virtual currencies input: ‘AMG’, 100, ‘AMS’, 60.
-
.add_user(user) ⇒ Object
Add_user is a class method of Api class that provides adding users to elements.
-
.buy_purchase_info(order, ipaddress) ⇒ Object
Buy Purchase Info is a class method that returns the buy line items and payment information for the order.
-
.cancel_forex_offer(forex_offer_id, ipaddress) ⇒ Object
Update Forex Offer to Status CANCELLED.
-
.cancel_purchase_order(order, ipaddress = '1.1.1.1') ⇒ Object
Cancel Purchase Order is a class method that cancels an order that has already been created.
-
.create_account(account, ip_address) ⇒ Object
Create account is a class method that creates an account in elements from the Account model object input: eg.
-
.create_purchase_order(order, ipaddress = '1.1.1.1') ⇒ Object
Create purchase order is a class method that creates a new purchaseOrder in the elements system depending on the order type.
-
.exchange_purchase_info(order, ipaddress) ⇒ Object
Sell Purchase Info is a class method that returns the exchange line items and payment information for the order.
-
.fetch_order_elements(order) ⇒ Object
Fetch Order Elements is a class method that fetches the elements_id from order table depending on the order type.
-
.find_account_name(account) ⇒ Object
Find account is a class method that find account via account_id input: eg.
-
.find_active_forex_offer(exchange_rate) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg.
-
.find_balance(account, ip_address) ⇒ Object
Find balance is a class method that returns the balance of the Account model object from elements input eg.
-
.find_forex_offer_by_currency(from_currency, to_currency) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg.
-
.find_forex_offer_by_external_key(external_key) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg.
-
.find_forex_offer_by_id(forex_offer_id) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg.
- .find_or_create_forex_offer(exchange_rate, ipaddress) ⇒ Object
-
.find_purchase_order(purchase_order_id) ⇒ Object
Find purchase Order is a class method that returns the purchase order output: xml with attributes.
-
.find_user(user) ⇒ Object
Find_user is a class method of Api class that provides finding users that are added in elements.
-
.process_purchase_order(order, ipaddress = '1.1.1.1') ⇒ Object
Process purchase order is a class method that processes an already created order.
-
.save_order_elements(elements_ids, order) ⇒ Object
Save Order Elements is a class method that saves the elements_id from order table depending on the order type.
-
.sell_purchase_info(order, ipaddress) ⇒ Object
Sell Purchase Info is a class method that returns the sell line items and payment information for the order.
-
.set_account_balance(account, ip_address) ⇒ Object
Add account balance is a class method that adds the account balance to the elements input: eg.
Class Method Details
.add_forex_offer(exchange_rate) ⇒ Object
Add Forex Offer is a class method that add the foreign exchange rate for virtual currencies input: ‘AMG’, 100, ‘AMS’, 60
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/elements.rb', line 90 def self.add_forex_offer(exchange_rate) params = { storeId: 3190, description: exchange_rate.to_s, fromCurrencyName: exchange_rate.from_currency.vcx_name, toCurrencyName: exchange_rate.to_currency.vcx_name, exchangeRate: exchange_rate.exchange_rate, externalKey: exchange_rate.id, effectiveDate: generate_date(DateTime.now), expirationDate: generate_date(DateTime.now + 75.years), isDefault: false, isSpecial: false, status: 'ACTIVE' } post_data("forexOffer", params) end |
.add_user(user) ⇒ Object
Add_user is a class method of Api class that provides adding users to elements
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/elements.rb', line 15 def self.add_user(user) params = { name: user.email.gsub('@','_'), emailaddress: user.email, namespaceId: '0' #gender: user.gender, #dateOfBirth: user.dob.strftime("%m/%d/%Y") } response = post_data 'user', params result = XmlSimple.xml_in(response) user.update_column(:elements_id, result['id']) end |
.buy_purchase_info(order, ipaddress) ⇒ Object
Buy Purchase Info is a class method that returns the buy line items and payment information for the order
156 157 158 |
# File 'lib/elements.rb', line 156 def self.buy_purchase_info(order, ipaddress) sell_purchase_info(order, ipaddress) end |
.cancel_forex_offer(forex_offer_id, ipaddress) ⇒ Object
Update Forex Offer to Status CANCELLED
83 84 85 86 |
# File 'lib/elements.rb', line 83 def self.cancel_forex_offer(forex_offer_id, ipaddress) params = {'forexOfferId' => forex_offer_id, 'ipAddress' => ipaddress, 'status' => 'CANCELLED'} put_data("forexOffer/#{forex_offer_id}", params) end |
.cancel_purchase_order(order, ipaddress = '1.1.1.1') ⇒ Object
Cancel Purchase Order is a class method that cancels an order that has already been created
140 141 142 143 144 145 |
# File 'lib/elements.rb', line 140 def self.cancel_purchase_order( order, ipaddress='1.1.1.1' ) params = { commandType: 'CANCEL' } XmlSimple.xml_in post_xml_data("purchaseOrder/#{order.elements_id}/command", params,"purchaseOrderCommand") end |
.create_account(account, ip_address) ⇒ Object
Create account is a class method that creates an account in elements from the Account model object input: eg. Account.find(254), ‘255.255.255.255’ output: elements id
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/elements.rb', line 233 def self.create_account(account, ip_address) params ={ program: " var accountId = es.addAccount(#{account.user.elements_id}, 'account#{account.id}', #{account.currency.elements_id}, null, authInfo); es.addToAccountBalance(new java.lang.Long(accountId), new java.math.BigDecimal(#{account.balance}), requestContext, authInfo); '' + accountId; ", ipAddress: ip_address } elements_account_id = post_data("batch", params) + '' #account.update_column('elements_id', elements_account_id.to_i) elements_account_id end |
.create_purchase_order(order, ipaddress = '1.1.1.1') ⇒ Object
Create purchase order is a class method that creates a new purchaseOrder in the elements system depending on the order type
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/elements.rb', line 108 def self.create_purchase_order( order, ipaddress='1.1.1.1' ) lineItems, payment = send("#{order.order_type}_purchase_info", order, ipaddress) elements_ids = Array.new lineItems.each_with_index do |lineItem, index| params = { orderCommand: 'AUTHORIZE', buyerUser: {'@id' => order.user.elements_id}, lineItems: lineItem, payment: payment[index] } rs = XmlSimple.xml_in post_xml_data("purchaseOrder", params,"purchaseOrder") elements_ids << rs['id'] end save_order_elements(elements_ids, order) end |
.exchange_purchase_info(order, ipaddress) ⇒ Object
Sell Purchase Info is a class method that returns the exchange line items and payment information for the order
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/elements.rb', line 174 def self.exchange_purchase_info(order, ipaddress) vcx_currency = Currency.find_by_currency_type('real') sell_exchange_rate = Forex.find_exchange_rate(Forex.find_by_currency_id(order.from_currency), Forex.find_by_currency_id(vcx_currency)) buy_exchange_rate = Forex.find_exchange_rate(Forex.find_by_currency_id(vcx_currency), Forex.find_by_currency_id(order.to_currency)) line_items = Array.new line_items << [{ 'forexOffer' => { 'forexOfferRequest' => { '@amountFromCurrency' => order.from_amount.abs, '@id' => find_or_create_forex_offer(sell_exchange_rate, ipaddress), '@buyerAccountId' => order.user.accounts.find_by_currency_id(vcx_currency).elements_id } } }] line_items << [{ 'forexOffer' => { 'forexOfferRequest' => { '@amountFromCurrency' => order.from_amount.abs * sell_exchange_rate.exchange_rate, '@id' => find_or_create_forex_offer(buy_exchange_rate, ipaddress), '@buyerAccountId' => order.to_account.elements_id } } }] payment = [ { '@currencyId' => order.from_currency.elements_id, 'tfeAccountId' => order.from_account.elements_id }, { '@currencyId' => vcx_currency.elements_id, 'tfeAccountId' => order.user.accounts.find_by_currency_id(vcx_currency).elements_id } ] return line_items, payment end |
.fetch_order_elements(order) ⇒ Object
Fetch Order Elements is a class method that fetches the elements_id from order table depending on the order type
201 202 203 204 205 206 207 208 209 |
# File 'lib/elements.rb', line 201 def self.fetch_order_elements(order) elements_id = order.elements_id case order.order_type when /['sell'|'buy']/ [elements_id] when 'exchange' order.elements_id.split('|') end end |
.find_account_name(account) ⇒ Object
Find account is a class method that find account via account_id input: eg. Account.find(254)
output: eg. “account254”
225 226 227 228 |
# File 'lib/elements.rb', line 225 def self.find_account_name(account) params = {program: "var account = es.findAccountById(#{account.elements_id}, authInfo); '' + account.name;", ipAddress: '123.123.123.123'} post_data("batch", params) end |
.find_active_forex_offer(exchange_rate) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg. exchange_rate output: xml with attributes eg. exchangeRate=“0.011”
52 53 54 55 56 |
# File 'lib/elements.rb', line 52 def self.find_active_forex_offer(exchange_rate) params = {'fromCurrencyName' => exchange_rate.from_currency.vcx_name, 'toCurrencyName' => exchange_rate.to_currency.vcx_name, 'statuses' => 'ACTIVE' } get_data("forexOffers", params) end |
.find_balance(account, ip_address) ⇒ Object
Find balance is a class method that returns the balance of the Account model object from elements input eg. Account.find(254), ‘255.255.255.255’ output: eg. 3214
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/elements.rb', line 250 def self.find_balance(account, ip_address) params = { program: " var balance = es.balanceInquiry(#{account.elements_id}, authInfo); '' + balance.amount; ", ipAddress: ip_address } post_data("batch", params) end |
.find_forex_offer_by_currency(from_currency, to_currency) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg. ‘AMS’, ‘AMG’ output: xml with attributes eg. exchangeRate=“0.011”
44 45 46 47 |
# File 'lib/elements.rb', line 44 def self.find_forex_offer_by_currency(from_currency, to_currency) params = {'fromCurrencyName' => from_currency, 'toCurrencyName' => to_currency } get_data("forexOffers", params) end |
.find_forex_offer_by_external_key(external_key) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg. exchange_rate output: xml with attributes eg. exchangeRate=“0.011”
77 78 79 80 |
# File 'lib/elements.rb', line 77 def self.find_forex_offer_by_external_key(external_key) params = {'externalKey' => external_key} get_data("forexOffers", params) end |
.find_forex_offer_by_id(forex_offer_id) ⇒ Object
Find Forex Offer is a class method that returns the foreign exchange rate for virtual currencies input: eg. ‘AMS’, ‘AMG’ output: xml with attributes eg. exchangeRate=“0.011”
36 37 38 39 |
# File 'lib/elements.rb', line 36 def self.find_forex_offer_by_id(forex_offer_id) params = {'id' => forex_offer_id } get_data("forexOffer/#{forex_offer_id}", params) end |
.find_or_create_forex_offer(exchange_rate, ipaddress) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/elements.rb', line 58 def self.find_or_create_forex_offer(exchange_rate, ipaddress) response = XmlSimple.xml_in find_active_forex_offer(exchange_rate) if(!response.nil? and !response['forexOffer'].nil?) response['forexOffer'].each do |forex_offer| if(forex_offer['externalKey'] == exchange_rate.id.to_s and forex_offer['status']== "ACTIVE") then return forex_offer["id"] else cancel_forex_offer(forex_offer["id"], ipaddress) end end end add_forex_offer(exchange_rate) #TODO Header info needs to be captured for optimization find_or_create_forex_offer(exchange_rate, ipaddress) end |
.find_purchase_order(purchase_order_id) ⇒ Object
Find purchase Order is a class method that returns the purchase order output: xml with attributes
149 150 151 152 |
# File 'lib/elements.rb', line 149 def self.find_purchase_order(purchase_order_id) params = {'id' => purchase_order_id } get_data("purchaseOrder/#{purchase_order_id}", params) end |
.find_user(user) ⇒ Object
Find_user is a class method of Api class that provides finding users that are added in elements
29 30 31 |
# File 'lib/elements.rb', line 29 def self.find_user(user) get_data "user/#{user.elements_id}" end |
.process_purchase_order(order, ipaddress = '1.1.1.1') ⇒ Object
Process purchase order is a class method that processes an already created order
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/elements.rb', line 127 def self.process_purchase_order( order, ipaddress='1.1.1.1' ) #elements_id = create_purchase_order(order, ipaddress) params = { commandType: 'CHARGE' } elements_ids = fetch_order_elements(order) elements_ids.each do |elements_id| XmlSimple.xml_in post_xml_data("purchaseOrder/#{elements_id}/command", params,"purchaseOrderCommand") end end |
.save_order_elements(elements_ids, order) ⇒ Object
Save Order Elements is a class method that saves the elements_id from order table depending on the order type
212 213 214 215 216 217 218 219 |
# File 'lib/elements.rb', line 212 def self.save_order_elements(elements_ids, order) case order.order_type when /['sell'|'buy']/ order.update_column 'elements_id', elements_ids.first when 'exchange' order.update_column 'elements_id', elements_ids.join('|') end end |
.sell_purchase_info(order, ipaddress) ⇒ Object
Sell Purchase Info is a class method that returns the sell line items and payment information for the order
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/elements.rb', line 161 def self.sell_purchase_info(order, ipaddress) line_items = Array.new line_items << [{ 'forexOffer' => { 'forexOfferRequest' => { '@amountFromCurrency' => order.from_amount.abs, '@id' => find_or_create_forex_offer(ExchangeRate.find(order.exchange_rate_id), ipaddress), '@buyerAccountId' => order.to_account.elements_id } } }] payment = [{ '@currencyId' => order.from_currency.elements_id, 'tfeAccountId' => order.from_account.elements_id }] return line_items, payment end |
.set_account_balance(account, ip_address) ⇒ Object
Add account balance is a class method that adds the account balance to the elements input: eg. Account.find(254), ‘255.255.255.255’ output
264 265 266 267 268 269 |
# File 'lib/elements.rb', line 264 def self.set_account_balance(account, ip_address) balance = account.balance.to_f - find_balance(account, ip_address).to_f command = balance >=0 ? "addToAccountBalance" : "subtractFromAccountBalance" params ={program: "es.#{command}(#{account.elements_id}, new java.math.BigDecimal(#{balance.abs}), requestContext, authInfo);''", ipAddress: ip_address} post_data('batch', params) end |