yandex-translator 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -19,7 +19,7 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- To detect language text use detect method:
22
+ To determine language text use detect method:
23
23
 
24
24
  ```ruby
25
25
  Yandex::Translator.detect('Hello, world!')
@@ -27,9 +27,15 @@ To detect language text use detect method:
27
27
  To translate text use translate method:
28
28
 
29
29
  ```ruby
30
- Yandex::Translator.translate('ru', 'Car')
30
+ Yandex::Translator.translate('Car', 'ru')
31
31
  ```
32
32
 
33
+ In this case Yandex automatically detect text language.
34
+ If you want to set text language manually add third paramater
35
+
36
+ ```ruby
37
+ Yandex::Translator.translate('Car', 'ru', 'en')
38
+ ```
33
39
 
34
40
  ## Contributing
35
41
 
data/Rakefile CHANGED
@@ -1 +1,3 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  require "yandex-translator/version"
2
4
  require "httparty"
3
5
 
@@ -18,10 +20,12 @@ module Yandex
18
20
  language["DetectedLang"]["lang"]
19
21
  end
20
22
 
21
- def translate(text, language)
23
+ def translate(text, to_language, from_language)
24
+ lang = from_language.nil? ? to_language : "#{from_language}-#{to_language}"
22
25
  options = {}
23
- options[:query] = { :lang => language, :text => text }
26
+ options[:query] = { :lang => lang, :text => text }
24
27
  translation = self.class.get("/translate", options)
28
+ raise TranslationError.new("Can't translate text to #{to_language}") if translation["Translation"].nil?
25
29
  case translation["Translation"]["code"].to_i
26
30
  when 200
27
31
  translation["Translation"]["text"]
@@ -30,7 +34,7 @@ module Yandex
30
34
  when 422
31
35
  raise TranslationError.new("Can't translate text")
32
36
  when 501
33
- raise TranslationError.new("Can't translate to this language")
37
+ raise TranslationError.new("Can't translate text to #{to_language}")
34
38
  else
35
39
  raise TranslationError.new("Try again later")
36
40
  end
@@ -38,13 +42,12 @@ module Yandex
38
42
 
39
43
  end
40
44
 
41
-
42
45
  def self.detect(text = '')
43
46
  Translation.new.detect_lang(text)
44
47
  end
45
48
 
46
- def self.translate(text = '', language)
47
- Translation.new.translate(text,language)
49
+ def self.translate(text, to_language, from_language = nil)
50
+ Translation.new.translate(text, to_language, from_language)
48
51
  end
49
52
 
50
53
  end
@@ -1,5 +1,5 @@
1
1
  module Yandex
2
2
  module Translator
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+ require 'yandex-translator'
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ config.formatter = 'documentation'
9
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Yandex::Translator do
6
+
7
+ it "shoud return right translation" do
8
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/translate?lang=ru&text=Car").
9
+ to_return(:body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Translation code=\"200\" lang=\"en-ru\"><text>Автомобиль</text></Translation>")
10
+ Yandex::Translator.translate("Car", "ru").should == "Автомобиль"
11
+ end
12
+
13
+ it "should return translation error when translate to the same language" do
14
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/translate?lang=ru-ru&text=Car").
15
+ to_return(:body => "TrService1: Invalid parameter 'lang'" )
16
+ expect{
17
+ Yandex::Translator.translate("Car", "ru", "ru")
18
+ }.to raise_error(Yandex::Translator::TranslationError, "Can't translate text to ru")
19
+ end
20
+
21
+ it "should return translation error when translate from unknown language" do
22
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/translate?lang=unknown-ru&text=Car").
23
+ to_return(:body => "TrService1: Invalid parameter 'lang'" )
24
+ expect{
25
+ Yandex::Translator.translate("Car", "ru", "unknown")
26
+ }.to raise_error(Yandex::Translator::TranslationError, "Can't translate text to ru")
27
+ end
28
+
29
+ it "should return translation error when translate to unknown language" do
30
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/translate?lang=ru-unknown&text=Car").
31
+ to_return(:body => "TrService1: Invalid parameter 'lang'" )
32
+ expect{
33
+ Yandex::Translator.translate("Car", "unknown", "ru")
34
+ }.to raise_error(Yandex::Translator::TranslationError, "Can't translate text to unknown")
35
+ end
36
+
37
+ it "shoud detect en language" do
38
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/detect?text=Car").
39
+ to_return(:body => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<DetectedLang code=\"200\" lang=\"en\"/>" )
40
+ Yandex::Translator.detect("Car").should == "en"
41
+ end
42
+
43
+ it "shoud detect ru language" do
44
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/detect?text=Автомобиль").
45
+ to_return(:body => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<DetectedLang code=\"200\" lang=\"ru\"/>" )
46
+ Yandex::Translator.detect("Автомобиль").should == "ru"
47
+ end
48
+
49
+ it "shoud not detect any language" do
50
+ stub_request(:get, "http://translate.yandex.net/api/v1/tr/detect?text=CarАвто").
51
+ to_return(:body => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<DetectedLang code=\"200\" lang=\"\"/>" )
52
+ Yandex::Translator.detect("CarАвто").should be_nil
53
+ end
54
+
55
+ end
@@ -13,6 +13,8 @@ Gem::Specification.new do |gem|
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.add_dependency "httparty"
16
+ gem.add_development_dependency 'rspec'
17
+ gem.add_development_dependency 'webmock'
16
18
 
17
19
  gem.files = `git ls-files`.split($/)
18
20
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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: 2013-02-04 00:00:00.000000000 Z
12
+ date: 2013-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &70105306721240 !ruby/object:Gem::Requirement
16
+ requirement: &70146220573360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,29 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70105306721240
24
+ version_requirements: *70146220573360
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70146220572580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70146220572580
36
+ - !ruby/object:Gem::Dependency
37
+ name: webmock
38
+ requirement: &70146220571560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70146220571560
25
47
  description: Library for Yandex Translate API
26
48
  email:
27
49
  - artur@egorov.in
@@ -36,6 +58,8 @@ files:
36
58
  - Rakefile
37
59
  - lib/yandex-translator.rb
38
60
  - lib/yandex-translator/version.rb
61
+ - spec/spec_helper.rb
62
+ - spec/yandex-translator_spec.rb
39
63
  - yandex-translator.gemspec
40
64
  homepage: ''
41
65
  licenses: []
@@ -61,4 +85,7 @@ rubygems_version: 1.8.10
61
85
  signing_key:
62
86
  specification_version: 3
63
87
  summary: Yandex Translate API
64
- test_files: []
88
+ test_files:
89
+ - spec/spec_helper.rb
90
+ - spec/yandex-translator_spec.rb
91
+ has_rdoc: