Module: GoogleBase

Includes:
HTTParty
Defined in:
lib/allyourbase.rb

Defined Under Namespace

Classes: Item, SearchResponse

Constant Summary collapse

GOOGLENS =
"http://base.google.com/ns/1.0"
OPENSEARCHNS =
"http://a9.com/-/spec/opensearchrss/1.0/"

Class Method Summary collapse

Class Method Details

.find(terms = [], options = {}) ⇒ Object

Finds Items in Google Base using the “snippets” Atom feed.

Options:

  • :limit => <Max number of results to return>

  • :offset => <0-based, starting offset of result set>

All other options are passed as structured terms (e.g. part of the ‘bq’ param).

**Options param will be mutated



26
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
# File 'lib/allyourbase.rb', line 26

def self.find(terms = [],options = {})
  #Max Number of results
  limit  = options.delete(:limit) || 10
  #Starting offset (zero based)
  offset = (options.delete(:offset) || 0) + 1
  
  bq = structure(options)
  
  q  = terms.collect { |t|
    t = "\"#{t}\"" if(t =~ /[^\w\d\-\.\_]/)
    t
  }.join(' ')

  atom = get('',:query => {:q => q, :bq => bq, :'max-results' => limit, :'start-index' => offset})
  
  res = SearchResponse.new({
    :terms   => terms,
    :limit   => limit,
    :offset  => atom[OPENSEARCHNS,'startIndex'].first.to_i - 1,
    :count   => atom[OPENSEARCHNS,'totalResults'].first.to_i,
    :options => options
  })
  res.results = atom.entries.collect { |e|
    Item.new(e)
  }
  res
end