Class: SafeBrowsingLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/google-safe-browsing-lookup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = '', debug = false, error = false) ⇒ SafeBrowsingLookup

New client

key

API key

debug

Set to true to print debug & error output to the standard output. false (disabled) by default.

error

Set to true to print error output to the standard output. false (disabled) by default.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/google-safe-browsing-lookup.rb', line 33

def initialize(key='', debug=false, error=false)
	@key = key || ''
	@debug = debug || false
	@error = error || false
	@last_error = ''

	@version = '0.2'
	@api_version = '3.1'
	

	raise ArgumentError, "Missing API key" if (@key == '')
end

Instance Attribute Details

#api_versionObject (readonly)

Google API version



26
27
28
# File 'lib/google-safe-browsing-lookup.rb', line 26

def api_version
  @api_version
end

#keyObject (readonly)

API key



16
17
18
# File 'lib/google-safe-browsing-lookup.rb', line 16

def key
  @key
end

#last_errorObject (readonly)

Contain last error



22
23
24
# File 'lib/google-safe-browsing-lookup.rb', line 22

def last_error
  @last_error
end

#versionObject (readonly)

Library version



24
25
26
# File 'lib/google-safe-browsing-lookup.rb', line 24

def version
  @version
end

Instance Method Details

#lookup(urls = '') ⇒ Object

Lookup a list of URLs against the Google Safe Browsing v2 lists.

Returns a hash <url>: <Gooogle match>. The possible values for <Gooogle match> are: “ok” (no match), “malware”, “phishing”, “malware,phishing” (match both lists) and “error”.

urls

List of URLs to lookup. The Lookup API allows only 10,000 URL checks a day. If you need more, find a Ruby implementation of the full Google Safe Browsing v2 API. Each requests must contain 500 URLs at most. The lookup() method will split the list of URLS in blocks of 500 URLs if needed.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
109
110
# File 'lib/google-safe-browsing-lookup.rb', line 51

def lookup(urls='')
	if (urls.respond_to?('each') == false)
		urls = Array.new(1, urls)
	end
# 		urls_copy = Array.new(urls)

	results = { }

# 		while (urls_copy.length > 0)
# 			inputs = urls_copy.slice!(0, 500)
	count = 0
	while (count * 500 < urls.length)
		inputs = urls.slice(count * 500, 500)
		body = inputs.length.to_s
		inputs.each do |url|
			body = body + "\n" + canonical(url)
		end

		debug("BODY:\n#{body}\n\n")
		uri = URI.parse("https://sb-ssl.google.com/safebrowsing/api/lookup?client=ruby&key=#{@key}&appver=#{@version}&pver=#{@api_version}")
		
		http = Net::HTTP.new(uri.host, uri.port)
		http.open_timeout = 30
		http.read_timeout = 30
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE

		response = http.request_post("#{uri.path}?#{uri.query}", body)

		case response
			when Net::HTTPOK # 200
				debug("At least 1 match\n")
				results.merge!( parse(inputs, response.body) )

			when Net::HTTPNoContent # 204
				debug("No match\n")
				results.merge!( ok(inputs) )

			when Net::HTTPBadRequest # 400
				error("Invalid request")
				results.merge( errors(inputs) )

			when Net::HTTPUnauthorized # 401
				error("Invalid API key")
				results.merge!( errors(inputs) )

			when Net::HTTPServiceUnavailable # 503
				error("Server error, client may have sent too many requests")
				results.merge!( errors(inputs) )

			else
				self.error("Unexpected server response: #{response.code}")
				results.merge!( errors(inputs) )
		end

	count = count + 1
	end

	return results
end