Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `_query': This connection is in use by: #<Thread:0x00007f9c73973eb8@thread.rb:62 sleep> (Mysql2::Error)
- require 'firebase'
- require 'conekta'
- require 'json'
- require 'savon'
- require "crack"
- require 'active_support/core_ext/hash' #from_xml
- require 'nokogiri'
- require 'xmlsimple'
- require 'mysql2'
- class Cron
- def generate_activation_code(size = 10)
- charset = %w{ 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
- (0...size).map{ charset.to_a[rand(charset.size)] }.join
- end
- def construct()
- base_uri = 'FIREBASE_URL'
- file = File.open("FIREBASE_CREDENTIALS", "rb")
- firebase = Firebase::Client.new(base_uri, file.read)
- Conekta.locale = :es
- Conekta.api_key = 'MY_KEY'
- @response = firebase.get('users', nil)
- @client = Savon.client(wsdl: 'MY_URL', ntlm: ["user", "pass"] , :convert_request_keys_to => :camelcase )
- @client_mysql = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "", :database => "masaldo_api")
- end
- def get_comision()
- last_validity = @client_mysql.query("SELECT comision * 100 as comision FROM configuration")
- last_validity.each do |validityr|
- @comision = validityr["comision"]
- end
- end
- def create_transaction(sku, token, phone, userid, card)
- validity = @client_mysql.query("SELECT precio * 100 as precio_total, vigencia, descripcion, precio as precio_base FROM bluesoft_services_validity WHERE sku='#{sku}'")
- validity.each do |row|
- @vigencia = row["vigencia"]
- @descipcion = row["descripcion"]
- @precio = row["precio_total"]
- @precio_base = row["precio_base"].to_i
- end
- if @vigencia.to_i > 0
- last_current = @client_mysql.query("SELECT * FROM transactions WHERE number='#{phone}' ORDER BY trandate DESC LIMIT 1")
- last_current.each do |last|
- @trandate = last["trandate"]
- @trandate_result = @trandate.strftime("%Y%m%d %H:%M:%S")
- end
- end
- @last_with_validty = (@trandate + (@vigencia).to_i.day).strftime("%Y-%m-%d")
- @today = (Time.now).strftime("%Y-%m-%d")
- if @last_with_validty == @today
- puts phone
- end
- end
- def init()
- threads = []
- hash = @response.body
- hash.each do |token , user|
- threads << Thread.new do
- #Check if user is current for transaction if not need to check agenda
- if user['is_current']
- self.create_transaction(user['sku'], token, user['phoneNumber'], user['customer_id'], user['fav_card'])
- user['addressBook'].each do |userid , user_address_book|
- if user['is_current']
- replacements = { '+521' => '' }
- phone_number = user_address_book['phoneNumber'].gsub(Regexp.union(replacements.keys), replacements)
- self.create_transaction(user_address_book['sku'], token, phone_number, user['customer_id'], user_address_book['fav_card'])
- end
- end
- end
- end
- end
- threads.each { |t| t.join }
- end
- end
- classCron = Cron.new()
- classCron.construct()
- classCron.get_comision()
- classCron.init()
Add Comment
Please, Sign In to add comment