bing_translator 0.0.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/lib/bing_translator.rb +42 -0
- metadata +66 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'cgi'
|
4
|
+
require 'uri'
|
5
|
+
require 'net/http'
|
6
|
+
require 'nokogiri'
|
7
|
+
require 'pp'
|
8
|
+
|
9
|
+
class BingTranslator
|
10
|
+
TRANSLATE_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Translate'
|
11
|
+
|
12
|
+
def initialize(params = {})
|
13
|
+
if params[:api_key].nil?
|
14
|
+
raise "Must pass :api_key when initializing BingTranslator"
|
15
|
+
end
|
16
|
+
@api_key = params[:api_key]
|
17
|
+
end
|
18
|
+
|
19
|
+
def translate(params = {})
|
20
|
+
if params[:to].nil? or params[:from].nil? or params[:text].nil?
|
21
|
+
raise "Must provide :to, :from, and :text."
|
22
|
+
else
|
23
|
+
to = CGI.escape params[:to]
|
24
|
+
from = CGI.escape params[:from]
|
25
|
+
text = CGI.escape params[:text]
|
26
|
+
uri = URI.parse(TRANSLATE_URI)
|
27
|
+
params = {
|
28
|
+
'to' => to,
|
29
|
+
'from' => from,
|
30
|
+
'text' => text,
|
31
|
+
'appId' => @api_key,
|
32
|
+
'category' => 'general',
|
33
|
+
'contentType' => 'text/plain'
|
34
|
+
}
|
35
|
+
params_s = params.map {|key, value| "#{key}=#{value}"}.join '&'
|
36
|
+
result = Net::HTTP.new(uri.host, uri.port)
|
37
|
+
result = result.get("#{uri.path}?#{params_s}")
|
38
|
+
noko = Nokogiri.parse(result.body)
|
39
|
+
noko.xpath("//xmlns:string")[0].content
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bing_translator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ricky Elrod
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-18 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Translate strings using the Bing HTTP API. Requires that you have an API key. See http://www.microsoft.com/web/post/using-the-free-bing-translation-apis
|
22
|
+
email: ricky@elrod.me
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/bing_translator.rb
|
31
|
+
homepage:
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.11
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Translate using the Bing HTTP API
|
64
|
+
test_files: []
|
65
|
+
|
66
|
+
has_rdoc:
|