Class: GoogleSafeBrowsing::APIv2

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

Overview

Main Interface for Module

Class Method Summary collapse

Class Method Details

.delay(delay_seconds) ⇒ Object

Can be used to force a delay into a script running updates

Parameters:

  • delay_seconds (Integer)

    the number of seconds to delay, should be the return value of update



63
64
65
66
67
68
69
70
71
72
# File 'lib/google_safe_browsing/api_v2.rb', line 63

def self.delay(delay_seconds)
  puts "Google told us to wait for #{delay_seconds} seconds"
  puts "We will wait...."
  start_time = Time.now
  while(start_time + delay_seconds > Time.now)
      puts "#{(delay_seconds - (Time.now - start_time)).to_i}..."
      sleep(10)
  end
  puts "Thank you for being patient"
end

.lookup(url) ⇒ String?

Performs a lookup of the given url

Parameters:

  • url (String)

    a url string to be looked up

Returns:

  • (String, nil)

    the friendly list name if found, or nil



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/google_safe_browsing/api_v2.rb', line 27

def self.lookup(url)
  urls = Canonicalize.urls_for_lookup(url.force_encoding('ASCII-8BIT'))
  return nil if urls.empty?

  hashes = HashHelper.urls_to_hashes(urls)
  raw_hash_array = hashes.collect{ |h| h.to_s }

  if full = FullHash.where(:full_hash => raw_hash_array).first
    return GoogleSafeBrowsing.friendly_list_name(full.list)
  end

  hits =  AddShavar.where(:prefix => hashes.map{|h| h.prefix}).collect{ |s| [ s.list, s.prefix ] }
  safes = SubShavar.where(:prefix => hashes.map{|h| h.prefix}).collect{ |s| [ s.list, s.prefix ] }

  reals = hits - safes

  if reals.any?
    full_hashes = HttpHelper.request_full_hashes(reals.collect{|r| r[1] })

    # save hashes first
    # cannot return early because all FullHashes need to be saved
    hit_list = nil
    full_hashes.each do |hash|
      FullHash.create!(:list => hash[:list], :add_chunk_number => hash[:add_chunk_num],
                                   :full_hash => hash[:full_hash])

      hit_list = hash[:list] if raw_hash_array.include?(hash[:full_hash])
    end
    return GoogleSafeBrowsing.friendly_list_name(hit_list)
  end
  nil
end

.updateInteger

Completes an update

Returns:

  • (Integer)

    the number of seconds before this method should be called again



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/google_safe_browsing/api_v2.rb', line 7

def self.update
  HttpHelper.get_keys unless GoogleSafeBrowsing.config.have_keys?

  data_response = HttpHelper.get_data

  to_do_array = ResponseHelper.parse_data_response(data_response.body)

  to_do_array[:lists].each do |list|
    to_do_array[:data_urls][list].each do |url|
      puts "#{list} - #{url}\n"
      ResponseHelper.receive_data('http://' + url, list)
    end
  end
  to_do_array[:delay_seconds]
end