Class: GoogleSafeBrowsing::ChunkHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/google_safe_browsing/chunk_helper.rb

Class Method Summary collapse

Class Method Details

.build_chunk_list(*lists) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_safe_browsing/chunk_helper.rb', line 3

def self.build_chunk_list(*lists)
  lists = if lists.any?
            lists.to_a
          else
            GoogleSafeBrowsing.config.current_lists
          end

  ret = ''
  lists.each do |list|
    ret += "#{list};"
    action_strings = []


    nums = GoogleSafeBrowsing::AddShavar.where(list: list)
                                        .order(:chunk_number)
                                        .select('distinct chunk_number')
                                        .map(&:chunk_number)
    action_strings << "a:#{squish_number_list(nums)}" if nums.any?

    nums = GoogleSafeBrowsing::SubShavar.where(list: list)
                                        .order(:chunk_number)
                                        .select('distinct chunk_number')
                                        .map(&:chunk_number)
    action_strings << "s:#{squish_number_list(nums)}" if nums.any?

    ret += "#{action_strings.join(':')}#{":mac" if GoogleSafeBrowsing.config.have_keys?}\n"
  end

  ret
end

.squish_number_list(chunks) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/google_safe_browsing/chunk_helper.rb', line 34

def self.squish_number_list(chunks)
  num_strings = []
  streak_begin = last_num = chunks.shift

  chunks.each do |c|
    unless c == last_num+1
      if streak_begin != last_num
        num_strings << "#{streak_begin}-#{last_num}"
      else
        num_strings << last_num
      end
      streak_begin = c
    end
    last_num = c
  end

  if streak_begin == chunks[-1]
    num_strings << streak_begin
  else
    num_strings << "#{streak_begin}-#{chunks[-1]}"
  end

  num_strings.join(',')
end