Class: Chen::Google

Inherits:
Google
  • Object
show all
Defined in:
lib/chen.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.query(str, page_number = 1, size = 8) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chen.rb', line 31

def query(str, page_number = 1, size = 8)
  options = {
    page: page_number,
    size: size,
    readability: true,
    markdown: true
  }
  
  query_object = Chen::Google.new(str,options)
  
  return query_object.to_results
end

.query_to_json(str, page_number = 1, size = 10) ⇒ Object



44
45
46
47
48
# File 'lib/chen.rb', line 44

def query_to_json(str, page_number = 1, size = 10)
  results = query(str, page_number, size)
  doc = ERB.new(Chen::Template)
  return doc.result(binding)
end

Instance Method Details

#to_resultsObject

copied mostly from the “show” method



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chen.rb', line 52

def to_results
  info = request :q => @query, :rsz => @opts[:size], :start => ((@opts[:page] - 1) * @opts[:size])
  results           = info[:results]
  query_strings     = info[:query_strings]
  coder             = HTMLEntities.new
  current_page      = results['responseData']['cursor']['currentPageIndex']+1
  max_result        = query_strings[:start] + query_strings[:rsz]
  estimated_results = results['responseData']['cursor']['resultCount']
  result_array      = results['responseData']['results']
  
  datas = []
  result_array.each_with_index do |result, i|
    new_data = Chen::SearchResult.new
    new_data.tap do |d|
      d.title = result["titleNoFormatting"]
      d.url = result["url"]
      d.desc = result["content"].squeeze(" ")
      d.number = (i + query_strings[:start] + 1)
      d.estimated = estimated_results
    end
    datas << new_data
  end
  return datas
end