Class: LibreTranslate::Api

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

Class Method Summary collapse

Class Method Details

.get(path) ⇒ Object



5
6
7
8
9
# File 'lib/libre_translate/api.rb', line 5

def self.get(path)
  uri = uri_from_path(path)
  body = Net::HTTP.get(uri)
  JSON.parse(body)
end

.post(path, params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/libre_translate/api.rb', line 11

def self.post(path, params = {})
  uri = uri_from_path(path)
  params.merge!({ api_key: LibreTranslate.api_key })

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
    request = Net::HTTP::Post.new(uri)
    request["Content-Type"] = "application/json"
    request.body = params.to_json
    http.request(request)
  end

  raise_error(response) unless response.code == "200"
  JSON.parse(response.body)
end