Module: Yandex::Dictionary

Extended by:
Dictionary
Includes:
HTTParty
Included in:
Dictionary
Defined in:
lib/yandex_dictionary/version.rb,
lib/yandex_dictionary.rb

Defined Under Namespace

Classes: ApiError, TranslationError

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/yandex_dictionary.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#check_errors(responce) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/yandex_dictionary.rb', line 30

def check_errors(responce)
  raise *(case responce.code
  when 401 then [ApiError, 'Invalid api key']
  when 402 then [ApiError, 'Api key blocked']
  when 403 then [ApiError, 'Daily request limit exceeded']
  when 404 then [ApiError, 'Daily char limit exceeded']
  when 413 then [TranslationError, 'Text too long']
  when 501 then [TranslationError, 'Can\'t translate text in that direction']
  end)
end

#get_langsObject

List of translation directions.



14
15
16
# File 'lib/yandex_dictionary.rb', line 14

def get_langs
  visit '/getLangs', key: api_key
end

#lookup(text, *lang) ⇒ Object

Search word or phrase. Return dictionary entry.



19
20
21
22
# File 'lib/yandex_dictionary.rb', line 19

def lookup(text, *lang)
  options = { key: api_key, text: text, lang: lang.join('-') }
  visit('/lookup', options)
end

#visit(address, options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/yandex_dictionary.rb', line 24

def visit(address, options = {})
  responce = get(address, query: options)
  check_errors(responce) unless responce.code == 200
  responce
end