Class: Camdict::Client
- Inherits:
-
HTTP::Client
- Object
- HTTP::Client
- Camdict::Client
- Defined in:
- lib/camdict/client.rb
Overview
The client downloads all the useful data about a word or phrase from remote Cambridge dictionaries, but not includes the extended data. For example, when the word “mind” is searched, the exactly matched entry is downloaded. However, other related entries like “turn of mind” & “open mind” are not included.
Instance Attribute Summary collapse
-
#dictionary ⇒ Object
readonly
Returns the value of attribute dictionary.
Instance Method Summary collapse
-
#get_htmldef(url) ⇒ Object
Get a word html definition page by its entry
url
. -
#html_definition(word) ⇒ Object
Get a word’s html definition from the web dictionary.
-
#initialize(dict = nil) ⇒ Client
constructor
Default dictionary is British english.
-
#search_url(word) ⇒ Object
search a word with this URL.
- #word_url(word) ⇒ Object
Constructor Details
#initialize(dict = nil) ⇒ Client
Default dictionary is British english. Other possible dict
values: english-chinese-simplified, learner-english, essential-british-english, essential-american-english, etc.
19 20 21 |
# File 'lib/camdict/client.rb', line 19 def initialize(dict = nil) @dictionary = dict || 'english' end |
Instance Attribute Details
#dictionary ⇒ Object (readonly)
Returns the value of attribute dictionary.
14 15 16 |
# File 'lib/camdict/client.rb', line 14 def dictionary @dictionary end |
Instance Method Details
#get_htmldef(url) ⇒ Object
Get a word html definition page by its entry url
.
36 37 38 |
# File 'lib/camdict/client.rb', line 36 def get_htmldef(url) di_extracted get_html(url) end |
#html_definition(word) ⇒ Object
Get a word’s html definition from the web dictionary. The returned result could be an empty string when nothing is found, or its html definition
26 27 28 29 30 31 32 33 |
# File 'lib/camdict/client.rb', line 26 def html_definition(word) html = fetch(word) if html di_extracted(html) else search(word) end end |
#search_url(word) ⇒ Object
search a word with this URL
41 42 43 |
# File 'lib/camdict/client.rb', line 41 def search_url(word) "#{host}/search/#{@dictionary}/?q=#{word}" end |
#word_url(word) ⇒ Object
45 46 47 |
# File 'lib/camdict/client.rb', line 45 def word_url(word) "#{host}/dictionary/#{@dictionary}/#{encode(word).downcase}" end |