Class: Vocabulary::Dictionary

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

Instance Method Summary collapse

Instance Method Details

#get(word, source_lang, target_lang) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vocabulary.rb', line 42

def get(word, source_lang, target_lang)
  # example request
  # /dictionary/json?callback=substr&q=apple&sl=en&tl=en&restrict=pr%2Cde&client=te
  options = {}
  options[:query] = {
    :callback => "substr",
    :q => word,
    :sl => target_lang, # source lang
    :tl => source_lang # target lang
  }
  
  response = self.class.get("/dictionary/json", options)
  if response
    info = response.parsed_response
    json = info.gsub("substr(", "").gsub!(",200,null)", "")
    JSON.parse(json)
  end
end