Class: GoogleSafeBrowsing::HttpHelper

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.encoded_paramsObject



8
9
10
11
12
13
# File 'lib/google_safe_browsing/http_helper.rb', line 8

def self.encoded_params
  "?client=#{GoogleSafeBrowsing.config.client}" +
  "&apikey=#{GoogleSafeBrowsing.config.api_key}" +
  "&appver=#{GoogleSafeBrowsing.config.app_ver}" +
  "&pver=#{GoogleSafeBrowsing.config.p_ver}"
end

.get_data(list = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/google_safe_browsing/http_helper.rb', line 33

def self.get_data(list=nil)
  # Get (via Post) List Data
  uri = uri_builder('downloads')
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = ChunkHelper.build_chunk_list(list)

  Net::HTTP.start(uri.host) { |http| http.request request }
end

.request_full_hashes(hash_array) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google_safe_browsing/http_helper.rb', line 15

def self.request_full_hashes(hash_array)
  uri = uri_builder('gethash')
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = "4:#{hash_array.length * 4}\n"
  hash_array.each do |h|
    request.body << BinaryHelper.hex_to_bin(h[0..7])
  end

  response = Net::HTTP.start(uri.host) { |http| http.request request }

  if response.code == 200
    ResponseHelper.parse_full_hash_response(response.body)
  else
    # if response not good, return empty array to represent no full hashes
    []
  end
end

.uri_builder(action) ⇒ Object



3
4
5
6
# File 'lib/google_safe_browsing/http_helper.rb', line 3

def self.uri_builder(action)
  uri = URI("#{GoogleSafeBrowsing.config.host}/#{action}#{encoded_params}")
  uri
end

Instance Method Details

#get_listsObject



42
43
44
45
# File 'lib/google_safe_browsing/http_helper.rb', line 42

def get_lists
  uri = uri_builder('list')
  lists = Net::HTTP.get(uri).split("\n")
end