Created
March 20, 2015 15:31
-
-
Save asvechkar/6d06c0eb323245ee1283 to your computer and use it in GitHub Desktop.
Sneakers worker example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sneakers' | |
| require 'json' | |
| require 'tubesock' | |
| class Processor | |
| include Sneakers::Worker | |
| include Tubesock::Hijack | |
| from_queue 'receive_transactions_queue', | |
| durable: true, | |
| arguments: { | |
| :'x-message-ttl' => 1800000 | |
| }, | |
| threads: 50, | |
| prefetch: 50, | |
| exchange: 'receive_transactions_queue' | |
| Sneakers.configure {} | |
| Sneakers.logger.level = Logger::ERROR | |
| Sneakers::Worker.configure_logger(Logger.new('/dev/null')) | |
| def work(msg) | |
| puts msg | |
| trans_message = JSON.parse(msg) | |
| puts trans_message['id'] | |
| @DATA = { | |
| 'apiSign' => ENV['API_SIGN'], | |
| 'transactionId' => trans_message['id'] | |
| }.to_json | |
| puts @DATA | |
| uri = URI.parse(ENV['TRANS_API_URL'] | |
| https = Net::HTTP.new(uri.host, uri.port) | |
| https.use_ssl = true | |
| https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'application/json'}) | |
| request.set_form_data({'DATA' => @DATA}) | |
| response = https.request(request) | |
| # puts "Response #{response.code} #{response.message}: #{response.body}" | |
| res_trans = JSON.parse(response.body) | |
| # puts res_trans | |
| if !res_trans['ErrCode'] | |
| puts res_trans[0]['user_id'] | |
| @DATA = { | |
| 'apiSign' => ENV['API_SIGN'], | |
| 'clientId' => res_trans[0]['user_id'] | |
| }.to_json | |
| puts @DATA | |
| uri = URI.parse(ENV['CLIENTS_API_URL'] | |
| https = Net::HTTP.new(uri.host, uri.port) | |
| https.use_ssl = true | |
| https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'application/json'}) | |
| request.set_form_data({'DATA' => @DATA}) | |
| response = https.request(request) | |
| res_client = JSON.parse(response.body) | |
| puts res_client | |
| if !res_client['ErrCode'] | |
| Client.find_or_create_by!({id: res_client['id']}) do |client| | |
| client.name = res_client['firmName'] | |
| client.fullname = res_client['clientFullname'] | |
| client.phone = res_client['clientPhone'] | |
| client.email = res_client['clientEmail'] | |
| client.inn = res_client['clientInn'] | |
| client.ogrn = res_client['clientOgrn'] | |
| client.account = res_client['clientAccountNum'] | |
| client.bank = res_client['bankName'] | |
| client.bik = res_client['bankBik'] | |
| client.status = res_client['statusName'] | |
| client.city = res_client['clientCity'] | |
| client.zipcode = res_client['clientZipcode'] | |
| client.activated_at = res_client['statusDate'] | |
| client.save! | |
| puts "client_id: #{client.id}" | |
| end | |
| Transaction.find_or_create_by!({transactnum: res_trans[0]['transactnum'], transtype: res_trans[0]['type'], proc_code: res_trans[0]['proc_code']}) do |trans| | |
| trans.transactdate = res_trans[0]['transactdate']['date'] | |
| trans.amount = res_trans[0]['amount'] | |
| trans.merchant_id = res_trans[0]['merchant_id'] | |
| trans.client_id = res_trans[0]['user_id'] | |
| trans.card_num = res_trans[0]['card_num'] | |
| trans.reader_num = res_trans[0]['reader_num'] | |
| trans.transtype = res_trans[0]['type'] | |
| trans.brand = res_trans[0]['brand'] | |
| trans.latitude = res_trans[0]['lat'] | |
| trans.longitude = res_trans[0]['lng'] | |
| trans.termname = res_trans[0]['termname'] | |
| trans.proc_code = res_trans[0]['pcode'] | |
| trans.save! | |
| puts "trans_id: #{trans.id}" | |
| end | |
| else | |
| puts res_client['ErrCode'] | |
| end | |
| else | |
| puts res_trans['ErrCode'] | |
| end | |
| ack! | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment