Class: GoogleSiteSearch::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/google-site-search/search.rb

Overview

Search is responsible for parsing the returned xml from google’s API.

XML parsing is done using LibXML Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, result_class) ⇒ Search

Attributes

  • url - Expects a fully qualified url to Googles search API (can be a string or from an objects to_s method).

  • reulst_class - A class that’s initialize method is expected to handle the parsing of an individual result entry.



41
42
43
44
45
# File 'lib/google-site-search/search.rb', line 41

def initialize url, result_class
  @url = url.to_s
  @results = Array.new
  @result_class = result_class
end

Instance Attribute Details

#estimated_results_totalObject (readonly)

Pulled from the XML as the estimated total number of results. Note Google themselves say this may not be accurate.



20
21
22
# File 'lib/google-site-search/search.rb', line 20

def estimated_results_total
  @estimated_results_total
end

#next_results_urlObject (readonly)

Relative URL to get the next set of results (if any).



22
23
24
# File 'lib/google-site-search/search.rb', line 22

def next_results_url
  @next_results_url
end

#previous_results_urlObject (readonly)

Relative URL to get the previous set of results (if any).



24
25
26
# File 'lib/google-site-search/search.rb', line 24

def previous_results_url
  @previous_results_url
end

#result_classObject (readonly)

Class supplied which is responsible for parsing each individual result from the API XML.



29
30
31
# File 'lib/google-site-search/search.rb', line 29

def result_class
  @result_class
end

#resultsObject (readonly)

Array of result_class objects.



13
14
15
# File 'lib/google-site-search/search.rb', line 13

def results
  @results
end

#search_queryObject (readonly)

The full search term + filters query google interpreted from the url supplied.



32
33
34
# File 'lib/google-site-search/search.rb', line 32

def search_query
  @search_query
end

#spellingObject (readonly)

Spelling suggestion in HTML format.



15
16
17
# File 'lib/google-site-search/search.rb', line 15

def spelling
  @spelling
end

#spelling_qObject (readonly)

Spelling suggestion URL escaped.



17
18
19
# File 'lib/google-site-search/search.rb', line 17

def spelling_q
  @spelling_q
end

#urlObject (readonly)

Goolge Site Search API url.



11
12
13
# File 'lib/google-site-search/search.rb', line 11

def url
  @url
end

#xmlObject (readonly)

String of the xml returned by Google.



26
27
28
# File 'lib/google-site-search/search.rb', line 26

def xml
  @xml
end

Instance Method Details

#queryObject

Query’s Google API, stores the xml and parses values into itself.



57
58
59
60
61
# File 'lib/google-site-search/search.rb', line 57

def query
  @xml = GoogleSiteSearch::request_xml(url)
  parse_xml unless @xml.nil?
      self
end