Skip to content
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: 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
14 changes: 10 additions & 4 deletions lib/emailage/client.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
require 'typhoeus'
require 'uuid'
require 'securerandom'
require 'json'

module Emailage
class Client
attr_reader :secret, :token, :hmac_key, :sandbox
attr_accessor :raise_errors

class EmailageError < StandardError; end
class TemporaryError < EmailageError; end

# @param secret [String] Consumer secret, e.g. SID or API key.
# @param token [String] Consumer OAuth token.
# @param sandbox [Boolean] Whether to use a sandbox instead of a production server.
Expand Down Expand Up @@ -38,16 +41,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 res.code == 200
return JSON.parse json
elsif json.empty?
raise TemporaryError
end
end

public
Expand Down
13 changes: 11 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) {'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
}

Expand All @@ -33,6 +34,14 @@
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(described_class::TemporaryError)
end
end
end

describe '#query' do
Expand Down