-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDetect.rb
40 lines (31 loc) · 1.07 KB
/
Detect.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'net/https'
require 'uri'
require 'cgi'
require 'json'
require 'securerandom'
if (!ENV["TRANSLATOR_TEXT_SUBSCRIPTION_KEY"])
raise "Please set/export the following environment variable: TRANSLATOR_TEXT_SUBSCRIPTION_KEY"
else
subscription_key = ENV["TRANSLATOR_TEXT_SUBSCRIPTION_KEY"]
end
if (!ENV["TRANSLATOR_TEXT_ENDPOINT"])
raise "Please set/export the following environment variable: TRANSLATOR_TEXT_ENDPOINT"
else
endpoint = ENV["TRANSLATOR_TEXT_ENDPOINT"]
end
path = '/detect?api-version=3.0'
uri = URI (endpoint + path)
text = 'Salve, mondo!'
content = '[{"Text" : "' + text + '"}]'
request = Net::HTTP::Post.new(uri)
request['Content-type'] = 'application/json'
request['Content-length'] = content.length
request['Ocp-Apim-Subscription-Key'] = subscription_key
request['X-ClientTraceId'] = SecureRandom.uuid
request.body = content
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request (request)
end
result = response.body.force_encoding("utf-8")
json = JSON.pretty_generate(JSON.parse(result))
puts json