Class: Y::Translate::API

Inherits:
Object
  • Object
show all
Defined in:
lib/y/translate/api.rb

Constant Summary collapse

BASE_URI =
'https://translate.yandex.net/api/v1.5/tr.json'.freeze
METHOD_GET_LANGS =
'getLangs'.freeze
METHOD_DETECT =
'detect'.freeze
METHOD_TRANSLATE =
'translate'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ API

Returns a new instance of API.



15
16
17
# File 'lib/y/translate/api.rb', line 15

def initialize(key)
  @key = key
end

Class Attribute Details

.apiObject

Returns the value of attribute api.



20
21
22
# File 'lib/y/translate/api.rb', line 20

def api
  @api
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



13
14
15
# File 'lib/y/translate/api.rb', line 13

def key
  @key
end

Class Method Details

.keyObject



27
28
29
# File 'lib/y/translate/api.rb', line 27

def self.key
  api.key
end

.key=(key) ⇒ Object



23
24
25
# File 'lib/y/translate/api.rb', line 23

def self.key=(key)
  @api = new(key)
end

.request(type, method, params = {}) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/y/translate/api.rb', line 31

def self.request(type, method, params = {})
  url = URI.parse("#{BASE_URI}/#{method}")
  params[:key] = key

  begin
    response = HTTP.send(type, url, params: params)
  rescue
    raise Error.new
  end

  code = response.code.to_i
  raise Error.new(code) if Error::ERROR_CODES.key?(code)

  response.parse
end