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

Bm emailage #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 7 additions & 4 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,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
Expand Down
24 changes: 22 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,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
Expand Down