Module: Google::SafeBrowsingUpdateHelper
- Included in:
- SafeBrowsingClient
- Defined in:
- lib/google/safe_browsing_update_helper.rb
Instance Method Summary collapse
-
#gen_chunk_nums_string(chunk_id_arr) ⇒ Object
chunk_id_arr: [113121, 113122, 113123, 113132], in increasing order return: “113121-113123,113132”.
- #safe_browsing_service ⇒ Object
- #save_redirect_urls(urls, list_obj) ⇒ Object
- #update_add_shavar_chunk(adds, shavar_list) ⇒ Object
- #update_add_sub_chunks(list_obj, del_chunk_ids, chunk_type) ⇒ Object
- #update_local_shavar_info(update_obj) ⇒ Object
- #update_next_update_time(link_function, ts) ⇒ Object
- #update_redirect_urls(url, list_obj, attributes) ⇒ Object
- #update_shavar_chunk(adds, subs, shavar_list) ⇒ Object
- #update_sub_shavar_chunk(subs, shavar_list) ⇒ Object
Instance Method Details
#gen_chunk_nums_string(chunk_id_arr) ⇒ Object
chunk_id_arr: [113121, 113122, 113123, 113132], in increasing order return: “113121-113123,113132”
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/google/safe_browsing_update_helper.rb', line 114 def gen_chunk_nums_string chunk_id_arr ranges_and_integers = [] first = last = chunk_id_arr.shift return "" if first.nil? increment = 0 while !(int = chunk_id_arr.shift).nil? increment += 1 if int == first + increment last = int next else if first == last ranges_and_integers << first else ranges_and_integers << (first..last) end first = last = int increment = 0 end end if first == last ranges_and_integers << first else ranges_and_integers << (first..last) end range_and_int_arr_to_string(ranges_and_integers) end |
#safe_browsing_service ⇒ Object
147 148 149 |
# File 'lib/google/safe_browsing_update_helper.rb', line 147 def safe_browsing_service Function.where(name: Function::GoogleSafeBrowsing).first end |
#save_redirect_urls(urls, list_obj) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/google/safe_browsing_update_helper.rb', line 28 def save_redirect_urls urls, list_obj urls.each do |url| obj = SafeBrowsingRedirectUrl.for_url_and_list_id(url, list_obj.id).first if obj.nil? obj = SafeBrowsingRedirectUrl.create(url: url, google_safe_browsing_list_id: list_obj.id) end end end |
#update_add_shavar_chunk(adds, shavar_list) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/google/safe_browsing_update_helper.rb', line 60 def update_add_shavar_chunk adds, shavar_list adds.each do |add| chunk_num = add[:chunk_num] add[:chunk_data].each do |host_key, prefixes| if prefixes.empty? shavar = SafeBrowsingShavar.where( google_safe_browsing_list_id: shavar_list.id, chunk_type: SafeBrowsingShavar::CHUNK_TYPE_ADD, chunk_num: chunk_num, host_key: host_key, prefix: nil).first_or_create else prefixes.each do |prefix| shavar = SafeBrowsingShavar.where( google_safe_browsing_list_id: shavar_list.id, chunk_type: SafeBrowsingShavar::CHUNK_TYPE_ADD, chunk_num: chunk_num, host_key: host_key, prefix: prefix).first_or_create end end end end end |
#update_add_sub_chunks(list_obj, del_chunk_ids, chunk_type) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/google/safe_browsing_update_helper.rb', line 42 def update_add_sub_chunks list_obj, del_chunk_ids, chunk_type del_chunk_ids.each do |chunk_id| if chunk_id.is_a?(Range) chunk_id.each do |id| SafeBrowsingShavar.where(google_safe_browsing_list_id: list_obj.id, chunk_num: id, chunk_type: chunk_type).destroy_all end else SafeBrowsingShavar.where(google_safe_browsing_list_id: list_obj.id, chunk_num: chunk_id, chunk_type: chunk_type).destroy_all end end end |
#update_local_shavar_info(update_obj) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/google/safe_browsing_update_helper.rb', line 4 def update_local_shavar_info update_obj gsb = safe_browsing_service update_next_update_time(gsb, update_obj.next) if update_obj.next if update_obj.reset if Rails.configuration.respond_to?(:google_safe_browsing_upon_reset) Rails.configuration.google_safe_browsing_upon_reset.call else Rails.logger.warn "I got a reset from Google... Don't know what to do." end end if update_obj.has_lists? update_obj.lists.keys.each do |name| if SafeBrowsingList.valid_list?(name) list = SafeBrowsingList.find_by_name(name.to_s) update_add_sub_chunks list, update_obj.get_ad_chunk_ids(name), SafeBrowsingShavar::CHUNK_TYPE_ADD update_add_sub_chunks list, update_obj.get_sd_chunk_ids(name), SafeBrowsingShavar::CHUNK_TYPE_SUB else Rails.logger.info "Got invalid list name [#{name}]" end end end end |
#update_next_update_time(link_function, ts) ⇒ Object
151 152 153 |
# File 'lib/google/safe_browsing_update_helper.rb', line 151 def update_next_update_time link_function, ts link_function.update_attributes(:next_updated_at => Time.now + ts) end |
#update_redirect_urls(url, list_obj, attributes) ⇒ Object
37 38 39 40 |
# File 'lib/google/safe_browsing_update_helper.rb', line 37 def update_redirect_urls url, list_obj, attributes obj = SafeBrowsingRedirectUrl.for_url_and_list_id(url, list_obj.id).first obj.update_attributes(attributes) end |
#update_shavar_chunk(adds, subs, shavar_list) ⇒ Object
55 56 57 58 |
# File 'lib/google/safe_browsing_update_helper.rb', line 55 def update_shavar_chunk adds, subs, shavar_list update_add_shavar_chunk adds, shavar_list update_sub_shavar_chunk subs, shavar_list end |
#update_sub_shavar_chunk(subs, shavar_list) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/google/safe_browsing_update_helper.rb', line 83 def update_sub_shavar_chunk subs, shavar_list subs.each do |sub| chunk_num = sub[:chunk_num] sub[:chunk_data].each do |host_key, chunk_num_hash_prefix| chunk_num_hash_prefix.each do |add_chunk_num, prefixes| if prefixes.empty? shavar = SafeBrowsingShavar.where( google_safe_browsing_list_id: shavar_list.id, chunk_type: SafeBrowsingShavar::CHUNK_TYPE_SUB, chunk_num: chunk_num, host_key: host_key, add_chunk_num: add_chunk_num, prefix: nil).first_or_create else prefixes.each do |prefix| shavar = SafeBrowsingShavar.where( google_safe_browsing_list_id: shavar_list.id, chunk_type: SafeBrowsingShavar::CHUNK_TYPE_SUB, chunk_num: chunk_num, host_key: host_key, add_chunk_num: add_chunk_num, prefix: prefix).first_or_create end end end # chunk_num_hash_prefix.each end # subs[:chunk_data].each end # subs.each end |