Class: Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/Connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
# File 'lib/Connection.rb', line 6

def initialize(key)
  @key = key
  @http = Net::HTTP.new('translate.yandex.net', 443)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Method Details

#request(path, parameters) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/Connection.rb', line 13

def request(path, parameters)
  request = Net::HTTP::Post.new("/api/v1.5/tr.json/#{path}")
  request.set_form_data({key:@key}.merge(parameters)) 
  tmp = JSON.parse(@http.request(request).body)
  if (tmp['code'] == nil) || (tmp['code'] == 200)
    tmp
  else
    raise YandexError.new(tmp['message'], tmp['code'])
  end
end