-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.rb
executable file
·46 lines (35 loc) · 1.18 KB
/
translate.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
41
42
43
44
45
46
# encoding: utf-8
require 'mechanize'
require 'json'
require 'open-uri'
module Translate
def self.get_tokens(grant_type, scope_url, client_id, client_secret, auth_url)
param_arr = {:grant_type => grant_type,
:scope => scope_url,
:client_id => client_id,
:client_secret => client_secret
}
agent = Mechanize.new
cert_store = OpenSSL::X509::Store.new
cert_store.add_file 'cacert.pem'
agent.cert_store = cert_store
response = agent.post auth_url, param_arr
res_hash = JSON.parse response.body
if res_hash['access_token']
return res_hash['access_token']
else
return nil
end
end
def self.get_translation(trans_url, access_token, from, to, input)
param_arr = {#'text' => URI::encode(input.to_s),
:text => input.to_s,
:to => to.to_s,
:from => from.to_s,
}
auth_header = {'Authorization' => 'Bearer ' + access_token}
agent = Mechanize.new
response = agent.get trans_url, param_arr, nil, auth_header
response.xml.children.children.to_s
end
end