Skip to content

Commit

Permalink
Merge pull request #12 from Panczo/master
Browse files Browse the repository at this point in the history
Fix Encoding::CompatibilityError: incompatible encoding regexp match …
  • Loading branch information
aegorov authored Jul 4, 2017
2 parents a736d6b + ff8a9bc commit c30ab6f
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lib/bundler/man
pkg
rdoc
spec/reports
spec/secrets.yml
test/tmp
test/version_tmp
tmp
9 changes: 9 additions & 0 deletions lib/yandex/translator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@

module Yandex

class JsonParser < HTTParty::Parser
def json
::JSON.parse(body)
end
end

class Translator
include HTTParty
parser JsonParser
base_uri 'https://translate.yandex.net/api/v1.5/tr.json'

attr_reader :api_key
Expand Down
1 change: 1 addition & 0 deletions spec/lib/yandex/translator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'vcr'

describe Yandex::Translator do
let(:api_key) { 'secret' }
Expand Down
90 changes: 90 additions & 0 deletions spec/lib/yandex/translator_vcr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'spec_helper'
require 'vcr'

describe Yandex::Translator do
let(:api_key) { RSpec.configuration.yandex_api_key }
subject(:translator) { Yandex::Translator.new(api_key) }

describe '::translate' do
before do
expect_any_instance_of(Yandex::Translator).to receive(:translate)
Yandex::Translator.api_key = api_key
end
end

describe '#translate' do
it 'returns ru translalation ' do
VCR.use_cassette('get_translated_ru_text') do
response = translator.translate('Car', 'ru')
expect(response).to eq("Автомобиль")
end
end

it 'returns pl translalation' do
VCR.use_cassette('get_translated_pl_text') do
response = translator.translate('Car', 'pl')
expect(response).to eq("Samochód")
end
end

it 'accepts options' do
VCR.use_cassette('accepts options') do
response = translator.translate('Car', from: :ru)
expect(response).to eq("Автомобиль")
end
end

context 'with two languages' do
it 'accepts options' do
VCR.use_cassette('accepts options') do
response = translator.translate('Samochód', from: :pl, to: :ru)
expect(response).to eq("Автомобиль")
end
end

it 'accepts languages list' do
VCR.use_cassette('accepts options') do
response = translator.translate('Car', :ru, :en)
expect(response).to eq("Автомобиль")
end
end
end

context 'with wrong api key' do
let(:wrong_translator) { Yandex::Translator.new('wrong_api_key') }

it 'returns translation error' do
expect{
VCR.use_cassette('wrong_api_key') do
wrong_translator.translate('Car', :ru, :en)
end
}.to raise_error(Yandex::ApiError, "API key is invalid")
end
end
end

describe '#detect' do
let(:detected_translation) {
VCR.use_cassette('#detect') do
translator.detect('Car')
end
}

it 'returns detected language' do
expect(detected_translation).to eq 'en'
end
end

describe '#langs' do
let(:list_of_langs) {
VCR.use_cassette('#langs') do
translator.langs
end
}

it 'returns array of dirs' do
expect(list_of_langs).to include 'en-ru', 'uk-pl', 'pl-ru'
end

end
end
14 changes: 13 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'rspec'
require 'yandex-translator'
require 'webmock/rspec'
include WebMock
require 'vcr'
include WebMock::API

RSpec.configure do |config|
if File.exists? ('spec/secrets.yml')
YANDEX_API_KEY = YAML.load_file('spec/secrets.yml')
else
YANDEX_API_KEY = {}
end
config.add_setting :yandex_api_key, :default => YANDEX_API_KEY['yandex_translator_api_key']
end

VCR.configure do |c|
c.cassette_library_dir = 'spec/vcr'
c.hook_into :webmock
c.filter_sensitive_data('<YANDEX_API_KEY>') { RSpec.configuration.yandex_api_key }
end
42 changes: 42 additions & 0 deletions spec/vcr/_detect.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions spec/vcr/_langs.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions spec/vcr/accepts_options.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions spec/vcr/get_translated_pl_text.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions spec/vcr/get_translated_ru_text.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c30ab6f

Please sign in to comment.