Class: WebSearchService
- Inherits:
-
Object
- Object
- WebSearchService
- Defined in:
- lib/generators/appchat/templates/services/web_search_service.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#initialize(args) ⇒ WebSearchService
constructor
A new instance of WebSearchService.
- #run {|"Searching Google for #{query}"| ... } ⇒ Object
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
#query ⇒ Object (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
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 |