Class: GSA::Searcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gsa/searcher.rb

Constant Summary collapse

FIELDS =
[
  :tlen, :proxyreload, :proxystylesheet, :emmain, :emsingleres, :emdstyle, :dnavs, :entqr, :entsp,
  :query, :filter, :getfields, :sort, :num, :output, :start, :client, :site, :requiredfields
]

Class Method Summary collapse

Class Method Details

.clean_query(query) ⇒ Object



32
33
34
# File 'lib/gsa/searcher.rb', line 32

def self.clean_query(query)
  query.tr(' ', '+')
end

.parsed_json(xml) ⇒ Object



36
37
38
# File 'lib/gsa/searcher.rb', line 36

def self.parsed_json(xml)
  Hash.from_xml(xml)
end

.results_found?(search_results) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/gsa/searcher.rb', line 40

def self.results_found?(search_results)
  search_results[GSA::GOOGLE_SEARCH_PROTOCOL].has_key? GSA::RESULTS
end

.search(query, args = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gsa/searcher.rb', line 8

def self.search(query, args = {})
  query           = clean_query(query)
  args[:site]            = args[:collection] || GSA::DEFAULT_COLLECTION
  args[:requiredfields]  = args[:filters] || nil

  search = "#{GSA.base_uri}/search?q=#{query}"

  FIELDS.each do |field|
    begin
      value = args[field] || GSA.send("DEFAULT_#{field.capitalize}")
      search = "#{search}&#{field.to_s}=#{value}"
    rescue NoMethodError
    end
  end

  puts "URL = #{search}"

  results = RestClient.get(search)
  return results if args[:embedded]

  search_results = parsed_json(results)
  results_found?(search_results) ? search_results : GSA::NO_RESULTS
end