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



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

#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



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_pronunciationsObject



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_pronunciationsObject



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

#definitionObject



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

#entriesObject



36
37
38
39
40
# File 'lib/ldoce/word.rb', line 36

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

#inspectObject



58
59
60
# File 'lib/ldoce/word.rb', line 58

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

#mp3?Boolean

Returns:

  • (Boolean)


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

def mp3?
  !!mp3_url
end

#mp3_urlObject



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

#playObject



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

#wordObject



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

def word
  query
end