Class: TranslationIO::API::Request

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

Overview

An HTTP request

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ TranslationIO::API::Request

Constructor

Parameters:

  • api_key (String)

    translation.io api key



16
17
18
# File 'lib/translation_io/api/request.rb', line 16

def initialize(api_key:)
  @api_key = api_key
end

Instance Method Details

#get(uri, data) ⇒ TranslationIO::API::Response

Get request

Parameters:

  • uri (String)
  • data (Hash)

    the JSON payload for this request

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/translation_io/api/request.rb', line 27

def get(uri, data)
  uri = URI.parse(uri)
  req = Net::HTTP::Get.new(uri)

  req["Content-Type"] = "application/json"
  req["X-api-key"] = @api_key
  req.body = data.to_json

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  Response.new(res)
end

#post(uri, data) ⇒ TranslationIO::API::Response

Post request

Parameters:

  • uri (String)
  • data (Hash)

    the JSON payload for this request

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/translation_io/api/request.rb', line 49

def post(uri, data)
  uri = URI.parse(uri)
  req = Net::HTTP::Post.new(uri)

  req["Content-Type"] = "application/json"
  req["X-api-key"] = @api_key
  req.body = data.to_json

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  Response.new(res)
end