Class: WebSearchService

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/appchat/templates/services/web_search_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ WebSearchService

Returns a new instance of WebSearchService.



6
7
8
# File 'lib/generators/appchat/templates/services/web_search_service.rb', line 6

def initialize(args)
  @query = args["query"]
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/generators/appchat/templates/services/web_search_service.rb', line 5

def query
  @query
end

Instance Method Details

#run {|"Searching Google for #{query}"| ... } ⇒ Object

Yields:

  • ("Searching Google for #{query}")


10
11
12
13
14
15
16
# File 'lib/generators/appchat/templates/services/web_search_service.rb', line 10

def run
  yield("Searching Google for #{query}")

  doc = Nokogiri::HTML(URI.open("https://www.google.com/search?q=#{CGI.escape(query)}").read.force_encoding('UTF-8'))
  doc.css('script, style, noscript, comment').remove
  doc.css('h1, h2, h3, h4, h5, h6, p').map(&:text).join("\n").strip
end