1. Ruby / Говнокод #22524

    −100

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    def check_web_property(file_folder_id)
        folder_for_updated_files = FileFolder.find file_folder_id
        # min_to_be_active = folder_for_updated_files.min_properties_to_be_active
        process_uniq_id = SecureRandom.urlsafe_base64(30)
        min_web_property = folder_for_updated_files.min_web_property_check
        max_web_property = folder_for_updated_files.max_web_property_check
        #puts "update ad_groups with given inventory limit to Previus Inventory = nil "
        self.ad_groups.and( { "Inventory" => {'$gte' => folder_for_updated_files.min_web_property_check}}, {"Inventory" =>{'$lte' => folder_for_updated_files.max_web_property_check}}).update_all( "Previus Inventory" => nil, :update_complited => "no")
        #PpcBuild.last.ad_groups.and( { "Previus Inventory" =>{'$gte' => 1}}, {"Previus Inventory" =>{'$lte' => 8 } }).count
    
        puts "select ad_groups with given inventory limit"
        selrcted_ad_groups = self.ad_groups.and( { "Inventory" =>{'$gte' => folder_for_updated_files.min_web_property_check}}, {"Inventory" =>{'$lte' => folder_for_updated_files.max_web_property_check } })
    
        count_selected_ad_groups_for_processing = selrcted_ad_groups.count
    
        selrcted_ad_groups.no_timeout.each do |ad|
          if !self.uniq_urls.where("Landing Page"=> ad["Landing Page"], process_id: process_uniq_id).exists?
            new_url = UniqUrl.new("Landing Page"=> ad["Landing Page"], process_id: process_uniq_id, ppc_build_id: self.id)
            new_url.save
          end
        end
        all_uniq_url_selected = self.uniq_urls.where(process_id: process_uniq_id)
        elemetns_count = 0
        all_uniq_url_selected.no_timeout.each do |uniq_url|
          ad = AdGroup.find_by("Landing Page" => uniq_url["Landing Page"], ppc_build_id: self.id)
          ad_parameters = { "Landing Page" => uniq_url["Landing Page"], ppc_build_id: self.id.to_s }
    
          UpdateDataFromWebSite.perform_async(ad_parameters, process_uniq_id)
    
          min_to_be_active = ad.min_for_ad_group_to_be_active
          elemetns_count +=1
    
          ad.web_update_inventory(ad.id.to_s, process_uniq_id, min_to_be_active)
          if elemetns_count == 20
            sleep 0
            elemetns_count = 0
          end
          ad.update_attributes("Previus Inventory" => ad["Inventory"])
          ad.push(process_id: process_id)
          url = ad["Landing Page"]
          uri = URI(url)
          use_ssl = true
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = use_ssl
          http.read_timeout = 20
          http.open_timeout = 20
    
          resp = http.start() { |http| http.get(uri.path) }
    
          body = resp.body
          page = Nokogiri::HTML(body)
          if !page.css('.results-count').empty?
          new_property = page.css('.results-count').first.text
          property_number = new_property[/(?<=of\s)?\d{1,}\D?\d?{1,}/]
    
          if property_number
          ad.update_attributes("Inventory" => new_property.gsub(/\s+/, ' ')[/(?<=of\s)?\d{1,}\D?\d?{1,}/].strip)
    
          else
          ad.update_attributes("Inventory" => "0")
          end
          else
          ad.update_attributes("Inventory" => "0")
          end
          ad.update_attributes("Status" => get_status(ad["Inventory"], min_to_be_active), update_complited: "yes")
        end

    Пример метода из ruby класса содержащего 890 строк

    Запостил: gururuby, 09 Марта 2017

    Комментарии (12) RSS

    Добавить комментарий