├── Rakefile ├── lib ├── yandex │ ├── translator │ │ └── version.rb │ └── translator.rb └── yandex-translator.rb ├── Gemfile ├── .gitignore ├── spec ├── spec_helper.rb ├── vcr │ ├── _detect.yml │ ├── wrong_api_key.yml │ ├── get_translated_pl_text.yml │ ├── accepts_options.yml │ ├── get_translated_ru_text.yml │ └── _langs.yml └── lib │ └── yandex │ ├── translator_vcr_spec.rb │ └── translator_spec.rb ├── yandex-translator.gemspec ├── LICENSE.txt └── README.md /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rspec/core/rake_task' 3 | RSpec::Core::RakeTask.new('spec') 4 | -------------------------------------------------------------------------------- /lib/yandex/translator/version.rb: -------------------------------------------------------------------------------- 1 | module Yandex 2 | class Translator 3 | VERSION = '0.3.3' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in yandex-translator.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /lib/yandex-translator.rb: -------------------------------------------------------------------------------- 1 | require 'yandex/translator/version' 2 | require 'httparty' 3 | require 'yandex/translator' 4 | 5 | module Yandex 6 | class ApiError < StandardError; end 7 | end 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | .swp 7 | Gemfile.lock 8 | InstalledFiles 9 | _yardoc 10 | coverage 11 | doc/ 12 | lib/bundler/man 13 | pkg 14 | rdoc 15 | spec/reports 16 | spec/secrets.yml 17 | test/tmp 18 | test/version_tmp 19 | tmp 20 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require 'yandex-translator' 3 | require 'webmock/rspec' 4 | require 'vcr' 5 | include WebMock::API 6 | 7 | RSpec.configure do |config| 8 | if File.exists? ('spec/secrets.yml') 9 | YANDEX_API_KEY = YAML.load_file('spec/secrets.yml') 10 | else 11 | YANDEX_API_KEY = {} 12 | end 13 | config.add_setting :yandex_api_key, :default => YANDEX_API_KEY['yandex_translator_api_key'] 14 | end 15 | 16 | VCR.configure do |c| 17 | c.cassette_library_dir = 'spec/vcr' 18 | c.hook_into :webmock 19 | c.filter_sensitive_data('') { RSpec.configuration.yandex_api_key } 20 | end 21 | -------------------------------------------------------------------------------- /yandex-translator.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'yandex/translator/version' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "yandex-translator" 8 | gem.version = Yandex::Translator::VERSION 9 | gem.authors = ["Artur Egorov"] 10 | gem.email = ["artur@egorov.in"] 11 | gem.description = %q{Library for Yandex Translate API} 12 | gem.summary = %q{Yandex Translate API} 13 | gem.homepage = "" 14 | 15 | gem.add_dependency 'httparty', '>= 0.13.4' 16 | gem.add_development_dependency 'rspec', '~> 3.1' 17 | gem.add_development_dependency 'webmock' 18 | gem.add_development_dependency 'vcr' 19 | 20 | gem.files = `git ls-files`.split($/) 21 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 22 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 23 | gem.require_paths = ["lib"] 24 | end 25 | -------------------------------------------------------------------------------- /spec/vcr/_detect.yml: -------------------------------------------------------------------------------- 1 | --- 2 | http_interactions: 3 | - request: 4 | method: post 5 | uri: https://translate.yandex.net/api/v1.5/tr.json/detect 6 | body: 7 | encoding: UTF-8 8 | string: text=Car&key= 9 | headers: 10 | Accept-Encoding: 11 | - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 12 | Accept: 13 | - "*/*" 14 | User-Agent: 15 | - Ruby 16 | response: 17 | status: 18 | code: 200 19 | message: OK 20 | headers: 21 | Server: 22 | - nginx/1.6.2 23 | Date: 24 | - Fri, 19 May 2017 10:25:21 GMT 25 | Content-Type: 26 | - application/json; charset=utf-8 27 | Content-Length: 28 | - '24' 29 | Connection: 30 | - keep-alive 31 | Keep-Alive: 32 | - timeout=120 33 | Cache-Control: 34 | - no-store 35 | X-Content-Type-Options: 36 | - nosniff 37 | body: 38 | encoding: UTF-8 39 | string: '{"code":200,"lang":"en"}' 40 | http_version: 41 | recorded_at: Fri, 19 May 2017 10:25:21 GMT 42 | recorded_with: VCR 3.0.3 43 | -------------------------------------------------------------------------------- /spec/vcr/wrong_api_key.yml: -------------------------------------------------------------------------------- 1 | --- 2 | http_interactions: 3 | - request: 4 | method: post 5 | uri: https://translate.yandex.net/api/v1.5/tr.json/translate 6 | body: 7 | encoding: UTF-8 8 | string: text=Car&lang=en-ru&key=wrong_api_key 9 | headers: 10 | Accept-Encoding: 11 | - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 12 | Accept: 13 | - "*/*" 14 | User-Agent: 15 | - Ruby 16 | response: 17 | status: 18 | code: 403 19 | message: Forbidden 20 | headers: 21 | Server: 22 | - nginx/1.6.2 23 | Date: 24 | - Fri, 19 May 2017 10:25:20 GMT 25 | Content-Type: 26 | - application/json; charset=utf-8 27 | Content-Length: 28 | - '43' 29 | Connection: 30 | - keep-alive 31 | Keep-Alive: 32 | - timeout=120 33 | X-Content-Type-Options: 34 | - nosniff 35 | Cache-Control: 36 | - no-store 37 | body: 38 | encoding: UTF-8 39 | string: '{"code":401,"message":"API key is invalid"}' 40 | http_version: 41 | recorded_at: Fri, 19 May 2017 10:25:20 GMT 42 | recorded_with: VCR 3.0.3 43 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Artur Egorov 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /spec/vcr/get_translated_pl_text.yml: -------------------------------------------------------------------------------- 1 | --- 2 | http_interactions: 3 | - request: 4 | method: post 5 | uri: https://translate.yandex.net/api/v1.5/tr.json/translate 6 | body: 7 | encoding: UTF-8 8 | string: text=Car&lang=pl&key= 9 | headers: 10 | Accept-Encoding: 11 | - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 12 | Accept: 13 | - "*/*" 14 | User-Agent: 15 | - Ruby 16 | response: 17 | status: 18 | code: 200 19 | message: OK 20 | headers: 21 | Server: 22 | - nginx/1.6.2 23 | Date: 24 | - Fri, 19 May 2017 10:25:19 GMT 25 | Content-Type: 26 | - application/json; charset=utf-8 27 | Content-Length: 28 | - '48' 29 | Connection: 30 | - keep-alive 31 | Keep-Alive: 32 | - timeout=120 33 | X-Content-Type-Options: 34 | - nosniff 35 | Cache-Control: 36 | - no-store 37 | body: 38 | encoding: ASCII-8BIT 39 | string: !binary |- 40 | eyJjb2RlIjoyMDAsImxhbmciOiJlbi1wbCIsInRleHQiOlsiU2Ftb2Now7NkIl19 41 | http_version: 42 | recorded_at: Fri, 19 May 2017 10:25:19 GMT 43 | recorded_with: VCR 3.0.3 44 | -------------------------------------------------------------------------------- /spec/vcr/accepts_options.yml: -------------------------------------------------------------------------------- 1 | --- 2 | http_interactions: 3 | - request: 4 | method: post 5 | uri: https://translate.yandex.net/api/v1.5/tr.json/translate 6 | body: 7 | encoding: UTF-8 8 | string: text=Car&lang=ru&key= 9 | headers: 10 | Accept-Encoding: 11 | - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 12 | Accept: 13 | - "*/*" 14 | User-Agent: 15 | - Ruby 16 | response: 17 | status: 18 | code: 200 19 | message: OK 20 | headers: 21 | Server: 22 | - nginx/1.6.2 23 | Date: 24 | - Fri, 19 May 2017 10:25:20 GMT 25 | Content-Type: 26 | - application/json; charset=utf-8 27 | Content-Length: 28 | - '59' 29 | Connection: 30 | - keep-alive 31 | Keep-Alive: 32 | - timeout=120 33 | X-Content-Type-Options: 34 | - nosniff 35 | Cache-Control: 36 | - no-store 37 | body: 38 | encoding: ASCII-8BIT 39 | string: !binary |- 40 | eyJjb2RlIjoyMDAsImxhbmciOiJlbi1ydSIsInRleHQiOlsi0JDQstGC0L7QvNC+0LHQuNC70YwiXX0= 41 | http_version: 42 | recorded_at: Fri, 19 May 2017 10:25:20 GMT 43 | recorded_with: VCR 3.0.3 44 | -------------------------------------------------------------------------------- /spec/vcr/get_translated_ru_text.yml: -------------------------------------------------------------------------------- 1 | --- 2 | http_interactions: 3 | - request: 4 | method: post 5 | uri: https://translate.yandex.net/api/v1.5/tr.json/translate 6 | body: 7 | encoding: UTF-8 8 | string: text=Car&lang=ru&key= 9 | headers: 10 | Accept-Encoding: 11 | - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 12 | Accept: 13 | - "*/*" 14 | User-Agent: 15 | - Ruby 16 | response: 17 | status: 18 | code: 200 19 | message: OK 20 | headers: 21 | Server: 22 | - nginx/1.6.2 23 | Date: 24 | - Fri, 19 May 2017 10:25:18 GMT 25 | Content-Type: 26 | - application/json; charset=utf-8 27 | Content-Length: 28 | - '59' 29 | Connection: 30 | - keep-alive 31 | Keep-Alive: 32 | - timeout=120 33 | X-Content-Type-Options: 34 | - nosniff 35 | Cache-Control: 36 | - no-store 37 | body: 38 | encoding: ASCII-8BIT 39 | string: !binary |- 40 | eyJjb2RlIjoyMDAsImxhbmciOiJlbi1ydSIsInRleHQiOlsi0JDQstGC0L7QvNC+0LHQuNC70YwiXX0= 41 | http_version: 42 | recorded_at: Fri, 19 May 2017 10:25:18 GMT 43 | recorded_with: VCR 3.0.3 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Code Climate](https://codeclimate.com/repos/5255368456b102667402265a/badges/cc0cf62aa85959503fc1/gpa.png)](https://codeclimate.com/repos/5255368456b102667402265a/feed) 2 | 3 | # Yandex::Translator 4 | 5 | Library for Yandex Translate API | Библиотека для API Яндекс.Переводчика 6 | 7 | 8 | ## Installation 9 | 10 | Add this line to your application's Gemfile: 11 | 12 | gem 'yandex-translator' 13 | 14 | And then execute: 15 | 16 | $ bundle 17 | 18 | Or install it yourself as: 19 | 20 | $ gem install yandex-translator 21 | 22 | ## Usage 23 | 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: 31 | 32 | ```ruby 33 | translator.langs 34 | ``` 35 | 36 | To determine language text use detect method: 37 | 38 | ```ruby 39 | translator.detect 'Hello, world!' 40 | ``` 41 | To translate text use translate method: 42 | 43 | ```ruby 44 | translator.translate 'Car', from: 'ru' 45 | ``` 46 | 47 | In this case Yandex automatically detect text language. 48 | If you want to set text language manually add third parameter 49 | 50 | ```ruby 51 | translator.translate 'Car', from: 'ru', to: 'en' 52 | ``` 53 | 54 | ## Contributing 55 | 56 | 1. Fork it 57 | 2. Create your feature branch (`git checkout -b my-new-feature`) 58 | 3. Commit your changes (`git commit -am 'Add some feature'`) 59 | 4. Push to the branch (`git push origin my-new-feature`) 60 | 5. Create new Pull Request 61 | -------------------------------------------------------------------------------- /lib/yandex/translator.rb: -------------------------------------------------------------------------------- 1 | 2 | module Yandex 3 | 4 | class JsonParser < HTTParty::Parser 5 | def json 6 | ::JSON.parse(body) 7 | end 8 | end 9 | 10 | class Translator 11 | include HTTParty 12 | parser JsonParser 13 | base_uri 'https://translate.yandex.net/api/v1.5/tr.json' 14 | 15 | attr_reader :api_key 16 | 17 | class << self # backwards compatability 18 | attr_reader :api_key, :translator 19 | 20 | def api_key=(key) 21 | @api_key = key 22 | @translator = new(api_key) 23 | end 24 | 25 | def get_langs 26 | translator.get_langs 27 | end 28 | 29 | def translate(text, from: nil, to: nil) 30 | translator.translate(text, from: from, to: to) 31 | end 32 | 33 | def detect(text) 34 | translator.detect(text) 35 | end 36 | 37 | private :translator 38 | end 39 | 40 | def initialize(api_key) 41 | @api_key = api_key 42 | end 43 | 44 | def langs 45 | @langs ||= visit('/getLangs')['dirs'] 46 | end 47 | alias_method :get_langs, :langs 48 | 49 | def detect(text) 50 | lang = visit('/detect', text: text)['lang'] 51 | lang == '' ? nil : lang 52 | end 53 | 54 | def translate(text, from: nil, to: nil, format: 'plain') 55 | lang = (to.nil? && from.nil?) ? [] : [to, from].compact 56 | 57 | options = { text: text, lang: lang.reverse.join('-'), format: format } 58 | 59 | result = visit('/translate', options)['text'] 60 | 61 | result.size == 1 ? result.first : result 62 | end 63 | 64 | def visit(address, options = {}) 65 | response = self.class.post address, body: options.merge(key: api_key) 66 | check_errors(response) 67 | response 68 | end 69 | 70 | private 71 | 72 | def check_errors(response) 73 | if response['code'] and response['code'].to_i != 200 74 | fail ApiError, response['message'] 75 | end 76 | end 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /spec/vcr/_langs.yml: -------------------------------------------------------------------------------- 1 | --- 2 | http_interactions: 3 | - request: 4 | method: post 5 | uri: https://translate.yandex.net/api/v1.5/tr.json/getLangs 6 | body: 7 | encoding: UTF-8 8 | string: key= 9 | headers: 10 | Accept-Encoding: 11 | - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 12 | Accept: 13 | - "*/*" 14 | User-Agent: 15 | - Ruby 16 | response: 17 | status: 18 | code: 200 19 | message: OK 20 | headers: 21 | Server: 22 | - nginx/1.6.2 23 | Content-Type: 24 | - application/json; charset=utf-8 25 | Transfer-Encoding: 26 | - chunked 27 | Connection: 28 | - keep-alive 29 | Keep-Alive: 30 | - timeout=120 31 | Date: 32 | - Fri, 19 May 2017 10:25:21 GMT 33 | X-Content-Type-Options: 34 | - nosniff 35 | body: 36 | encoding: ASCII-8BIT 37 | string: '{"dirs":["az-ru","be-bg","be-cs","be-de","be-en","be-es","be-fr","be-it","be-pl","be-ro","be-ru","be-sr","be-tr","bg-be","bg-ru","bg-uk","ca-en","ca-ru","cs-be","cs-en","cs-ru","cs-uk","da-en","da-ru","de-be","de-en","de-es","de-fr","de-it","de-ru","de-tr","de-uk","el-en","el-ru","en-be","en-ca","en-cs","en-da","en-de","en-el","en-es","en-et","en-fi","en-fr","en-hu","en-it","en-lt","en-lv","en-mk","en-nl","en-no","en-pt","en-ru","en-sk","en-sl","en-sq","en-sv","en-tr","en-uk","es-be","es-de","es-en","es-ru","es-uk","et-en","et-ru","fi-en","fi-ru","fr-be","fr-de","fr-en","fr-ru","fr-uk","hr-ru","hu-en","hu-ru","hy-ru","it-be","it-de","it-en","it-ru","it-uk","lt-en","lt-ru","lv-en","lv-ru","mk-en","mk-ru","nl-en","nl-ru","no-en","no-ru","pl-be","pl-ru","pl-uk","pt-en","pt-ru","ro-be","ro-ru","ro-uk","ru-az","ru-be","ru-bg","ru-ca","ru-cs","ru-da","ru-de","ru-el","ru-en","ru-es","ru-et","ru-fi","ru-fr","ru-hr","ru-hu","ru-hy","ru-it","ru-lt","ru-lv","ru-mk","ru-nl","ru-no","ru-pl","ru-pt","ru-ro","ru-sk","ru-sl","ru-sq","ru-sr","ru-sv","ru-tr","ru-uk","sk-en","sk-ru","sl-en","sl-ru","sq-en","sq-ru","sr-be","sr-ru","sr-uk","sv-en","sv-ru","tr-be","tr-de","tr-en","tr-ru","tr-uk","uk-bg","uk-cs","uk-de","uk-en","uk-es","uk-fr","uk-it","uk-pl","uk-ro","uk-ru","uk-sr","uk-tr"]}' 38 | http_version: 39 | recorded_at: Fri, 19 May 2017 10:25:21 GMT 40 | recorded_with: VCR 3.0.3 41 | -------------------------------------------------------------------------------- /spec/lib/yandex/translator_vcr_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'vcr' 3 | 4 | describe Yandex::Translator do 5 | let(:api_key) { RSpec.configuration.yandex_api_key } 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 | end 14 | 15 | describe '#translate' do 16 | it 'returns ru translalation ' do 17 | VCR.use_cassette('get_translated_ru_text') do 18 | response = translator.translate('Car', 'ru') 19 | expect(response).to eq("Автомобиль") 20 | end 21 | end 22 | 23 | it 'returns pl translalation' do 24 | VCR.use_cassette('get_translated_pl_text') do 25 | response = translator.translate('Car', 'pl') 26 | expect(response).to eq("Samochód") 27 | end 28 | end 29 | 30 | it 'accepts options' do 31 | VCR.use_cassette('accepts options') do 32 | response = translator.translate('Car', from: :ru) 33 | expect(response).to eq("Автомобиль") 34 | end 35 | end 36 | 37 | context 'with two languages' do 38 | it 'accepts options' do 39 | VCR.use_cassette('accepts options') do 40 | response = translator.translate('Samochód', from: :pl, to: :ru) 41 | expect(response).to eq("Автомобиль") 42 | end 43 | end 44 | 45 | it 'accepts languages list' do 46 | VCR.use_cassette('accepts options') do 47 | response = translator.translate('Car', :ru, :en) 48 | expect(response).to eq("Автомобиль") 49 | end 50 | end 51 | end 52 | 53 | context 'with wrong api key' do 54 | let(:wrong_translator) { Yandex::Translator.new('wrong_api_key') } 55 | 56 | it 'returns translation error' do 57 | expect{ 58 | VCR.use_cassette('wrong_api_key') do 59 | wrong_translator.translate('Car', :ru, :en) 60 | end 61 | }.to raise_error(Yandex::ApiError, "API key is invalid") 62 | end 63 | end 64 | end 65 | 66 | describe '#detect' do 67 | let(:detected_translation) { 68 | VCR.use_cassette('#detect') do 69 | translator.detect('Car') 70 | end 71 | } 72 | 73 | it 'returns detected language' do 74 | expect(detected_translation).to eq 'en' 75 | end 76 | end 77 | 78 | describe '#langs' do 79 | let(:list_of_langs) { 80 | VCR.use_cassette('#langs') do 81 | translator.langs 82 | end 83 | } 84 | 85 | it 'returns array of dirs' do 86 | expect(list_of_langs).to include 'en-ru', 'uk-pl', 'pl-ru' 87 | end 88 | 89 | end 90 | end 91 | -------------------------------------------------------------------------------- /spec/lib/yandex/translator_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'vcr' 3 | 4 | describe Yandex::Translator do 5 | let(:api_key) { 'secret' } 6 | 7 | subject(:translator) { Yandex::Translator.new(api_key) } 8 | 9 | describe '::translate' do 10 | before do 11 | expect_any_instance_of(Yandex::Translator).to receive(:translate) 12 | Yandex::Translator.api_key = api_key 13 | end 14 | 15 | it 'calls translate method on instace object' do 16 | Yandex::Translator.translate('Bar', 'ru', 'en') 17 | end 18 | end 19 | 20 | describe '::get_langs' do 21 | before do 22 | expect_any_instance_of(Yandex::Translator).to receive(:get_langs) 23 | Yandex::Translator.api_key = api_key 24 | end 25 | 26 | it 'calls translate method on instace object' do 27 | Yandex::Translator.get_langs 28 | end 29 | end 30 | 31 | describe '::detect' do 32 | before do 33 | expect_any_instance_of(Yandex::Translator).to receive(:detect) 34 | Yandex::Translator.api_key = api_key 35 | end 36 | 37 | it 'calls translate method on instace object' do 38 | Yandex::Translator.detect('Car') 39 | end 40 | end 41 | 42 | 43 | 44 | describe '#translate' do 45 | let(:translate_url) { 'https://translate.yandex.net/api/v1.5/tr.json/translate' } 46 | let(:translate_request_body) { "text=Car&lang=ru&key=#{api_key}" } 47 | let(:translate_response_body) do 48 | '{"code":200, "lang": "en-ru", "text": ["Автомобиль"]}' 49 | end 50 | 51 | let!(:translate_request) do 52 | stub_request(:post, translate_url) 53 | .with(body: translate_request_body) 54 | .to_return( 55 | body: translate_response_body, 56 | headers: {'Content-Type' => 'application/json'} 57 | ) 58 | end 59 | 60 | it 'triggers correct yandex translation url' do 61 | translator.translate('Car', 'ru') 62 | expect(translate_request).to have_been_made.once 63 | end 64 | 65 | it 'returns translalation' do 66 | expect(translator.translate('Car', 'ru')).to eq 'Автомобиль' 67 | end 68 | 69 | it 'accepts options' do 70 | translator.translate('Car', from: :ru) 71 | expect(translate_request).to have_been_made.once 72 | end 73 | 74 | context 'with two languages' do 75 | let(:translate_request_body) { "text=Car&lang=en-ru&key=#{api_key}" } 76 | 77 | it 'accepts options' do 78 | translator.translate('Car', from: :en, to: :ru) 79 | expect(translate_request).to have_been_made.once 80 | end 81 | 82 | it 'accepts languages list' do 83 | translator.translate('Car', :ru, :en) 84 | expect(translate_request).to have_been_made.once 85 | end 86 | end 87 | 88 | context 'when server responds with invalid "lang" parameter error' do 89 | let(:translate_request_body) { "text=Car&lang=ru-ru&key=#{api_key}" } 90 | 91 | let(:translation_url) do 92 | 'https://translate.yandex.net/api/v1.5/tr.json/translate?' \ 93 | "key=#{api_key}&lang=ru-ru&text=Car" 94 | end 95 | 96 | let(:translate_response_body) do 97 | '{"code": 401, "message": "API key is invalid"}' 98 | end 99 | 100 | it 'returns translation error' do 101 | expect{ 102 | translator.translate('Car', 'ru', 'ru') 103 | }.to raise_error(Yandex::ApiError, "API key is invalid") 104 | end 105 | end 106 | end 107 | 108 | describe '#detect' do 109 | let(:detect_url) { "https://translate.yandex.net/api/v1.5/tr.json/detect" } 110 | let(:detect_request_body) { "text=Car&key=#{api_key}" } 111 | let(:decect_response_body) { '{"code": 200, "lang": "en"}' } 112 | let!(:detect_request) do 113 | stub_request(:post, detect_url) 114 | .with(body: detect_request_body) 115 | .to_return( 116 | body: decect_response_body, 117 | headers: { 'Content-Type' => 'application/json' } 118 | ) 119 | end 120 | 121 | it 'hits correct url' do 122 | translator.detect('Car') 123 | expect(detect_request).to have_been_made.once 124 | end 125 | 126 | it 'returns detected language' do 127 | expect(translator.detect('Car')).to eq 'en' 128 | end 129 | 130 | context 'when response does not contains any language' do 131 | let(:decect_response_body) { '{"code": 200, "lang": ""}' } 132 | 133 | it 'returns nil' do 134 | expect(translator.detect('Car')).to be_nil 135 | end 136 | end 137 | end 138 | 139 | describe '#langs' do 140 | let(:langs_url) { "https://translate.yandex.net/api/v1.5/tr.json/getLangs" } 141 | let(:langs_request_body) { "key=#{api_key}" } 142 | let(:langs_response_body) { '{"dirs": ["en-ru"]}' } 143 | let!(:langs_request) do 144 | stub_request(:post, langs_url) 145 | .with(body: langs_request_body) 146 | .to_return( 147 | body: langs_response_body, 148 | headers: { 'Content-Type' => 'application/json' } 149 | ) 150 | end 151 | 152 | it 'hits correct url' do 153 | translator.langs 154 | expect(langs_request).to have_been_made.once 155 | end 156 | 157 | it 'returns array of dirs' do 158 | expect(translator.langs).to eq ['en-ru'] 159 | end 160 | 161 | end 162 | end 163 | --------------------------------------------------------------------------------