Class: SearchLogger::GoogleParser

Inherits:
Object
  • Object
show all
Defined in:
lib/search_logger/google_parser.rb,
lib/search_logger/google_parser/result.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result = false) ⇒ GoogleParser

Returns a new instance of GoogleParser.



5
6
7
8
9
10
11
# File 'lib/search_logger/google_parser.rb', line 5

def initialize(result = false)
  @result = result if result
  @start, @position_offset = 0, 1
  @rough_query, @query = "", ""
  @num = 100
  @base_url = "https://www.google.com/search?"
end

Instance Attribute Details

#numObject

Returns the value of attribute num.



3
4
5
# File 'lib/search_logger/google_parser.rb', line 3

def num
  @num
end

#position_offsetObject

Returns the value of attribute position_offset.



3
4
5
# File 'lib/search_logger/google_parser.rb', line 3

def position_offset
  @position_offset
end

#query(query) ⇒ Object

query options



15
16
17
# File 'lib/search_logger/google_parser.rb', line 15

def query
  @query
end

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/search_logger/google_parser.rb', line 3

def result
  @result
end

#rough_queryObject

Returns the value of attribute rough_query.



3
4
5
# File 'lib/search_logger/google_parser.rb', line 3

def rough_query
  @rough_query
end

#startObject

Returns the value of attribute start.



3
4
5
# File 'lib/search_logger/google_parser.rb', line 3

def start
  @start
end

Instance Method Details

#last_result(result) ⇒ Object



27
28
29
# File 'lib/search_logger/google_parser.rb', line 27

def last_result(result)
  self.tap { |s| s.position_offset = result.last.position + 1 }
end

#page(current_page) ⇒ Object



23
24
25
# File 'lib/search_logger/google_parser.rb', line 23

def page(current_page)
  self.tap { |s| s.start = ((current_page-1) * (s.num)); s.position_offset = s.start+1 }
end

#per_page(quantity) ⇒ Object



19
20
21
# File 'lib/search_logger/google_parser.rb', line 19

def per_page(quantity)
  self.tap { |s| s.num = quantity}
end

#search(result_object = Result) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/search_logger/google_parser.rb', line 31

def search(result_object = Result)
  require "nokogiri"
  Nokogiri::HTML.parse(get_response).css('li.g').each_with_object([]) do |e, all|
    all << result_object.new(e, @position_offset, @rough_query).parse
    @position_offset += 1 unless all.empty?
  end
end

#urlObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/search_logger/google_parser.rb', line 39

def url
  url = @base_url
  query_strings = []
  query_strings << "q=#{@query}" if @query
  query_strings << "num=#{num}"
  query_strings << "hl=en"
  query_strings << "start=#{@start}"
  url += query_strings.join("&")
  require "uri"
  url = URI.encode(url)
end