diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..a4dd9db --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.4 diff --git a/emailage.gemspec b/emailage.gemspec index afab9cb..e5f1e2a 100644 --- a/emailage.gemspec +++ b/emailage.gemspec @@ -26,6 +26,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "redcarpet", "~> 3.3" spec.add_dependency "typhoeus", "~> 1.0" - spec.add_dependency "uuid", "~> 2.3" spec.add_dependency "json", "~> 2.3" end diff --git a/lib/emailage.rb b/lib/emailage.rb index f506e54..d67ddd2 100644 --- a/lib/emailage.rb +++ b/lib/emailage.rb @@ -19,4 +19,5 @@ module Emailage } class Error < StandardError; end + class Failure < StandardError; end end diff --git a/lib/emailage/client.rb b/lib/emailage/client.rb index b3f9839..ae0d177 100644 --- a/lib/emailage/client.rb +++ b/lib/emailage/client.rb @@ -1,5 +1,5 @@ require 'typhoeus' -require 'uuid' +require 'securerandom' require 'json' module Emailage @@ -38,16 +38,19 @@ def request(endpoint, params) params = { :format => 'json', :oauth_consumer_key => @secret, - :oauth_nonce => UUID.new.generate, + :oauth_nonce => SecureRandom.uuid, :oauth_signature_method => 'HMAC-SHA1', :oauth_timestamp => Time.now.to_i, :oauth_version => 1.0 }.merge(params) res = Typhoeus.get url, :params => params.merge(:oauth_signature => Signature.create('GET', url, params, @hmac_key)) - + json = res.body.sub(/^[^{]+/, '') - JSON.parse json + if json.empty? + raise Failure, "Body length: #{res.body&.length} Code: #{res.code} " + end + return JSON.parse json end public diff --git a/spec/client_spec.rb b/spec/client_spec.rb index d469b86..5c4e9a8 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -4,9 +4,10 @@ let(:t) {spy :typhoeus} let(:email) {'test+emailage@example.com'} let(:ip) {'1.234.56.7'} - + let(:response) { double :response, :body => "\xEF\xBB\xBF{\"success\":[true]}", :code => 200 } + before { - allow(t).to receive(:get) {double :response, :body => "\xEF\xBB\xBF{\"success\":[true]}"} + allow(t).to receive(:get) {response} stub_const 'Typhoeus', t } @@ -33,6 +34,25 @@ it 'parses response body as JSON' do expect(request).to eq 'success' => [true] end + + context 'empty string returned' do + let(:response) { double :response, :body => "", :code => 503 } + + it 'raises TemporaryError' do + expect { request }.to raise_error(Emailage::Failure) + end + end + + context 'error string is returned' do + let(:response) { double :response, :body => "dummy body }", :code => 503 } + + it 'raises TemporaryError' do + expect { request }.to raise_error(Emailage::Failure) + expect(response.body.length).to eq(12) + expect(response.code).to eq 503 + + end + end end describe '#query' do