Class: DictionaryLookup::Pearson
- Inherits:
-
Object
- Object
- DictionaryLookup::Pearson
- Defined in:
- lib/dictionary_lookup/pearson.rb
Constant Summary collapse
- VALID_DICTIONARIES =
["ldoce5", "lasde", "ldec", "wordwise", "laesd", "leasd", "laad3", "laes", "lase", "brep", "brpe"]
- DEFAULT_DICTIONARY =
VALID_DICTIONARIES.first
Class Method Summary collapse
-
.define(term, config) ⇒ Array
Fetches term definitions from Pearson dictionary API.
-
.get_dictionary(config) ⇒ Object
Helper method.
Class Method Details
.define(term, config) ⇒ Array
Fetches term definitions from Pearson dictionary API
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dictionary_lookup/pearson.rb', line 26 def self.define(term, config) dictionary = get_dictionary(config) url = "https://api.pearson.com:443/v2/dictionaries/#{dictionary}/entries?headword=#{term}" uri = URI(URI.escape(url)) response = Net::HTTP.get(uri) data = JSON.parse(response) # Select definitions that match exactly with the term results = data["results"].select{ |d| d["headword"].downcase == term.downcase } definitions = [] results.each do |result| part_of_speech = result["part_of_speech"] # In wordwise dictionary, value of definition is String. Hence the is_a? test _definitions = result["senses"].first["definition"] denotation = _definitions.is_a?(Array) ? _definitions.first : _definitions if result["senses"].first["examples"].nil? examples = [] else examples = result["senses"].first["examples"].map{|e| e["text"]} end definitions << DictionaryLookup::Definition.new(part_of_speech, denotation, examples) end definitions end |
.get_dictionary(config) ⇒ Object
Helper method. Returns either a valid dictionary specified through config or the default dictionary
14 15 16 17 |
# File 'lib/dictionary_lookup/pearson.rb', line 14 def self.get_dictionary(config) VALID_DICTIONARIES.include?(config[:dictionary]) ? config[:dictionary] : DEFAULT_DICTIONARY end |