nele 0.2.2

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/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem "json", "1.4.6"
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ Nele translates ruby string to the other language.
2
+
3
+ It uses external public translators like Microsoft Translator or Yahoo's Babelfish.
4
+
5
+ ### Installation:
6
+
7
+ git install nele
8
+
9
+ ### Usage:
10
+
11
+ require 'nele'
12
+
13
+ String.translators
14
+ => [:babelfish, :ms]
15
+
16
+ ### Microsoft Translator:
17
+
18
+ String.translator = :ms
19
+ String.translator.config[:from] = "en"
20
+ String.translator.config[:to] = "pl"
21
+ String.translator.config[:appId] = YOUR_APP_ID
22
+
23
+ "nice girl".translate
24
+ => "mila dziewczyna"
25
+
26
+ ### Yahoo's Babelfish:
27
+
28
+ String.translator = :babelfish
29
+ String.translator.config[:lp] = "en_es"
30
+
31
+ "hello".translate
32
+ => "hola"
33
+
34
+ If you know any other (free) public translators just let me know, or write your own adapter for nele.
35
+
@@ -0,0 +1,37 @@
1
+ Nele translates ruby string to the other language.
2
+
3
+ It uses external public translators like Microsoft Translator or Yahoo's Babelfish.
4
+
5
+ ### Installation:
6
+ git clone git@github.com:cfx/nele.git
7
+ cd nele
8
+ gem build nele.gemspec
9
+ gem install nele-0.x.x.gem
10
+
11
+ ### Usage:
12
+
13
+ require 'nele'
14
+
15
+ String.translators
16
+ => [:babelfish, :ms]
17
+
18
+ ### Microsoft Translator:
19
+
20
+ String.translator = :ms
21
+ String.translator.config[:from] = "en"
22
+ String.translator.config[:to] = "pl"
23
+ String.translator.config[:appId] = YOUR_APP_ID
24
+
25
+ "nice girl".translate
26
+ => "mila dziewczyna"
27
+
28
+ ### Yahoo's Babelfish:
29
+
30
+ String.translator = :babelfish
31
+ String.translator.config[:lp] = "en_es"
32
+
33
+ "hello".translate
34
+ => "hola"
35
+
36
+ If you know any other (free) public translators just let me know, or write your own adapter for nele.
37
+
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |i|
4
+ i.test_files = FileList['test/*_test.rb']
5
+ i.verbose = true
6
+ end
7
+
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'json'
5
+ require 'uri'
6
+ require 'yaml'
7
+ require 'nele/string_ext'
8
+ require 'nele/generic_translator'
9
+ require 'nele/ms_translator'
10
+ require 'nele/babelfish_translator'
11
+
12
+
13
+ module Nele
14
+ class TranslatorInstanceError < StandardError; end
15
+ class TranslatorMissingMethodError < StandardError; end
16
+ class TranslatorMissingError < StandardError; end
17
+ class TranslatorInvalidError < StandardError; end
18
+ class EmptyStringError < StandardError; end
19
+ class TranslatorAPIError < StandardError; end
20
+ class ConnectionError < StandardError; end
21
+ end
22
+
23
+
24
+
25
+
26
+ String.send(:extend, Nele::StringExt::ClassMethods)
27
+ String.send(:include, Nele::StringExt::InstanceMethods)
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'json'
5
+ require 'uri'
6
+ require 'yaml'
7
+ require 'ruby-debug'
8
+ require 'nele/string_ext'
9
+ require 'nele/generic_translator'
10
+ require 'nele/ms_translator'
11
+ require 'nele/babelfish_translator'
12
+
13
+
14
+ module Nele
15
+ class TranslatorInstanceError < StandardError; end
16
+ class TranslatorMissingMethodError < StandardError; end
17
+ class TranslatorMissingError < StandardError; end
18
+ class TranslatorInvalidError < StandardError; end
19
+ class EmptyStringError < StandardError; end
20
+ class TranslatorAPIError < StandardError; end
21
+ class ConnectionError < StandardError; end
22
+ end
23
+
24
+
25
+
26
+
27
+ String.send(:extend, Nele::StringExt::ClassMethods)
28
+ String.send(:include, Nele::StringExt::InstanceMethods)
@@ -0,0 +1,42 @@
1
+ module Nele
2
+ class BabelfishTranslator < Nele::GenericTranslator
3
+ attr_reader :post_form
4
+
5
+ def build_request(str)
6
+ @uri = parse_uri(config[:url])
7
+ @post_form = create_post_form(str)
8
+ end
9
+
10
+ def connect
11
+ begin
12
+ Net::HTTP.post_form(uri, post_form)
13
+ rescue => err
14
+ raise Nele::ConnectionError, err
15
+ end
16
+ end
17
+
18
+ def parse_response(res)
19
+ body = res.body
20
+ tag = body.match(/<div\sid=\"result\"[^>]*>(.*?)<\/div><\/div>/)
21
+ if tag && tag.length > 0
22
+ tag[1].match(/>(.*)$/)[0].gsub(">", "")
23
+ else
24
+ raise TranslatorAPIError, "error"
25
+ end
26
+ end
27
+
28
+ private
29
+ def create_post_form(str)
30
+ {
31
+ 'ei' => 'UTF-8',
32
+ 'doit' => 'done',
33
+ 'fr' => 'bf-res',
34
+ 'intl' => 1,
35
+ 'tt' => 'urltext',
36
+ 'trtext' => str,
37
+ 'lp' => config[:lp],
38
+ 'btnTrTxt' => 'Translate'
39
+ }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ module Nele
2
+ class BabelfishTranslator < Nele::GenericTranslator
3
+ attr_reader :post_form
4
+ def config
5
+ { :url => "http://babelfish.yahoo.com/translate_txt",
6
+ :lp => "en_es" }
7
+ end
8
+
9
+ def build_request(str)
10
+ @uri = parse_uri(config[:url])
11
+ @post_form = create_post_form(str)
12
+ end
13
+
14
+ def connect
15
+ begin
16
+ Net::HTTP.post_form(uri, post_form)
17
+ rescue => err
18
+ raise Nele::ConnectionError, err
19
+ end
20
+ end
21
+
22
+ def parse_response(res)
23
+ body = res.body
24
+ debugger
25
+ tag = body.match(/<div\sid=\"result\"[^>]*>(.*?)<\/div><\/div>/)
26
+ if tag && tag.length > 0
27
+ tag[1].match(/>(.*)$/)[0].gsub(">", "")
28
+ else
29
+ raise TranslatorAPIError, "error"
30
+ end
31
+ end
32
+
33
+ private
34
+ def create_post_form(str)
35
+ {
36
+ 'ei' => 'UTF-8',
37
+ 'doit' => 'done',
38
+ 'fr' => 'bf-res',
39
+ 'intl' => 1,
40
+ 'tt' => 'urltext',
41
+ 'trtext' => str,
42
+ 'lp' => config[:lp],
43
+ 'btnTrTxt' => 'Translate'
44
+ }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,58 @@
1
+ module Nele
2
+ class GenericTranslator
3
+ attr_reader :uri
4
+ attr_accessor :config
5
+
6
+ def config
7
+ @config ||= {}
8
+ end
9
+
10
+ def initialize
11
+ validate_children()
12
+ end
13
+
14
+ def parse_uri(uri)
15
+ URI.parse(uri.gsub(" ", "+"))
16
+ end
17
+
18
+ def name
19
+ klass_name = self.class.to_s.split("::")[1]
20
+ @name ||= klass_name.split('Translator')[0].downcase
21
+ end
22
+
23
+ def translate(str)
24
+ build_request(str)
25
+ res = connect()
26
+ parse_response(res)
27
+ end
28
+
29
+ class << self
30
+ def build_translator_class_name(name)
31
+ klass_name = name.to_s.capitalize + "Translator"
32
+ begin
33
+ Nele.const_get(klass_name)
34
+ rescue NameError
35
+ raise(::Nele::TranslatorInvalidError, "invalid translator")
36
+ end
37
+ end
38
+
39
+ def check_inheritance(klass)
40
+ raise(::Nele::TranslatorMissingError,
41
+ "translator should inherit from GenericTranslator"
42
+ ) if klass.superclass != Nele::GenericTranslator
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def validate_children
49
+ unless self.class == Nele::GenericTranslator
50
+ [:build_request, :connect, :parse_response, :config].each do |m|
51
+ raise(Nele::TranslatorMissingMethodError,
52
+ "#{m.to_s} method is required") unless respond_to?(m)
53
+ end
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,57 @@
1
+ module Nele
2
+ class GenericTranslator
3
+ attr_reader :config, :uri
4
+
5
+ def config
6
+ @config ||= {}
7
+ end
8
+
9
+ def initialize
10
+ validate_children()
11
+ end
12
+
13
+ def parse_uri(uri)
14
+ URI.parse(uri.gsub(" ", "+"))
15
+ end
16
+
17
+ def name
18
+ klass_name = self.class.to_s.split("::")[1]
19
+ @name ||= klass_name.split('Translator')[0].downcase
20
+ end
21
+
22
+ def translate(str)
23
+ build_request(str)
24
+ res = connect()
25
+ parse_response(res)
26
+ end
27
+
28
+ class << self
29
+ def build_translator_class_name(name)
30
+ klass_name = name.to_s.capitalize + "Translator"
31
+ begin
32
+ Nele.const_get(klass_name)
33
+ rescue NameError
34
+ raise(::Nele::TranslatorInvalidError, "invalid translator")
35
+ end
36
+ end
37
+
38
+ def check_inheritance(klass)
39
+ raise(::Nele::TranslatorMissingError,
40
+ "translator should inherit from GenericTranslator"
41
+ ) if klass.superclass != Nele::GenericTranslator
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def validate_children
48
+ unless self.class == Nele::GenericTranslator
49
+ [:build_request, :connect, :parse_response, :config].each do |m|
50
+ raise(Nele::TranslatorMissingMethodError,
51
+ "#{m.to_s} method is required") unless respond_to?(m)
52
+ end
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,45 @@
1
+ require 'rexml/document'
2
+
3
+ module Nele
4
+ class MsTranslator < Nele::GenericTranslator
5
+ def build_request(str)
6
+ @uri = parse_uri(build_uri(str))
7
+ end
8
+
9
+ def build_uri(str)
10
+ config[:url] +
11
+ "?appId=" + config[:appId] +
12
+ "&text=" + str +
13
+ "&from=" + config[:from] +
14
+ "&to=" + config[:to]
15
+ end
16
+
17
+ def connect
18
+ begin
19
+ http = Net::HTTP.new(uri.host, uri.port)
20
+ http.use_ssl = false
21
+ request = Net::HTTP::Get.new(uri.request_uri)
22
+ http.request(request).body
23
+ rescue => err
24
+ raise Nele::ConnectionError, err
25
+ end
26
+ end
27
+
28
+ def parse_response(res)
29
+ doc = REXML::Document.new(res)
30
+ unless doc.elements['string']
31
+ error_message = nil
32
+ doc.elements.each("//p") do |e|
33
+ str = e[0].to_s
34
+ if str.index(":")
35
+ k,v = str.split(":")
36
+ raise TranslatorAPIError, v.strip if k == "Message"
37
+ end
38
+ end
39
+ else
40
+ doc.elements['string'][0].to_s
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,50 @@
1
+ require 'rexml/document'
2
+
3
+ module Nele
4
+ class MsTranslator < Nele::GenericTranslator
5
+ def config
6
+ @config ||= {
7
+ }
8
+ end
9
+
10
+ def build_request(str)
11
+ @uri = parse_uri(build_uri(str))
12
+ end
13
+
14
+ def build_uri(str)
15
+ config[:url] +
16
+ "?appId=" + config[:appId] +
17
+ "&text=" + str +
18
+ "&from=" + config[:from] +
19
+ "&to=" + config[:to]
20
+ end
21
+
22
+ def connect
23
+ begin
24
+ http = Net::HTTP.new(@uri.host, @uri.port)
25
+ http.use_ssl = false
26
+ request = Net::HTTP::Get.new(@uri.request_uri)
27
+ http.request(request).body
28
+ rescue => err
29
+ raise Nele::ConnectionError, err
30
+ end
31
+ end
32
+
33
+ def parse_response(res)
34
+ doc = REXML::Document.new(res)
35
+ unless doc.elements['string']
36
+ error_message = nil
37
+ doc.elements.each("//p") do |e|
38
+ str = e[0].to_s
39
+ if str.index(":")
40
+ k,v = str.split(":")
41
+ raise TranslatorAPIError, v.strip if k == "Message"
42
+ end
43
+ end
44
+ else
45
+ doc.elements['string'][0].to_s
46
+ end
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,31 @@
1
+ module Nele
2
+ module StringExt
3
+ module InstanceMethods
4
+ def translate
5
+ raise(EmptyStringError, "string is empty") if self.empty?
6
+ raise(
7
+ ::Nele::TranslatorMissingError,
8
+ "missing translator") if self.class.translator.nil?
9
+ String.translator.translate(self)
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ attr_reader(:translator)
15
+
16
+ def translator=(name)
17
+ return @translator = nil if name.nil?
18
+ klass = Nele::GenericTranslator.build_translator_class_name(name)
19
+ Nele::GenericTranslator.check_inheritance(klass)
20
+ @translator = klass.new
21
+ end
22
+
23
+ def translators
24
+ regexp = /^[^Translator|^GenericTranslator|Config|StringExt]/
25
+ Nele.constants.select { |e| e=~regexp }.map do |e|
26
+ e.to_s.gsub("Translator", "").downcase
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Nele
2
+ VERSION = "0.2.2"
3
+ end
@@ -0,0 +1,3 @@
1
+ module Nele
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'nele/version'
5
+ require 'bundler'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.name = "nele"
9
+ s.summary = "string translator"
10
+ s.description= File.read(File.join(File.dirname(__FILE__), 'README.md'))
11
+ s.requirements =
12
+ [ 'An installed dictionary (most Unix systems have one)' ]
13
+ s.version = Nele::VERSION
14
+ s.author = "cfx"
15
+ s.email = "jozef@applicake.com"
16
+ s.homepage = "http://github.com/cfx/nele"
17
+ s.platform = Gem::Platform::RUBY
18
+ s.required_ruby_version = '>=1.8.7'
19
+ s.files = Dir['**/**']
20
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
21
+ s.test_files = Dir["test/test*.rb"]
22
+ s.has_rdoc = false
23
+ s.add_dependency('json', '>= 1.4.6')
24
+ end
@@ -0,0 +1,46 @@
1
+ require "./test/tests_helper"
2
+ require "nele"
3
+
4
+ describe "BabelfishTranslator" do
5
+
6
+ def setup
7
+ String.translator = :babelfish
8
+ String.translator.config[:url] = "url"
9
+ String.translator.config[:lp] = "es_en"
10
+ end
11
+
12
+ def subject
13
+ String.translator
14
+ end
15
+
16
+ describe("#build_request") do
17
+ def str
18
+ 'hello'
19
+ end
20
+
21
+ it("invokes /parse_uri/ method") do
22
+ subject.expects(:parse_uri).with(subject.config[:url]).once
23
+ subject.build_request(str)
24
+ end
25
+
26
+ it("invokes /create_post_form/ method") do
27
+ subject.expects(:create_post_form).with(str).once
28
+ subject.build_request(str)
29
+ end
30
+ end
31
+
32
+ describe("#parse_response") do
33
+ it("returns proper translation") do
34
+ html_element = "<div id=\"result\">"\
35
+ "<div style=\"padding:0.6em;\">hola</div></div>"
36
+ mock = mock(:body => html_element)
37
+ subject.parse_response(mock).must_equal "hola"
38
+ end
39
+
40
+ it("raises exception when tag is not found") do
41
+ mock = mock(:body => "some content")
42
+ lambda { subject.parse_response(mock) }.must_raise(Nele::TranslatorAPIError)
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,46 @@
1
+ require "./test/tests_helper"
2
+ require "nele"
3
+
4
+ describe "BabelfishTranslator" do
5
+
6
+ def setup
7
+ String.translator = :babelfish
8
+ String.translator.config[:url] = "url"
9
+ String.translator.config[:lp] = "es_en"
10
+ end
11
+
12
+ def subject
13
+ String.translator
14
+ end
15
+
16
+ describe("#build_request") do
17
+ def str
18
+ 'hello'
19
+ end
20
+
21
+ it("invokes /parse_uri/ method") do
22
+ subject.expects(:parse_uri).with(subject.config[:url]).once
23
+ subject.build_request(str)
24
+ end
25
+
26
+ it("invokes /post_form/ method") do
27
+ subject.expects(:post_form).with(str).once
28
+ subject.build_request(str)
29
+ end
30
+ end
31
+
32
+ describe("#parse_response") do
33
+ it("returns proper translation") do
34
+ html_element = "<div id=\"result\">"\
35
+ "<div style=\"padding:0.6em;\">hola</div></div>"
36
+ mock = mock(:body => html_element)
37
+ subject.parse_response(mock).must_equal "hola"
38
+ end
39
+
40
+ it("raises exception when tag is not found") do
41
+ mock = mock(:body => "some content")
42
+ lambda { subject.parse_response(mock) }.must_raise(Nele::TranslatorAPIError)
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,82 @@
1
+ require "./test/tests_helper"
2
+ require "nele"
3
+
4
+ @custom_translators.call()
5
+
6
+ describe "GenericTranslator" do
7
+ def subject
8
+ Nele::BarTranslator.new
9
+ end
10
+
11
+ describe("#validate_children") do
12
+ before(:each) { remove_required_methods }
13
+
14
+ it("requires /build_request/ method") do
15
+ define_required_methods [:connect, :parse_response]
16
+ lambda { Nele::FooTranslator.new }.must_raise(Nele::TranslatorMissingMethodError)
17
+ end
18
+
19
+ it("requires /connect/ method") do
20
+ define_required_methods [:build_request, :parse_response, :config]
21
+ lambda { Nele::FooTranslator.new }.must_raise(Nele::TranslatorMissingMethodError)
22
+ end
23
+
24
+ it("requires /parse_response/ method") do
25
+ define_required_methods [:build_request, :connect]
26
+ lambda { Nele::FooTranslator.new }.must_raise(Nele::TranslatorMissingMethodError)
27
+ end
28
+
29
+ it("initilizes new translator instance") do
30
+ define_required_methods [:build_request, :connect, :parse_response]
31
+ Nele::FooTranslator.new.class.must_equal Nele::FooTranslator
32
+ end
33
+ end
34
+
35
+ describe("name") do
36
+ it("returns translator name") do
37
+ subject.name.must_equal "bar"
38
+ end
39
+ end
40
+
41
+ describe("#translate method") do
42
+ def setup
43
+ @subject = Nele::GenericTranslator.new
44
+ @subject.stubs(:build_request)
45
+ @subject.stubs(:connect)
46
+ @subject.stubs(:parse_response)
47
+ end
48
+
49
+ it("invokes /build_request/ method") do
50
+ @subject.expects(:build_request).once
51
+ @subject.translate("")
52
+ end
53
+
54
+ it("invokes /connect/ method") do
55
+ @subject.expects(:connect).once
56
+ @subject.translate("")
57
+ end
58
+
59
+ it("invokes /parse_response/ method") do
60
+ res = "res"
61
+ @subject.expects(:parse_response).once
62
+ @subject.translate("")
63
+ end
64
+ end
65
+
66
+ def remove_required_methods(arr=[:build_request, :connect, :parse_response])
67
+ arr.each do |m|
68
+ if Nele::FooTranslator.instance_methods.include?(m)
69
+ Nele::FooTranslator.send(:remove_method, m)
70
+ end
71
+ end
72
+ end
73
+
74
+ def define_required_methods(arr=[])
75
+ arr.each do |m|
76
+ unless Nele::FooTranslator.instance_methods.include?(m)
77
+ Nele::FooTranslator.instance_eval do define_method m do; end; end
78
+ end
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,65 @@
1
+ require "./test/tests_helper"
2
+ require "nele"
3
+
4
+ describe "MsTranslator" do
5
+
6
+ def setup
7
+ String.translator = :ms
8
+ String.translator.config[:url] = "http://example.com/"
9
+ String.translator.config[:appId] = "abc"
10
+ String.translator.config[:to] = "pl"
11
+ String.translator.config[:from] = "en"
12
+ end
13
+
14
+ def subject
15
+ String.translator
16
+ end
17
+
18
+ describe("#build_uri") do
19
+ it("builds proper uri") do
20
+ str = "hello"
21
+ expected_url = "http://example.com/?appId=abc&text=hello&from=en&to=pl"
22
+ subject.build_uri(str).must_equal expected_url
23
+ end
24
+ end
25
+
26
+ describe("#build_request") do
27
+ it("invokes /parse_uri/ method") do
28
+ str = "hello"
29
+ subject.expects(:parse_uri).with(subject.build_uri(str)).once
30
+ subject.build_request(str)
31
+ end
32
+ end
33
+
34
+ describe("when there are no errors from the ms api") do
35
+ it("returns translated string") do
36
+ subject.stubs(:connect).returns ms_response
37
+ "hello world".translate.must_equal "powitania"
38
+ end
39
+ end
40
+
41
+ describe("when api returns errors") do
42
+ it("returns hash with api errors") do
43
+ subject.stubs(:connect).returns ms_response_with_error
44
+ lambda { "hello world".translate }.must_raise(Nele::TranslatorAPIError)
45
+ end
46
+ end
47
+
48
+ describe("when there is connection error") do
49
+ it("invokes /handle_connection/ on rescue") do
50
+ subject.config[:url] = "invalid_url"
51
+ lambda { subject.connect }.must_raise(Nele::ConnectionError)
52
+ end
53
+ end
54
+
55
+ def ms_response
56
+ "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">powitania</string>"
57
+ end
58
+
59
+ def ms_response_with_error
60
+ "<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: appId</p><p>Message: Invalid appId</p><code></code></body></html>"
61
+ end
62
+ end
63
+
64
+
65
+
@@ -0,0 +1,62 @@
1
+ require "./test/tests_helper"
2
+ require "nele"
3
+
4
+ describe "MsTranslator" do
5
+ before(:each) do
6
+ String.translator = :ms
7
+ @subject = String.translator
8
+ end
9
+
10
+ describe("#build_uri") do
11
+ it("builds proper uri") do
12
+ str = "hello"
13
+ @subject.config[:url] = "http://example.com/"
14
+ @subject.config[:appId] = "abc"
15
+ @subject.config[:to] = "pl"
16
+ @subject.config[:from] = "en"
17
+
18
+ expected_url = "http://example.com/?appId=abc&text=hello&from=en&to=pl"
19
+ @subject.build_uri(str).must_equal expected_url
20
+ end
21
+ end
22
+
23
+ describe("#build_request") do
24
+ it("invokes /parse_uri/ method") do
25
+ str = "hello"
26
+ @subject.expects(:parse_uri).with(@subject.build_uri(str)).once
27
+ @subject.build_request(str)
28
+ end
29
+ end
30
+
31
+ describe("when there are no errors from the ms api") do
32
+ it("returns translated string") do
33
+ @subject.stubs(:connect).returns ms_response
34
+ "hello world".translate.must_equal "powitania"
35
+ end
36
+ end
37
+
38
+ describe("when api returns errors") do
39
+ it("returns hash with api errors") do
40
+ @subject.stubs(:connect).returns ms_response_with_error
41
+ lambda { "hello world".translate }.must_raise(Nele::TranslatorAPIError)
42
+ end
43
+ end
44
+
45
+ describe("when there is connection error") do
46
+ it("invokes /handle_connection/ on rescue") do
47
+ @subject.config[:url] = "invalid_url"
48
+ lambda { @subject.connect }.must_raise(Nele::ConnectionError)
49
+ end
50
+ end
51
+
52
+ def ms_response
53
+ "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">powitania</string>"
54
+ end
55
+
56
+ def ms_response_with_error
57
+ "<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: appId</p><p>Message: Invalid appId</p><code></code></body></html>"
58
+ end
59
+ end
60
+
61
+
62
+
@@ -0,0 +1,45 @@
1
+ require "./test/tests_helper"
2
+ require "nele"
3
+
4
+ @custom_translators.call()
5
+
6
+ describe "StringExt" do
7
+
8
+ it("has /translators/ method which returns providers") do
9
+ String.translators.must_equal [ 'ms', 'babelfish', 'foo', 'bar' ]
10
+ end
11
+
12
+ it("responses to translate method") do
13
+ "hello".respond_to?(:translate).must_equal true
14
+ end
15
+
16
+ it("raises TranslatorInvalidError") do
17
+ lambda { String.translator = "blah" }.must_raise(Nele::TranslatorInvalidError)
18
+ end
19
+
20
+ it("raises TranslatorMissingError") do
21
+ String.translator = nil
22
+ lambda { "hello".translate }.must_raise(Nele::TranslatorMissingError)
23
+ end
24
+
25
+ it("raises EmptyStringError") do
26
+ lambda { "".translate }.must_raise(Nele::EmptyStringError)
27
+ end
28
+
29
+ it("initializes translator instance based on string") do
30
+ String.translator = "bar"
31
+ String.translator.class.must_equal Nele::BarTranslator
32
+ end
33
+
34
+ it("initializes translator instance based on symbol") do
35
+ String.translator = :bar
36
+ String.translator.class.must_equal Nele::BarTranslator
37
+ end
38
+
39
+ it("sets translator to nil") do
40
+ String.translator = :bar
41
+ String.translator = nil
42
+ String.translator.class.must_equal NilClass
43
+ end
44
+ end
45
+
@@ -0,0 +1,22 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'minitest/autorun'
4
+ require 'mocha'
5
+
6
+ require 'rubygems'
7
+ require 'ruby-debug'
8
+
9
+ @custom_translators = lambda do
10
+ module Nele
11
+ class FooTranslator < Nele::GenericTranslator; end
12
+ class BarTranslator < Nele::GenericTranslator
13
+ def build_request(param); end
14
+ def connect; end
15
+ def parse_response(param); end
16
+ end
17
+ end
18
+ end
19
+
20
+
21
+
22
+
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nele
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.2
6
+ platform: ruby
7
+ authors:
8
+ - cfx
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-12-13 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ description: |+
27
+ Nele translates ruby string to the other language.
28
+
29
+ It uses external public translators like Microsoft Translator or Yahoo's Babelfish.
30
+
31
+ ### Installation:
32
+
33
+ git install nele
34
+
35
+ ### Usage:
36
+
37
+ require 'nele'
38
+
39
+ String.translators
40
+ => [:babelfish, :ms]
41
+
42
+ ### Microsoft Translator:
43
+
44
+ String.translator = :ms
45
+ String.translator.config[:from] = "en"
46
+ String.translator.config[:to] = "pl"
47
+ String.translator.config[:appId] = YOUR_APP_ID
48
+
49
+ "nice girl".translate
50
+ => "mila dziewczyna"
51
+
52
+ ### Yahoo's Babelfish:
53
+
54
+ String.translator = :babelfish
55
+ String.translator.config[:lp] = "en_es"
56
+
57
+ "hello".translate
58
+ => "hola"
59
+
60
+ If you know any other (free) public translators just let me know, or write your own adapter for nele.
61
+
62
+ email: jozef@applicake.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - Gemfile
71
+ - lib/nele/babelfish_translator.rb
72
+ - lib/nele/babelfish_translator.rb~
73
+ - lib/nele/generic_translator.rb
74
+ - lib/nele/generic_translator.rb~
75
+ - lib/nele/ms_translator.rb
76
+ - lib/nele/ms_translator.rb~
77
+ - lib/nele/string_ext.rb
78
+ - lib/nele/version.rb
79
+ - lib/nele/version.rb~
80
+ - lib/nele.rb
81
+ - lib/nele.rb~
82
+ - LICENSE
83
+ - nele.gemspec
84
+ - Rakefile
85
+ - README.md
86
+ - README.md~
87
+ - test/babelfish_translator_test.rb
88
+ - test/babelfish_translator_test.rb~
89
+ - test/generic_translator_test.rb
90
+ - test/ms_translator_test.rb
91
+ - test/ms_translator_test.rb~
92
+ - test/string_test.rb
93
+ - test/tests_helper.rb
94
+ homepage: http://github.com/cfx/nele
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 1.8.7
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ requirements:
115
+ - An installed dictionary (most Unix systems have one)
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.12
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: string translator
121
+ test_files:
122
+ - test/tests_helper.rb