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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #check_errors(responce) ⇒ Object
-
#get_langs ⇒ Object
List of translation directions.
-
#lookup(text, *lang) ⇒ Object
Search word or phrase.
- #visit(address, options = {}) ⇒ Object
Instance Attribute Details
#api_key ⇒ Object
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_langs ⇒ Object
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) = { key: api_key, text: text, lang: lang.join('-') } visit('/lookup', ) end |
#visit(address, options = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/yandex_dictionary.rb', line 24 def visit(address, = {}) responce = get(address, query: ) check_errors(responce) unless responce.code == 200 responce end |