Class: Ldoce::Word

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ldoce/word.rb

Defined Under Namespace

Classes: MissingApiKey

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_keyObject



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

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/ldoce/word.rb', line 5

def query
  @query
end

#responseObject (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_pronunciationsObject



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_pronunciationsObject



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

#definitionObject



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

#entriesObject



30
31
32
33
34
# File 'lib/ldoce/word.rb', line 30

def entries
  entries = response["Entries"]
  entries = entries["Entry"] rescue []
  arrayify entries
end

#inspectObject



52
53
54
# File 'lib/ldoce/word.rb', line 52

def inspect
  "<Word #{@query}: #{definition} mp3:#{mp3?}>"
end

#mp3?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ldoce/word.rb', line 26

def mp3?
  !!mp3_url
end

#mp3_urlObject



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

#playObject



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

#wordObject



11
12
13
# File 'lib/ldoce/word.rb', line 11

def word
  query
end