Class: Ubi::Consultor::Base
- Inherits:
-
Object
- Object
- Ubi::Consultor::Base
- Defined in:
- lib/ubi/consultor.rb
Overview
Base for araneas (spiders)
Direct Known Subclasses
Bing, DuckDuckGo, Facebook, Foursquare, Google, Twitter, Wikipedia, Yahoo
Constant Summary collapse
- HEADERS =
{ 'User-Agent' => "Ubi v#{Ubi::VERSION}" }
Class Method Summary collapse
- .inherited(base) ⇒ Object
-
.name ⇒ Object
Human-readable name of the aranea.
-
.url ⇒ Object
Url to query.
Instance Method Summary collapse
- #datum ⇒ Object
-
#initialize(thema) ⇒ Base
constructor
A new instance of Base.
- #links ⇒ Object
- #parser(chunk) ⇒ Object
- #query_url ⇒ Object
-
#request(opts = {}) ⇒ Object
Make an HTTP(S) request to a geocoding API and return the response object.
Constructor Details
#initialize(thema) ⇒ Base
Returns a new instance of Base.
7 8 9 |
# File 'lib/ubi/consultor.rb', line 7 def initialize(thema) @thema = thema end |
Class Method Details
.inherited(base) ⇒ Object
40 41 42 43 |
# File 'lib/ubi/consultor.rb', line 40 def inherited(base) Ubi.araneas << base # puts "Using aranea #{base}" end |
.name ⇒ Object
Human-readable name of the aranea
48 49 50 |
# File 'lib/ubi/consultor.rb', line 48 def name fail "Not implemented by #{self}" end |
.url ⇒ Object
Url to query
55 56 57 |
# File 'lib/ubi/consultor.rb', line 55 def url fail "Not implemented by #{self}" end |
Instance Method Details
#datum ⇒ Object
23 24 25 |
# File 'lib/ubi/consultor.rb', line 23 def datum @datum ||= Ubi::Datum.new(parser(request), words, links) end |
#links ⇒ Object
15 16 17 |
# File 'lib/ubi/consultor.rb', line 15 def links '//a' end |
#parser(chunk) ⇒ Object
19 20 21 |
# File 'lib/ubi/consultor.rb', line 19 def parser(chunk) Nokogiri::HTML(chunk) end |
#query_url ⇒ Object
11 12 13 |
# File 'lib/ubi/consultor.rb', line 11 def query_url self.class.url + query.to_query end |
#request(opts = {}) ⇒ Object
Make an HTTP(S) request to a geocoding API and return the response object.
31 32 33 34 35 36 37 |
# File 'lib/ubi/consultor.rb', line 31 def request(opts = {}) timeout(10) do uri = URI.parse(query_url) puts "#{self} working on `#{@thema}` (#{query_url}) #{opts}" uri.open(HEADERS).read end end |