Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix occasional errors we see when generating a uuid using uuid library by switching to securerandom #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.4
1 change: 0 additions & 1 deletion emailage.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/emailage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ module Emailage
}

class Error < StandardError; end
class Failure < StandardError; end
end
9 changes: 6 additions & 3 deletions lib/emailage/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'typhoeus'
require 'uuid'
require 'securerandom'
require 'json'

module Emailage
Expand Down Expand Up @@ -38,7 +38,7 @@ 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
Expand All @@ -47,7 +47,10 @@ def request(endpoint, 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, "Received Body: '#{res.body}' with Code: #{res.code}"
end
return JSON.parse json
end

public
Expand Down
21 changes: 19 additions & 2 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
let(:t) {spy :typhoeus}
let(:email) {'[email protected]'}
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
}

Expand All @@ -33,6 +34,22 @@
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)
end
end
end

describe '#query' do
Expand Down