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
# 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.select('distinct chunk_number').where(:list => list).
      order(:chunk_number).collect{|c| c.chunk_number }
    action_strings << "a:#{squish_number_list(nums)}" if nums.any?

    nums = GoogleSafeBrowsing::SubShavar.select('distinct chunk_number').where(:list => list).
      order(:chunk_number).uniq.collect{|c| c.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

.chunklist_to_sql(chunk_list) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/google_safe_browsing/chunk_helper.rb', line 55

def self.chunklist_to_sql(chunk_list)
  ret_array = []
  chunk_list.split(',').each do |s|
    if s.index('-')
      range = s.split('-')
      ret_array << "chunk_number between #{range[0]} and #{range[1]}"
    else
      ret_array << "chunk_number = #{s}"
    end
  end
  ret_array.join(" or ")
end

.squish_number_list(chunks) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/google_safe_browsing/chunk_helper.rb', line 30

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