microsoft_translator 0.1.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p125@microsoft_translator --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in microsoft_translator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ikayzo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MicrosoftTranslator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'microsoft_translator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install microsoft_translator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,43 @@
1
+ module MicrosoftTranslator
2
+ class AzureAuthentication
3
+ AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
4
+ API_SCOPE = 'http://api.microsofttranslator.com'
5
+ GRANT_TYPE = 'client_credentials'
6
+
7
+ attr_reader :token
8
+ attr_reader :token_expires_at
9
+
10
+ def initialize(client_id, client_secret)
11
+ @client_id = client_id
12
+ @client_secret = client_secret
13
+ renew_token
14
+ end
15
+
16
+ def current_token
17
+ if @token_expires_at < Time.now
18
+ renew_token
19
+ end
20
+ @token
21
+ end
22
+
23
+
24
+ def renew_token
25
+ auth_response = RestClient.post(AUTH_URL, auth_params)
26
+ parsed_json = JSON.parse(auth_response.body)
27
+ @token_expires_at = Time.now + parsed_json['expires_in'].to_i
28
+ @token = parsed_json['access_token']
29
+ end
30
+
31
+ private
32
+
33
+ def auth_params
34
+ {
35
+ "client_id" => @client_id,
36
+ "client_secret" => URI.encode(@client_secret),
37
+ "scope" => URI.encode(API_SCOPE),
38
+ "grant_type" => GRANT_TYPE
39
+ }
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ module MicrosoftTranslator
2
+ class Client
3
+
4
+ def initialize(client_id, client_secret)
5
+ @client_id = client_id
6
+ raise "client_id must be a string" unless @client_id.is_a?(String)
7
+ @client_secret = client_secret
8
+ raise "client_secret must be a string" unless @client_secret.is_a?(String)
9
+ @auth = MicrosoftTranslator::AzureAuthentication.new(client_id, client_secret)
10
+ end
11
+
12
+ def translate(text, from_lang, to_lang, content_type)
13
+ response = RestClient.get(
14
+ "http://api.microsofttranslator.com/V2/Http.svc/Translate",
15
+ translate_params(text, from_lang, to_lang, content_type)
16
+ )
17
+ parse_xml(response.body)
18
+ end
19
+
20
+ private
21
+
22
+ def base_params
23
+ {:Authorization => "Bearer #{@auth.current_token}"}
24
+ end
25
+
26
+ def translate_params(text, from_lang, to_lang, content_type)
27
+ hash = base_params
28
+ hash.store(:params,{
29
+ "text" => text,
30
+ "from" => from_lang,
31
+ "to" => to_lang,
32
+ "contentType" => content_type
33
+ })
34
+ puts hash
35
+ hash
36
+ end
37
+
38
+ def parse_xml(xml)
39
+ xml_doc = Nokogiri::XML(xml)
40
+ xml_doc.xpath("/").text
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module MicrosoftTranslator
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'microsoft_translator/version'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'rest-client'
5
+ require 'nokogiri'
6
+
7
+ module MicrosoftTranslator
8
+ autoload :Client, 'microsoft_translator/client'
9
+ autoload :AzureAuthentication, 'microsoft_translator/azure_authentication'
10
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/microsoft_translator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Christopher Sass"]
6
+ gem.email = ["chris@lupinedev.com"]
7
+ gem.description = %q{Ruby library for Microsoft Translate HTTP API}
8
+ gem.summary = %q{Use Microsoft Translate API in your ruby program}
9
+ gem.homepage = "https://github.com/ikayzo/microsoft_translator"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "microsoft_translator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MicrosoftTranslator::VERSION
17
+ gem.add_runtime_dependency "rest-client", [">= 1.6.0"]
18
+ gem.add_runtime_dependency "nokogiri", [">= 1.4.0"]
19
+ gem.add_development_dependency "rspec", [">= 2.0.0"]
20
+ gem.add_development_dependency "timecop", [">= 0.3.5"]
21
+ end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe MicrosoftTranslator::AzureAuthentication do
4
+
5
+ after :all do
6
+ Timecop.return
7
+ end
8
+
9
+ describe "#initialize" do
10
+ before :each do
11
+ stub_auth_request
12
+ end
13
+ it "should get auth token" do
14
+ auth = MicrosoftTranslator::AzureAuthentication.new('id', 'secret')
15
+ auth.token.should eq('stubbed_token')
16
+ end
17
+ end
18
+
19
+ describe "#renew_token" do
20
+ before :each do
21
+ stub_auth_request
22
+ @auth = MicrosoftTranslator::AzureAuthentication.new('id','secret')
23
+ end
24
+
25
+ it "should build send a request if to get a new token" do
26
+ RestClient.should_receive(:post).with(MicrosoftTranslator::AzureAuthentication::AUTH_URL, auth_params('id', 'secret'))
27
+ @auth.renew_token
28
+ end
29
+
30
+ it "should set the token" do
31
+ @auth.renew_token
32
+ @auth.token.should eq("stubbed_token")
33
+ end
34
+
35
+ it "should set token_expires_at" do
36
+ Timecop.freeze
37
+ @auth.renew_token
38
+ @auth.token_expires_at.should eq(Time.now + 599)
39
+ end
40
+ end
41
+
42
+ describe "#latest_token" do
43
+ before :each do
44
+ stub_auth_request('first_token')
45
+ @auth = MicrosoftTranslator::AzureAuthentication.new('id','secret')
46
+ end
47
+
48
+ after :each do
49
+ Timecop.return
50
+ end
51
+
52
+ it "should return the current_token if it has not expired yet" do
53
+ @auth.should_not_receive(:renew_token)
54
+ Timecop.travel(@auth.token_expires_at - 100)
55
+ @auth.current_token.should eq('first_token')
56
+ end
57
+
58
+ it "should get a new token if it has not expired yet" do
59
+ pending "having trouble testing"
60
+ @auth.should_receive(:renew_token)
61
+ Timecop.travel(@auth.token_expires_at + 1000)
62
+ stub_auth_request('new_token')
63
+ token = @auth.current_token
64
+ token.should eq('new_token')
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe MicrosoftTranslator::Client do
4
+ describe "#initialize" do
5
+ it "should build a new AzureAuthentication with the id and secret" do
6
+ stub_auth_request
7
+ MicrosoftTranslator::AzureAuthentication.should_receive(:new).with('id','secret')
8
+ translator = MicrosoftTranslator::Client.new('id', 'secret')
9
+ end
10
+ end
11
+
12
+ describe "#translate" do
13
+ it "should send a request with the correct params" do
14
+ pending "better way to test this"
15
+ stub_auth_request
16
+ translator = MicrosoftTranslator::Client.new('id', 'secret')
17
+ spanish = "no corras muchacho"
18
+ stub_translate_request("don't run boy")
19
+ translator.translate(spanish,"es","en","text/html").should eq("don't run boy")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'microsoft_translator'
5
+ require 'timecop'
6
+
7
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
8
+
9
+ RSpec.configure do |config|
10
+ # some (optional) config here
11
+ end
@@ -0,0 +1,19 @@
1
+ def auth_params(id, secret)
2
+ {
3
+ "client_id" => id,
4
+ "client_secret" => secret,
5
+ "scope" => URI.encode(MicrosoftTranslator::AzureAuthentication::API_SCOPE),
6
+ "grant_type" => MicrosoftTranslator::AzureAuthentication::GRANT_TYPE
7
+ }
8
+ end
9
+ def stub_auth_request(returned_token=nil)
10
+ response = stub("response", :body => {"access_token"=>"#{returned_token || 'stubbed_token'}", "token_type"=>"http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0", "expires_in"=>"599", "scope"=>"http://api.microsofttranslator.com"}.to_json)
11
+
12
+ RestClient.stub(:post).and_return(response)
13
+ end
14
+
15
+ def stub_translate_request(translated_text=nil)
16
+ response = stub("response", :body => "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">#{translated_text || "hello world"}</string>")
17
+
18
+ RestClient.stub(:get).and_return(response)
19
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: microsoft_translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Sass
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.4.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.4.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: timecop
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.3.5
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.3.5
78
+ description: Ruby library for Microsoft Translate HTTP API
79
+ email:
80
+ - chris@lupinedev.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .rvmrc
88
+ - Gemfile
89
+ - LICENSE
90
+ - README.md
91
+ - Rakefile
92
+ - lib/microsoft_translator.rb
93
+ - lib/microsoft_translator/azure_authentication.rb
94
+ - lib/microsoft_translator/client.rb
95
+ - lib/microsoft_translator/version.rb
96
+ - microsoft_translator.gemspec
97
+ - spec/lib/microsoft_translator/azure_authentication_spec.rb
98
+ - spec/lib/microsoft_translator/client_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/support/requests_helper.rb
101
+ homepage: https://github.com/ikayzo/microsoft_translator
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.19
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Use Microsoft Translate API in your ruby program
125
+ test_files:
126
+ - spec/lib/microsoft_translator/azure_authentication_spec.rb
127
+ - spec/lib/microsoft_translator/client_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/requests_helper.rb