bing_translator 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/bing_translator.rb +43 -10
  2. metadata +7 -7
@@ -6,20 +6,25 @@ require 'rubygems'
6
6
  require 'cgi'
7
7
  require 'uri'
8
8
  require 'net/http'
9
+ require 'net/https'
9
10
  require 'nokogiri'
11
+ require 'json'
10
12
 
11
13
  class BingTranslator
12
14
  TRANSLATE_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Translate'
13
15
  DETECT_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Detect'
14
16
  LANG_CODE_LIST_URI = 'http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate'
17
+ ACCESS_TOKEN_URI = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
15
18
 
16
- def initialize(api_key)
17
- @api_key = api_key
19
+ def initialize(client_id, client_secret)
20
+ @client_id = client_id
21
+ @client_secret = client_secret
18
22
  @translate_uri = URI.parse TRANSLATE_URI
19
23
  @detect_uri = URI.parse DETECT_URI
20
24
  @list_codes_uri = URI.parse LANG_CODE_LIST_URI
25
+ @access_token_uri = URI.parse ACCESS_TOKEN_URI
21
26
  end
22
-
27
+
23
28
  def translate(text, params = {})
24
29
  raise "Must provide :to." if params[:to].nil?
25
30
 
@@ -27,7 +32,6 @@ class BingTranslator
27
32
  params = {
28
33
  'to' => CGI.escape(params[:to].to_s),
29
34
  'text' => CGI.escape(text.to_s),
30
- 'appId' => @api_key,
31
35
  'category' => 'general',
32
36
  'contentType' => 'text/plain'
33
37
  }
@@ -40,7 +44,6 @@ class BingTranslator
40
44
  def detect(text)
41
45
  params = {
42
46
  'text' => CGI.escape(text.to_s),
43
- 'appId' => @api_key,
44
47
  'category' => 'general',
45
48
  'contentType' => 'text/plain'
46
49
  }
@@ -50,17 +53,47 @@ class BingTranslator
50
53
  end
51
54
 
52
55
  def supported_language_codes
53
- params = {'appId' => @api_key}
54
- result = result @list_codes_uri, params
56
+ result = result @list_codes_uri
55
57
  Nokogiri.parse(result.body).xpath("//xmlns:string").map(&:content)
56
58
  end
57
59
 
58
60
  private
59
61
  def prepare_param_string(params)
60
- params.map {|key, value| "#{key}=#{value}"}.join '&'
62
+ params.map { |key, value| "#{key}=#{value}" }.join '&'
63
+ end
64
+
65
+ def result(uri, params={})
66
+ get_access_token
67
+ result = Net::HTTP.new(uri.host, uri.port).get(
68
+ "#{uri.path}?#{prepare_param_string(params)}",
69
+ { 'Authorization' => "Bearer #{@access_token['access_token']}" })
61
70
  end
62
71
 
63
- def result(uri, params)
64
- result = Net::HTTP.new(uri.host, uri.port).get("#{uri.path}?#{prepare_param_string(params)}")
72
+ # Private: Get a new access token
73
+ #
74
+ # Microsoft changed up how you get access to the Translate API.
75
+ # This gets a new token if it's required. We call this internally
76
+ # before any request we make to the Translate API.
77
+ #
78
+ # Returns nothing if we don't need a new token yet, or
79
+ # a Hash of information relating to the token if we obtained a new one.
80
+ # Also sets @access_token internally.
81
+ def get_access_token
82
+ return @access_token if @access_token and
83
+ Time.now < @access_token['expires_at']
84
+
85
+ params = {
86
+ 'client_id' => CGI.escape(@client_id),
87
+ 'client_secret' => CGI.escape(@client_secret),
88
+ 'scope' => CGI.escape('http://api.microsofttranslator.com'),
89
+ 'grant_type' => 'client_credentials'
90
+ }
91
+
92
+ http = Net::HTTP.new(@access_token_uri.host, @access_token_uri.port)
93
+ http.use_ssl = true
94
+ response = http.post(@access_token_uri.path, prepare_param_string(params))
95
+ @access_token = JSON.parse(response.body)
96
+ @access_token['expires_at'] = Time.now + @access_token['expires_in'].to_i
97
+ @access_token
65
98
  end
66
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bing_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-21 00:00:00.000000000 Z
12
+ date: 2012-06-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &9487740 !ruby/object:Gem::Requirement
16
+ requirement: &13280360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,9 +21,9 @@ dependencies:
21
21
  version: 1.5.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *9487740
25
- description: Translate strings using the Bing HTTP API. Requires that you have an
26
- API key. See http://www.microsoft.com/web/post/using-the-free-bing-translation-apis
24
+ version_requirements: *13280360
25
+ description: Translate strings using the Bing HTTP API. Requires that you have a Client
26
+ ID and Secret. See README.md for information.
27
27
  email: ricky@elrod.me
28
28
  executables: []
29
29
  extensions: []
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version: '0'
51
51
  requirements: []
52
52
  rubyforge_project:
53
- rubygems_version: 1.8.15
53
+ rubygems_version: 1.8.17
54
54
  signing_key:
55
55
  specification_version: 3
56
56
  summary: Translate using the Bing HTTP API