yandex-translator 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1de77f641e2fa9133334e7e60841e6af74c22153
4
- data.tar.gz: 996c9c7c4195d9f40ea4f77c36407b684a3f4a63
3
+ metadata.gz: 74895efb7c9206b03ded8f1927b9512a0d69af21
4
+ data.tar.gz: 6be39b5b2a8d6acb79054df43a2732a1dfc6471b
5
5
  SHA512:
6
- metadata.gz: 206cd4c86dc0c21ee5d03cdae3506c97daeb22ff92c05c536cae6b0b7051db7c7f929bfa8ea654914858496207c50153f1ada8571fa87e84beceafe2ec997d16
7
- data.tar.gz: 1ec94ecd1aa8ee8f838f43a6cf35a2354a798ca3adbb206fdc2236ed28b7edaef41825f9b10f4c998486fb56aa9b16e25047cefdba10daa006d81a7ebc76e7e2
6
+ metadata.gz: 9049203db833860d1bebd8c2a6e102551c4d9107b271144f70149ebe15b5f925b4a6ee4e6d98153dd5180f8107d6c2c3720ef1a95398e35863f2b2df7b283a46
7
+ data.tar.gz: 19e47931a2437cfcb2c76f1ced5c8c7c9f55ee56817255c6b63ae8d5ea8a2549d901a6b394dcd1e3927aac710999693043d03d80e4b4145892028b9f653ed5f3
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .swp
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Code Climate](https://codeclimate.com/repos/5255368456b102667402265a/badges/cc0cf62aa85959503fc1/gpa.png)](https://codeclimate.com/repos/5255368456b102667402265a/feed)
2
+
1
3
  # Yandex::Translator
2
4
 
3
5
  Library for Yandex Translate API | Библиотека для API Яндекс.Переводчика
@@ -19,28 +21,34 @@ Or install it yourself as:
19
21
 
20
22
  ## Usage
21
23
 
22
- First, set your api key:
24
+ First, create translator using your api key:
25
+
26
+ ```ruby
27
+ translator = Yandex::Translator.new('your.key')
28
+ ```
29
+
30
+ To get list of possible translation directions use #langs method:
23
31
 
24
32
  ```ruby
25
- Yandex::Translator.set_api_key('API_KEY')
33
+ translator.langs
26
34
  ```
27
35
 
28
36
  To determine language text use detect method:
29
37
 
30
38
  ```ruby
31
- Yandex::Translator.detect('Hello, world!')
39
+ translator.detect 'Hello, world!'
32
40
  ```
33
41
  To translate text use translate method:
34
42
 
35
43
  ```ruby
36
- Yandex::Translator.translate('Car', 'ru')
44
+ translator.translate 'Car', from: 'ru'
37
45
  ```
38
46
 
39
47
  In this case Yandex automatically detect text language.
40
48
  If you want to set text language manually add third parameter
41
49
 
42
50
  ```ruby
43
- Yandex::Translator.translate('Car', 'ru', 'en')
51
+ translator.translate 'Car', from: 'ru', to: 'en'
44
52
  ```
45
53
 
46
54
  ## Contributing
@@ -1,84 +1,7 @@
1
- # encoding: UTF-8
2
-
3
- require "yandex-translator/version"
4
- require "httparty"
1
+ require 'yandex/translator/version'
2
+ require 'httparty'
3
+ require 'yandex/translator'
5
4
 
6
5
  module Yandex
7
- module Translator
8
-
9
- class TranslationError < StandardError; end
10
- class ApiError < StandardError; end
11
-
12
- class Translation
13
- include HTTParty
14
- base_uri 'https://translate.yandex.net/api/v1.5/tr'
15
-
16
-
17
- def self.api_key=(key)
18
- @@api_key = key
19
- end
20
-
21
- def detect_lang(text)
22
- options = {}
23
- options[:query] = { :text => text, :key => @@api_key }
24
- language = self.class.get("/detect", options)
25
- if language["DetectedLang"]
26
- language["DetectedLang"]["lang"]
27
- else
28
- check_api_errors(language)
29
- end
30
- end
31
-
32
- def translate(text, to_language, from_language)
33
- lang = from_language.nil? ? to_language : "#{from_language}-#{to_language}"
34
- options = {}
35
- options[:query] = { :lang => lang, :text => text, :key => @@api_key }
36
- translation = self.class.get("/translate", options)
37
- raise TranslationError.new("Can't translate text to #{to_language}") if translation["Translation"].nil?
38
- case translation["Translation"]["code"].to_i
39
- when 200
40
- translation["Translation"]["text"]
41
- when 413
42
- raise TranslationError.new("Text too long")
43
- when 422
44
- raise TranslationError.new("Can't translate text")
45
- when 501
46
- raise TranslationError.new("Can't translate text to #{to_language}")
47
- else
48
- check_api_errors(translation)
49
- end
50
- end
51
-
52
- private
53
-
54
- def check_api_errors(responce)
55
- case responce["Translation"]["code"].to_i
56
- when 401
57
- ApiError.new("Invalid api key")
58
- when 402
59
- ApiError.new("Api key blocked")
60
- when 403
61
- ApiError.new("Daily request limit exceeded")
62
- when 404
63
- ApiError.new("Daily char limit exceeded")
64
- else
65
- raise TranslationError.new("Try again later")
66
- end
67
- end
68
-
69
- end
70
-
71
- def self.set_api_key(key)
72
- Translation.api_key = key
73
- end
74
-
75
- def self.detect(text = '')
76
- Translation.new.detect_lang(text)
77
- end
78
-
79
- def self.translate(text, to_language, from_language = nil)
80
- Translation.new.translate(text, to_language, from_language)
81
- end
82
-
83
- end
6
+ class ApiError < StandardError; end
84
7
  end
@@ -0,0 +1,71 @@
1
+ module Yandex
2
+ class Translator
3
+ include HTTParty
4
+ base_uri 'https://translate.yandex.net/api/v1.5/tr.json'
5
+
6
+ attr_reader :api_key
7
+
8
+ class << self # backwards compatability
9
+ attr_reader :api_key, :translator
10
+
11
+ def api_key=(key)
12
+ @api_key = key
13
+ @translator = new(api_key)
14
+ end
15
+
16
+ def get_langs
17
+ translator.get_langs
18
+ end
19
+
20
+ def translate(text, *lang)
21
+ translator.translate(text, *lang)
22
+ end
23
+
24
+ def detect(text)
25
+ translator.detect(text)
26
+ end
27
+
28
+ private :translator
29
+ end
30
+
31
+ def initialize(api_key)
32
+ @api_key = api_key
33
+ end
34
+
35
+ def langs
36
+ @langs ||= visit('/getLangs')['dirs']
37
+ end
38
+ alias_method :get_langs, :langs
39
+
40
+ def detect(text)
41
+ lang = visit('/detect', text: text)['lang']
42
+ lang == '' ? nil : lang
43
+ end
44
+
45
+ def translate(text, *lang)
46
+ if lang.last.is_a?(Hash)
47
+ lang_options = lang.last
48
+ lang = [lang_options[:to], lang_options[:from]].compact
49
+ end
50
+
51
+ options = { text: text, lang: lang.reverse.join('-') }
52
+
53
+ result = visit('/translate', options)['text']
54
+
55
+ result.size == 1 ? result.first : result
56
+ end
57
+
58
+ def visit(address, options = {})
59
+ response = self.class.post address, body: options.merge(key: api_key)
60
+ check_errors(response)
61
+ response
62
+ end
63
+
64
+ private
65
+
66
+ def check_errors(response)
67
+ return if response['code'].to_i == 200
68
+ fail ApiError, response['message']
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ module Yandex
2
+ class Translator
3
+ VERSION = '0.3.0'
4
+ end
5
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yandex::Translator do
4
+ let(:api_key) { 'secret' }
5
+
6
+ subject(:translator) { Yandex::Translator.new(api_key) }
7
+
8
+ describe '::translate' do
9
+ before do
10
+ expect_any_instance_of(Yandex::Translator).to receive(:translate)
11
+ Yandex::Translator.api_key = api_key
12
+ end
13
+
14
+ it 'calls translate method on instace object' do
15
+ Yandex::Translator.translate('Bar', 'ru', 'en')
16
+ end
17
+ end
18
+
19
+ describe '::get_langs' do
20
+ before do
21
+ expect_any_instance_of(Yandex::Translator).to receive(:get_langs)
22
+ Yandex::Translator.api_key = api_key
23
+ end
24
+
25
+ it 'calls translate method on instace object' do
26
+ Yandex::Translator.get_langs
27
+ end
28
+ end
29
+
30
+ describe '::detect' do
31
+ before do
32
+ expect_any_instance_of(Yandex::Translator).to receive(:detect)
33
+ Yandex::Translator.api_key = api_key
34
+ end
35
+
36
+ it 'calls translate method on instace object' do
37
+ Yandex::Translator.detect('Car')
38
+ end
39
+ end
40
+
41
+
42
+
43
+ describe '#translate' do
44
+ let(:translate_url) { 'https://translate.yandex.net/api/v1.5/tr.json/translate' }
45
+ let(:translate_request_body) { "text=Car&lang=ru&key=#{api_key}" }
46
+ let(:translate_response_body) do
47
+ '{"code":200, "lang": "en-ru", "text": ["Автомобиль"]}'
48
+ end
49
+
50
+ let!(:translate_request) do
51
+ stub_request(:post, translate_url)
52
+ .with(body: translate_request_body)
53
+ .to_return(
54
+ body: translate_response_body,
55
+ headers: {'Content-Type' => 'application/json'}
56
+ )
57
+ end
58
+
59
+ it 'triggers correct yandex translation url' do
60
+ translator.translate('Car', 'ru')
61
+ expect(translate_request).to have_been_made.once
62
+ end
63
+
64
+ it 'returns translalation' do
65
+ expect(translator.translate('Car', 'ru')).to eq 'Автомобиль'
66
+ end
67
+
68
+ it 'accepts options' do
69
+ translator.translate('Car', from: :ru)
70
+ expect(translate_request).to have_been_made.once
71
+ end
72
+
73
+ context 'with two languages' do
74
+ let(:translate_request_body) { "text=Car&lang=en-ru&key=#{api_key}" }
75
+
76
+ it 'accepts options' do
77
+ translator.translate('Car', from: :en, to: :ru)
78
+ expect(translate_request).to have_been_made.once
79
+ end
80
+
81
+ it 'accepts languages list' do
82
+ translator.translate('Car', :ru, :en)
83
+ expect(translate_request).to have_been_made.once
84
+ end
85
+ end
86
+
87
+ context 'when server responds with invalid "lang" parameter error' do
88
+ let(:translate_request_body) { "text=Car&lang=ru-ru&key=#{api_key}" }
89
+
90
+ let(:translation_url) do
91
+ 'https://translate.yandex.net/api/v1.5/tr.json/translate?' \
92
+ "key=#{api_key}&lang=ru-ru&text=Car"
93
+ end
94
+
95
+ let(:translate_response_body) do
96
+ '{"code": 401, "message": "API key is invalid"}'
97
+ end
98
+
99
+ it 'returns translation error' do
100
+ expect{
101
+ translator.translate('Car', 'ru', 'ru')
102
+ }.to raise_error(Yandex::ApiError, "API key is invalid")
103
+ end
104
+ end
105
+ end
106
+
107
+ describe '#detect' do
108
+ let(:detect_url) { "https://translate.yandex.net/api/v1.5/tr.json/detect" }
109
+ let(:detect_request_body) { "text=Car&key=#{api_key}" }
110
+ let(:decect_response_body) { '{"code": 200, "lang": "en"}' }
111
+ let!(:detect_request) do
112
+ stub_request(:post, detect_url)
113
+ .with(body: detect_request_body)
114
+ .to_return(
115
+ body: decect_response_body,
116
+ headers: { 'Content-Type' => 'application/json' }
117
+ )
118
+ end
119
+
120
+ it 'hits correct url' do
121
+ translator.detect('Car')
122
+ expect(detect_request).to have_been_made.once
123
+ end
124
+
125
+ it 'returns detected language' do
126
+ expect(translator.detect('Car')).to eq 'en'
127
+ end
128
+
129
+ context 'when response does not contains any language' do
130
+ let(:decect_response_body) { '{"code": 200, "lang": ""}' }
131
+
132
+ it 'returns nil' do
133
+ expect(translator.detect('Car')).to be_nil
134
+ end
135
+ end
136
+ end
137
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,5 @@ require 'webmock/rspec'
4
4
  include WebMock
5
5
 
6
6
  RSpec.configure do |config|
7
- config.color_enabled = true
8
- config.formatter = 'documentation'
9
- end
7
+
8
+ end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'yandex-translator/version'
4
+ require 'yandex/translator/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "yandex-translator"
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.add_dependency "httparty"
16
- gem.add_development_dependency 'rspec'
16
+ gem.add_development_dependency 'rspec', '~> 3.1'
17
17
  gem.add_development_dependency 'webmock'
18
18
 
19
19
  gem.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Egorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-19 00:00:00.000000000 Z
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: webmock
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Library for Yandex Translate API
@@ -59,15 +59,16 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".gitignore"
63
63
  - Gemfile
64
64
  - LICENSE.txt
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/yandex-translator.rb
68
- - lib/yandex-translator/version.rb
68
+ - lib/yandex/translator.rb
69
+ - lib/yandex/translator/version.rb
70
+ - spec/lib/yandex/translator_spec.rb
69
71
  - spec/spec_helper.rb
70
- - spec/yandex-translator_spec.rb
71
72
  - yandex-translator.gemspec
72
73
  homepage: ''
73
74
  licenses: []
@@ -78,21 +79,20 @@ require_paths:
78
79
  - lib
79
80
  required_ruby_version: !ruby/object:Gem::Requirement
80
81
  requirements:
81
- - - '>='
82
+ - - ">="
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
84
85
  required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  requirements:
86
- - - '>='
87
+ - - ">="
87
88
  - !ruby/object:Gem::Version
88
89
  version: '0'
89
90
  requirements: []
90
91
  rubyforge_project:
91
- rubygems_version: 2.0.0.rc.2
92
+ rubygems_version: 2.4.1
92
93
  signing_key:
93
94
  specification_version: 4
94
95
  summary: Yandex Translate API
95
96
  test_files:
97
+ - spec/lib/yandex/translator_spec.rb
96
98
  - spec/spec_helper.rb
97
- - spec/yandex-translator_spec.rb
98
- has_rdoc:
@@ -1,5 +0,0 @@
1
- module Yandex
2
- module Translator
3
- VERSION = "0.1.0"
4
- end
5
- end
@@ -1,59 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Yandex::Translator do
6
-
7
- before do
8
- Yandex::Translator.set_api_key(ENV['API_KEY'])
9
- end
10
-
11
- it "shoud return right translation" do
12
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/translate?key=#{ENV['API_KEY']}&lang=ru&text=Car").
13
- to_return(:body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Translation code=\"200\" lang=\"en-ru\"><text>Автомобиль</text></Translation>")
14
- Yandex::Translator.translate("Car", "ru").should == "Автомобиль"
15
- end
16
-
17
- it "should return translation error when translate to the same language" do
18
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/translate?key=#{ENV['API_KEY']}&lang=ru-ru&text=Car").
19
- to_return(:body => "TrService1: Invalid parameter 'lang'" )
20
- expect{
21
- Yandex::Translator.translate("Car", "ru", "ru")
22
- }.to raise_error(Yandex::Translator::TranslationError, "Can't translate text to ru")
23
- end
24
-
25
- it "should return translation error when translate from unknown language" do
26
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/translate?key=#{ENV['API_KEY']}&lang=unknown-ru&text=Car").
27
- to_return(:body => "TrService1: Invalid parameter 'lang'" )
28
- expect{
29
- Yandex::Translator.translate("Car", "ru", "unknown")
30
- }.to raise_error(Yandex::Translator::TranslationError, "Can't translate text to ru")
31
- end
32
-
33
- it "should return translation error when translate to unknown language" do
34
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/translate?key=#{ENV['API_KEY']}&lang=ru-unknown&text=Car").
35
- to_return(:body => "TrService1: Invalid parameter 'lang'" )
36
- expect{
37
- Yandex::Translator.translate("Car", "unknown", "ru")
38
- }.to raise_error(Yandex::Translator::TranslationError, "Can't translate text to unknown")
39
- end
40
-
41
- it "shoud detect en language" do
42
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/detect?key=#{ENV['API_KEY']}&text=Car").
43
- to_return(:body => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<DetectedLang code=\"200\" lang=\"en\"/>" )
44
- Yandex::Translator.detect("Car").should == "en"
45
- end
46
-
47
- it "shoud detect ru language" do
48
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/detect?key=#{ENV['API_KEY']}&text=Автомобиль").
49
- to_return(:body => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<DetectedLang code=\"200\" lang=\"ru\"/>" )
50
- Yandex::Translator.detect("Автомобиль").should == "ru"
51
- end
52
-
53
- it "shoud not detect any language" do
54
- stub_request(:get, "https://translate.yandex.net/api/v1.5/tr/detect?key=#{ENV['API_KEY']}&text=CarАвто").
55
- to_return(:body => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<DetectedLang code=\"200\" lang=\"\"/>" )
56
- Yandex::Translator.detect("CarАвто").should be_nil
57
- end
58
-
59
- end