Class: Howdy::Dictionary::Base
- Inherits:
-
Object
- Object
- Howdy::Dictionary::Base
- Defined in:
- lib/howdy/base.rb
Overview
Base class for all dictionaries. Inherit from it if you want to implement your own dictionary.
Example:
class AwesomeDictionary < Howdy::Dictionary::Base
url 'http://awesome.example.com?word=#{user_query}'
label 'example.com'
description 'Most awesome dictionary you've ever seen'
def parse
document.css('table.results tr td.word') do |cell|
result << cell.content
end
end
end
Direct Known Subclasses
Instance Attribute Summary collapse
-
#user_query ⇒ Object
readonly
Returns the value of attribute user_query.
Class Method Summary collapse
-
.description(dictionary_description) ⇒ Object
Sets a description of the dictionary.
-
.dict_description ⇒ Object
Returns description of the dictionary.
-
.dict_label ⇒ Object
Returns label of the dictionary.
-
.dict_url ⇒ Object
Returns URL of the dictionary.
-
.doc_encoding ⇒ Object
Returns an encoding of HTTP response.
-
.encoding(enc) ⇒ Object
Sets an encoding of content returned by HTTP response.
-
.inherited(base) ⇒ Object
:nodoc.
-
.label(human_name) ⇒ Object
Sets a label of the dictionary.
-
.url(dictionary_url) ⇒ Object
Sets URL to query the web dictionary.
Instance Method Summary collapse
-
#document ⇒ Object
Returns content of HTTP response wrapped in Nokogiri::HTML backend.
-
#initialize(user_query) ⇒ Base
constructor
:nodoc.
-
#parse ⇒ Object
Method which transformates raw content received in HTTP response to human-friendly form.
-
#print ⇒ Object
Prints the contents of
result
array. -
#result ⇒ Object
Array which stores the answers for the user query.
Constructor Details
#initialize(user_query) ⇒ Base
:nodoc
72 73 74 |
# File 'lib/howdy/base.rb', line 72 def initialize(user_query) # :nodoc @user_query = user_query.squeeze(' ').strip end |
Instance Attribute Details
#user_query ⇒ Object (readonly)
Returns the value of attribute user_query.
66 67 68 |
# File 'lib/howdy/base.rb', line 66 def user_query @user_query end |
Class Method Details
.description(dictionary_description) ⇒ Object
Sets a description of the dictionary. Description is used when listing available dictionaries.
124 125 126 |
# File 'lib/howdy/base.rb', line 124 def self.description(dictionary_description) @description = dictionary_description end |
.dict_description ⇒ Object
Returns description of the dictionary.
134 135 136 |
# File 'lib/howdy/base.rb', line 134 def self.dict_description @description ||= "" end |
.dict_label ⇒ Object
Returns label of the dictionary.
129 130 131 |
# File 'lib/howdy/base.rb', line 129 def self.dict_label @name ||= self.label.underscore end |
.dict_url ⇒ Object
Returns URL of the dictionary
112 113 114 |
# File 'lib/howdy/base.rb', line 112 def self.dict_url @url end |
.doc_encoding ⇒ Object
Returns an encoding of HTTP response. UTF-8 by default.
144 145 146 |
# File 'lib/howdy/base.rb', line 144 def self.doc_encoding @encoding ||= 'UTF-8' end |
.encoding(enc) ⇒ Object
Sets an encoding of content returned by HTTP response.
139 140 141 |
# File 'lib/howdy/base.rb', line 139 def self.encoding(enc) @encoding = enc.to_s end |
.inherited(base) ⇒ Object
:nodoc
68 69 70 |
# File 'lib/howdy/base.rb', line 68 def self.inherited(base) # :nodoc Howdy::Dictionary.available << base end |
.label(human_name) ⇒ Object
Sets a label of the dictionary. Label is used when listing available dictionaries
118 119 120 |
# File 'lib/howdy/base.rb', line 118 def self.label(human_name) @name = human_name.to_s end |
.url(dictionary_url) ⇒ Object
Sets URL to query the web dictionary. You must pass protocol as well:
http://dictionary.com is correct
dictionary.com is incorrect
URL is interpolated:
'http://dictionary.com/browse/#{user_query}'
user_query
is a string given by the user
You may provide own interpolations easily:
Example:
class Dictionary < Howdy::Dictionary::Base
url 'http://awesome.example.com?word=#{user_query}&language=#{lang}'
label 'example.com'
description 'Interpolations example'
def lang
File.read('/etc/howdy/language.conf')
end
def parse
# implement parsing
end
end
In that example #{lang} in url will be replaced with content of
/etc/howdy/language.conf
Indicate the url must be given using SINGLE QUOTES.
107 108 109 |
# File 'lib/howdy/base.rb', line 107 def self.url(dictionary_url) @url = dictionary_url end |
Instance Method Details
#document ⇒ Object
Returns content of HTTP response wrapped in Nokogiri::HTML backend.
167 168 169 170 171 172 173 174 175 |
# File 'lib/howdy/base.rb', line 167 def document # FIXME: deal more sophistically with exception begin @document ||= Nokogiri::HTML(open(URI.escape(interpolated_url)), nil, self.class.doc_encoding) rescue UI.error "Nothing found" exit 0 end end |
#parse ⇒ Object
Method which transformates raw content received in HTTP response to human-friendly form. Usually you’ll use document
which exposes the raw content wrapped by Nokogiri parser. Read to nokogiri documentation for details.
Each human readable word goes to result
array.
Example:
def parse
document.css('table.content tr td.word').each do |row|
result << row.content
end
end
162 163 164 |
# File 'lib/howdy/base.rb', line 162 def parse raise Howdy::HowdyError, "Implement parse in your Dictionary::Base subclass" end |
#print ⇒ Object
Prints the contents of result
array. You may ovveride it in subclass.
185 186 187 188 189 190 191 192 193 |
# File 'lib/howdy/base.rb', line 185 def print if result.empty? puts "Nothing found" else result.each do |item| puts item.to_s end end end |
#result ⇒ Object
Array which stores the answers for the user query. Used in parse
method.
Example:
result << 'Polyglot - a person who speaks, writes, or reads a number of languages'
180 181 182 |
# File 'lib/howdy/base.rb', line 180 def result @result ||= [] end |