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
58 59 60 |
# File 'lib/howdy/base.rb', line 58 def initialize(user_query) # :nodoc @user_query = user_query end |
Instance Attribute Details
#user_query ⇒ Object (readonly)
Returns the value of attribute user_query.
52 53 54 |
# File 'lib/howdy/base.rb', line 52 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.
110 111 112 |
# File 'lib/howdy/base.rb', line 110 def self.description(dictionary_description) @description = dictionary_description end |
.dict_description ⇒ Object
Returns description of the dictionary.
120 121 122 |
# File 'lib/howdy/base.rb', line 120 def self.dict_description @description ||= "" end |
.dict_label ⇒ Object
Returns label of the dictionary.
115 116 117 |
# File 'lib/howdy/base.rb', line 115 def self.dict_label @name ||= self.label.underscore end |
.dict_url ⇒ Object
Returns URL of the dictionary
98 99 100 |
# File 'lib/howdy/base.rb', line 98 def self.dict_url @url end |
.doc_encoding ⇒ Object
Returns an encoding of HTTP response. UTF-8 by default.
130 131 132 |
# File 'lib/howdy/base.rb', line 130 def self.doc_encoding @encoding ||= 'UTF-8' end |
.encoding(enc) ⇒ Object
Sets an encoding of content returned by HTTP response.
125 126 127 |
# File 'lib/howdy/base.rb', line 125 def self.encoding(enc) @encoding = enc.to_s end |
.inherited(base) ⇒ Object
:nodoc
54 55 56 |
# File 'lib/howdy/base.rb', line 54 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
104 105 106 |
# File 'lib/howdy/base.rb', line 104 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.
93 94 95 |
# File 'lib/howdy/base.rb', line 93 def self.url(dictionary_url) @url = dictionary_url end |
Instance Method Details
#document ⇒ Object
Returns content of HTTP response wrapped in Nokogiri::HTML backend.
153 154 155 |
# File 'lib/howdy/base.rb', line 153 def document @document ||= Nokogiri::HTML(open(URI.escape(interpolated_url)), nil, self.class.doc_encoding) 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
148 149 150 |
# File 'lib/howdy/base.rb', line 148 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.
165 166 167 168 169 170 171 172 173 |
# File 'lib/howdy/base.rb', line 165 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'
160 161 162 |
# File 'lib/howdy/base.rb', line 160 def result @result ||= [] end |