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
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ldoce/word.rb', line 110 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
99 100 101 |
# File 'lib/ldoce/word.rb', line 99 def find query search query end |
.play(query) ⇒ Object
95 96 97 |
# File 'lib/ldoce/word.rb', line 95 def play query search(query).play end |
.search(query) ⇒ Object
103 104 105 106 |
# File 'lib/ldoce/word.rb', line 103 def search query response = get(url(query)).parsed_response Word.new query, response end |
.url(query) ⇒ Object
122 123 124 |
# File 'lib/ldoce/word.rb', line 122 def url query "https://api.pearson.com/longman/dictionary/entry.json?q=#{query}&apikey=#{api_key}" end |
Instance Method Details
#american_pronunciations ⇒ Object
62 63 64 |
# File 'lib/ldoce/word.rb', line 62 def american_pronunciations entries.map{|e| ["multimedia"].detect{|w| w["@type"]=="US_PRON"}["@href"]} end |
#british_pronunciations ⇒ Object
66 67 68 |
# File 'lib/ldoce/word.rb', line 66 def british_pronunciations entries.map{|e| e["multimedia"].detect{|w| w["@type"]=="GB_PRON"}["@href"]} end |
#definition ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ldoce/word.rb', line 42 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
36 37 38 39 40 |
# File 'lib/ldoce/word.rb', line 36 def entries entries = response["Entries"] entries = entries["Entry"] rescue [] arrayify entries end |
#inspect ⇒ Object
58 59 60 |
# File 'lib/ldoce/word.rb', line 58 def inspect "<Word #{@query}: #{definition} mp3:#{mp3?}>" end |
#mp3? ⇒ Boolean
32 33 34 |
# File 'lib/ldoce/word.rb', line 32 def mp3? !!mp3_url end |
#mp3_url ⇒ Object
70 71 72 73 74 75 |
# File 'lib/ldoce/word.rb', line 70 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 25 26 27 28 29 30 |
# 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 rescue raise DependencyMissingException.new( "This gem depends on curl and afplay for fetching and playing audio, feel free to fork or make a pull request to improve it ") ensure self end |
#word ⇒ Object
11 12 13 |
# File 'lib/ldoce/word.rb', line 11 def word query end |