Class: DictionaryLookup::Pearson
- Inherits:
-
Object
- Object
- DictionaryLookup::Pearson
- Defined in:
- lib/dictionary_lookup/pearson.rb
Class Method Summary collapse
-
.define(term) ⇒ Array
Fetches term definitions from Pearson dictionary API.
Class Method Details
.define(term) ⇒ Array
Fetches term definitions from Pearson dictionary API
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dictionary_lookup/pearson.rb', line 12 def self.define(term) url = "https://api.pearson.com:443/v2/dictionaries/ldoce5/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"] denotation = result["senses"].first["definition"].first 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 |