bing_translator 4.0.0 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bing_translator.rb +27 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21849fa61aa43268772d2237149d17d00553d11e
|
4
|
+
data.tar.gz: 754749a9704a2e4ef72ce0d6d3616bc8a7676c62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4223f11f756cc110f4221718872009de81c39aaa27e994b7f218c1ad285c39ee500312416052bf16a07f7a19cc8161160f5c2090078ffb2f71a04adc4be15d3
|
7
|
+
data.tar.gz: 0d50bead3436c60af808ea98d32e19265d3241a5d7a93280555dc2bc59eef9506baa49c288e2a8f5e4f762fac032c453b812c7eea80989fdf33b047cd378497c
|
data/lib/bing_translator.rb
CHANGED
@@ -16,13 +16,15 @@ class BingTranslator
|
|
16
16
|
LANG_CODE_LIST_URI = 'http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate'
|
17
17
|
ACCESS_TOKEN_URI = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
|
18
18
|
SPEAK_URI = 'http://api.microsofttranslator.com/v2/Http.svc/Speak'
|
19
|
+
DATASETS_URI = "https://api.datamarket.azure.com/Services/My/Datasets?$format=json"
|
19
20
|
|
20
21
|
class Exception < StandardError; end
|
21
22
|
class AuthenticationException < StandardError; end
|
22
23
|
|
23
|
-
def initialize(client_id, client_secret, skip_ssl_verify = false)
|
24
|
+
def initialize(client_id, client_secret, skip_ssl_verify = false, account_key = nil)
|
24
25
|
@client_id = client_id
|
25
26
|
@client_secret = client_secret
|
27
|
+
@account_key = account_key
|
26
28
|
@skip_ssl_verify = skip_ssl_verify
|
27
29
|
|
28
30
|
@translate_uri = URI.parse TRANSLATE_URI
|
@@ -30,6 +32,7 @@ class BingTranslator
|
|
30
32
|
@list_codes_uri = URI.parse LANG_CODE_LIST_URI
|
31
33
|
@access_token_uri = URI.parse ACCESS_TOKEN_URI
|
32
34
|
@speak_uri = URI.parse SPEAK_URI
|
35
|
+
@datasets_uri = URI.parse DATASETS_URI
|
33
36
|
end
|
34
37
|
|
35
38
|
def translate(text, params = {})
|
@@ -37,10 +40,10 @@ class BingTranslator
|
|
37
40
|
|
38
41
|
from = CGI.escape params[:from].to_s
|
39
42
|
params = {
|
40
|
-
'to'
|
41
|
-
'text'
|
42
|
-
'category'
|
43
|
-
'contentType' => 'text/plain'
|
43
|
+
'to' => CGI.escape(params[:to].to_s),
|
44
|
+
'text' => CGI.escape(text.to_s),
|
45
|
+
'category' => 'general',
|
46
|
+
'contentType' => params[:content_type] || 'text/plain'
|
44
47
|
}
|
45
48
|
params[:from] = from unless from.empty?
|
46
49
|
result = result @translate_uri, params
|
@@ -82,7 +85,26 @@ class BingTranslator
|
|
82
85
|
end
|
83
86
|
|
84
87
|
|
88
|
+
def balance
|
89
|
+
datasets["d"]["results"].each do |result|
|
90
|
+
return result["ResourceBalance"] if result["ProviderName"] == "Microsoft Translator"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
85
94
|
private
|
95
|
+
def datasets
|
96
|
+
raise AuthenticationException, "Must provide account key" if @account_key.nil?
|
97
|
+
|
98
|
+
http = Net::HTTP.new(@datasets_uri.host, @datasets_uri.port)
|
99
|
+
http.use_ssl = true
|
100
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
101
|
+
request = Net::HTTP::Get.new(@datasets_uri.request_uri)
|
102
|
+
request.basic_auth("", @account_key)
|
103
|
+
response = http.request(request)
|
104
|
+
|
105
|
+
JSON.parse response.body
|
106
|
+
end
|
107
|
+
|
86
108
|
def prepare_param_string(params)
|
87
109
|
params.map { |key, value| "#{key}=#{value}" }.join '&'
|
88
110
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bing_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricky Elrod
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
65
|
version: '0'
|
66
66
|
requirements: []
|
67
67
|
rubyforge_project:
|
68
|
-
rubygems_version: 2.0.
|
68
|
+
rubygems_version: 2.0.5
|
69
69
|
signing_key:
|
70
70
|
specification_version: 4
|
71
71
|
summary: Translate using the Bing HTTP API
|