Class: Ldoce::Word
Defined Under Namespace
Classes: MissingApiKey
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #american_pronunciations ⇒ Object
- #british_pronunciations ⇒ Object
- #definition ⇒ Object
- #entries ⇒ Object
-
#initialize(query, response) ⇒ Word
constructor
A new instance of Word.
- #inspect ⇒ Object
- #mp3? ⇒ Boolean
- #mp3_url ⇒ Object
- #play ⇒ Object
- #word ⇒ Object
Constructor Details
#initialize(query, response) ⇒ Word
Returns a new instance of Word.
7 8 9 |
# File 'lib/ldoce/word.rb', line 7 def initialize query, response @query, @response = query, response end |
Class Attribute Details
.api_key ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ldoce/word.rb', line 104 def api_key @api_key ||= YAML.load(File.read "api_key.yml")["api_key"] rescue raise MissingApiKey.new "Either set the API key programmatically: Ldoce::Word.api_key = '<your_key>' or Create a file called api_key.yml and add your Longman API Key: api_key: <your_key_here>" end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'lib/ldoce/word.rb', line 5 def query @query end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
5 6 7 |
# File 'lib/ldoce/word.rb', line 5 def response @response end |
Class Method Details
.find(query) ⇒ Object
93 94 95 |
# File 'lib/ldoce/word.rb', line 93 def find query search query end |
.play(query) ⇒ Object
89 90 91 |
# File 'lib/ldoce/word.rb', line 89 def play query search(query).play end |
.search(query) ⇒ Object
97 98 99 100 |
# File 'lib/ldoce/word.rb', line 97 def search query response = get(url(query)).parsed_response Word.new query, response end |
.url(query) ⇒ Object
116 117 118 |
# File 'lib/ldoce/word.rb', line 116 def url query "https://api.pearson.com/longman/dictionary/entry.json?q=#{query}&apikey=#{api_key}" end |
Instance Method Details
#american_pronunciations ⇒ Object
56 57 58 |
# File 'lib/ldoce/word.rb', line 56 def american_pronunciations entries.map{|e| ["multimedia"].detect{|w| w["@type"]=="US_PRON"}["@href"]} end |
#british_pronunciations ⇒ Object
60 61 62 |
# File 'lib/ldoce/word.rb', line 60 def british_pronunciations entries.map{|e| e["multimedia"].detect{|w| w["@type"]=="GB_PRON"}["@href"]} end |
#definition ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ldoce/word.rb', line 36 def definition definitions = each_in entries do |entry| each_in entry["Sense"] do |f| if f["DEF"] f["DEF"]["#text"] elsif f["Subsense"] each_in f["Subsense"] do |g| g["DEF"]["#text"] end end end end.flatten.compact definitions.map { |e| "\"#{e}\"" }.join(",") end |
#entries ⇒ Object
30 31 32 33 34 |
# File 'lib/ldoce/word.rb', line 30 def entries entries = response["Entries"] entries = entries["Entry"] rescue [] arrayify entries end |
#inspect ⇒ Object
52 53 54 |
# File 'lib/ldoce/word.rb', line 52 def inspect "<Word #{@query}: #{definition} mp3:#{mp3?}>" end |
#mp3? ⇒ Boolean
26 27 28 |
# File 'lib/ldoce/word.rb', line 26 def mp3? !!mp3_url end |
#mp3_url ⇒ Object
64 65 66 67 68 69 |
# File 'lib/ldoce/word.rb', line 64 def mp3_url pronunciation = british_pronunciations.first || american_pronunciations.first url = "https://api.pearson.com/longman/dictionary#{pronunciation}?apikey=#{Word.api_key}" rescue nil end |
#play ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ldoce/word.rb', line 15 def play if mp3? unless File.exists? filename command = "curl #{mp3_url} -silent > #{filename}" `#{command}` end `afplay #{filename}` end self end |
#word ⇒ Object
11 12 13 |
# File 'lib/ldoce/word.rb', line 11 def word query end |