bing_translator 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/bing_translator.rb +38 -21
  2. metadata +22 -7
@@ -9,32 +9,49 @@ require 'nokogiri'
9
9
 
10
10
  class BingTranslator
11
11
  TRANSLATE_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Translate'
12
+ DETECT_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Detect'
12
13
 
13
14
  def initialize(api_key)
14
15
  @api_key = api_key
16
+ @translate_uri = URI.parse TRANSLATE_URI
17
+ @detect_uri = URI.parse DETECT_URI
15
18
  end
16
19
 
17
20
  def translate(text, params = {})
18
- if params[:to].nil? or params[:from].nil?
19
- raise "Must provide :to and :from."
20
- else
21
- to = CGI.escape params[:to].to_s
22
- from = CGI.escape params[:from].to_s
23
- text = CGI.escape text.to_s
24
- uri = URI.parse(TRANSLATE_URI)
25
- params = {
26
- 'to' => to,
27
- 'from' => from,
28
- 'text' => text,
29
- 'appId' => @api_key,
30
- 'category' => 'general',
31
- 'contentType' => 'text/plain'
32
- }
33
- params_s = params.map {|key, value| "#{key}=#{value}"}.join '&'
34
- result = Net::HTTP.new(uri.host, uri.port)
35
- result = result.get("#{uri.path}?#{params_s}")
36
- noko = Nokogiri.parse(result.body)
37
- noko.xpath("//xmlns:string")[0].content
38
- end
21
+ raise "Must provide :to." if params[:to].nil?
22
+
23
+ from = CGI.escape params[:from].to_s
24
+ params = {
25
+ 'to' => CGI.escape(params[:to].to_s),
26
+ 'text' => CGI.escape(text.to_s),
27
+ 'appId' => @api_key,
28
+ 'category' => 'general',
29
+ 'contentType' => 'text/plain'
30
+ }
31
+ params[:from] = from unless from.empty?
32
+ result = result @translate_uri, params
33
+
34
+ Nokogiri.parse(result.body).xpath("//xmlns:string")[0].content
35
+ end
36
+
37
+ def detect(text)
38
+ params = {
39
+ 'text' => CGI.escape(text.to_s),
40
+ 'appId' => @api_key,
41
+ 'category' => 'general',
42
+ 'contentType' => 'text/plain'
43
+ }
44
+ result = result @detect_uri, params
45
+
46
+ Nokogiri.parse(result.body).xpath("//xmlns:string")[0].content.to_sym
47
+ end
48
+
49
+ private
50
+ def prepare_param_string(params)
51
+ params.map {|key, value| "#{key}=#{value}"}.join '&'
52
+ end
53
+
54
+ def result(uri, params)
55
+ result = Net::HTTP.new(uri.host, uri.port).get("#{uri.path}?#{prepare_param_string(params)}")
39
56
  end
40
57
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bing_translator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ricky Elrod
@@ -15,9 +15,24 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-18 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2011-12-07 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 1
31
+ - 5
32
+ - 0
33
+ version: 1.5.0
34
+ type: :runtime
35
+ version_requirements: *id001
21
36
  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
37
  email: ricky@elrod.me
23
38
  executables: []
@@ -60,7 +75,7 @@ rubyforge_project:
60
75
  rubygems_version: 1.8.11
61
76
  signing_key:
62
77
  specification_version: 3
63
- summary: Remove the need for some un-necessary, always-there keys.
78
+ summary: Translate using the Bing HTTP API
64
79
  test_files: []
65
80
 
66
81
  has_rdoc: