Skip to content

Commit

Permalink
Merge branch 'buybay-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
seejohnrun committed Apr 2, 2024
2 parents dac01cb + d46519a commit c1808ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
13 changes: 9 additions & 4 deletions lib/easy_translate/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ def perform_raw
request.body = body
# Fire and return
response = http.request(request)
unless response.code == '200'
err = JSON.parse(response.body)['error']['errors'].first['message']
raise EasyTranslateException.new(err)
end
raise_exception(response) unless response.code == '200'
response.body
end

private

def raise_exception(response)
err = JSON.parse(response.body)['error']['errors'].first['message']
rescue JSON::ParserError => _e
err = "#{response.code} - #{response.message}"
ensure
raise EasyTranslateException.new(err)
end

def uri
@uri ||= URI.parse("https://translation.googleapis.com#{path}?#{param_s}")
end
Expand Down
4 changes: 2 additions & 2 deletions spec/examples/real_world_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

it 'should be able to translate multiple' do
res = EasyTranslate.translate ['hello world', 'i love you'], :to => :spanish
expect(res).to eq(['Hola Mundo', 'te amo'])
expect(res).to eq(['Hola Mundo', 'te quiero'])
end

it 'should work concurrently' do
res = EasyTranslate.translate ['hello world', 'i love you', 'good morning'], :to => :spanish, :concurrency => 2, :batch_size => 1
expect(res).to eq(['Hola Mundo', 'te amo', 'Buenos días'])
expect(res).to eq(['Hola Mundo', 'te quiero', 'Buenos días'])
end
end

Expand Down
10 changes: 4 additions & 6 deletions spec/examples/translation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
expect(trans).to eq(%{Hallo ' & " Welt})
end

describe 'needs api key' do
context 'detecting availale language' do
before :each do
if ENV['API_KEY']
EasyTranslate.api_key = ENV['API_KEY']
else
pending 'please provide an API_KEY for this suite'
end
pending 'please provide an API_KEY for this suite' if ENV['API_KEY'].nil?

EasyTranslate.api_key = ENV['API_KEY']
end

it 'should detect simplified chinese as zh-CN' do
Expand Down

0 comments on commit c1808ce

Please sign in to comment.