Guest User

Untitled

a guest
Jan 23rd, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. `_query': This connection is in use by: #<Thread:0x00007f9c73973eb8@thread.rb:62 sleep> (Mysql2::Error)
  2.  
  3. require 'firebase'
  4. require 'conekta'
  5. require 'json'
  6. require 'savon'
  7. require "crack"
  8. require 'active_support/core_ext/hash' #from_xml
  9. require 'nokogiri'
  10. require 'xmlsimple'
  11. require 'mysql2'
  12.  
  13. class Cron
  14.  
  15. def generate_activation_code(size = 10)
  16. 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}
  17. (0...size).map{ charset.to_a[rand(charset.size)] }.join
  18. end
  19.  
  20. def construct()
  21. base_uri = 'FIREBASE_URL'
  22. file = File.open("FIREBASE_CREDENTIALS", "rb")
  23. firebase = Firebase::Client.new(base_uri, file.read)
  24. Conekta.locale = :es
  25. Conekta.api_key = 'MY_KEY'
  26. @response = firebase.get('users', nil)
  27. @client = Savon.client(wsdl: 'MY_URL', ntlm: ["user", "pass"] , :convert_request_keys_to => :camelcase )
  28. @client_mysql = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "", :database => "masaldo_api")
  29. end
  30.  
  31. def get_comision()
  32. last_validity = @client_mysql.query("SELECT comision * 100 as comision FROM configuration")
  33. last_validity.each do |validityr|
  34. @comision = validityr["comision"]
  35. end
  36. end
  37.  
  38. def create_transaction(sku, token, phone, userid, card)
  39. validity = @client_mysql.query("SELECT precio * 100 as precio_total, vigencia, descripcion, precio as precio_base FROM bluesoft_services_validity WHERE sku='#{sku}'")
  40. validity.each do |row|
  41. @vigencia = row["vigencia"]
  42. @descipcion = row["descripcion"]
  43. @precio = row["precio_total"]
  44. @precio_base = row["precio_base"].to_i
  45. end
  46. if @vigencia.to_i > 0
  47. last_current = @client_mysql.query("SELECT * FROM transactions WHERE number='#{phone}' ORDER BY trandate DESC LIMIT 1")
  48. last_current.each do |last|
  49. @trandate = last["trandate"]
  50. @trandate_result = @trandate.strftime("%Y%m%d %H:%M:%S")
  51. end
  52. end
  53. @last_with_validty = (@trandate + (@vigencia).to_i.day).strftime("%Y-%m-%d")
  54. @today = (Time.now).strftime("%Y-%m-%d")
  55. if @last_with_validty == @today
  56. puts phone
  57. end
  58. end
  59.  
  60. def init()
  61. threads = []
  62. hash = @response.body
  63. hash.each do |token , user|
  64. threads << Thread.new do
  65. #Check if user is current for transaction if not need to check agenda
  66. if user['is_current']
  67. self.create_transaction(user['sku'], token, user['phoneNumber'], user['customer_id'], user['fav_card'])
  68. user['addressBook'].each do |userid , user_address_book|
  69. if user['is_current']
  70. replacements = { '+521' => '' }
  71. phone_number = user_address_book['phoneNumber'].gsub(Regexp.union(replacements.keys), replacements)
  72. self.create_transaction(user_address_book['sku'], token, phone_number, user['customer_id'], user_address_book['fav_card'])
  73. end
  74. end
  75. end
  76. end
  77. end
  78. threads.each { |t| t.join }
  79. end
  80. end
  81.  
  82. classCron = Cron.new()
  83. classCron.construct()
  84. classCron.get_comision()
  85. classCron.init()
Add Comment
Please, Sign In to add comment