Skip to content

Commit

Permalink
Merge pull request #48 from Buyapowa/master
Browse files Browse the repository at this point in the history
Add timeout option
  • Loading branch information
keolo committed Jun 21, 2016
2 parents 52cb38f + a21e4de commit 6c4a127
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/mixpanel/client.rb
Expand Up @@ -14,7 +14,7 @@ class Client
IMPORT_URI = 'https://api.mixpanel.com'

attr_reader :uri
attr_accessor :api_key, :api_secret, :parallel
attr_accessor :api_key, :api_secret, :parallel, :timeout

# Configure the client
#
Expand All @@ -27,6 +27,7 @@ def initialize(config)
@api_key = config[:api_key]
@api_secret = config[:api_secret]
@parallel = config[:parallel] || false
@timeout = config[:timeout] || nil

fail ConfigurationError if @api_key.nil? || @api_secret.nil?
end
Expand Down Expand Up @@ -63,7 +64,7 @@ def make_parallel_request
end

def make_normal_request(resource)
response = URI.get(@uri)
response = URI.get(@uri, @timeout)

if %w(export import).include?(resource) && @format != 'raw'
response = %Q([#{response.split("\n").join(',')}])
Expand Down
4 changes: 2 additions & 2 deletions lib/mixpanel/uri.rb
Expand Up @@ -18,8 +18,8 @@ def self.encode(params)
params.map { |key, val| "#{key}=#{CGI.escape(val.to_s)}" }.sort.join('&')
end

def self.get(uri)
::URI.parse(uri).read
def self.get(uri, timeout)
::URI.parse(uri).read(read_timeout: timeout)
rescue OpenURI::HTTPError => error
raise HTTPError, JSON.parse(error.io.read)['error']
end
Expand Down
15 changes: 15 additions & 0 deletions spec/mixpanel_client/mixpanel_client_spec.rb
Expand Up @@ -25,6 +25,21 @@
parallel: true
).parallel.should eq true
end

it 'should set a timeout option as nil by default' do
Mixpanel::Client.new(
api_key: 'test_key',
api_secret: 'test_secret'
).timeout.should be_nil
end

it 'should be able to set a timeout option when passed' do
Mixpanel::Client.new(
api_key: 'test_key',
api_secret: 'test_secret',
timeout: 3
).timeout.should eql 3
end
end

context 'when making an invalid request' do
Expand Down
24 changes: 23 additions & 1 deletion spec/mixpanel_client/uri_spec.rb
Expand Up @@ -53,7 +53,29 @@
it 'should return a string response' do
stub_request(:get, 'http://example.com').to_return(body: 'something')

Mixpanel::URI.get('http://example.com').should eq 'something'
Mixpanel::URI.get('http://example.com', nil).should eq 'something'
end

context 'when timeout is not nil' do

context 'when the request times out' do
it 'should return a timeout error' do
stub_request(:get, 'http://example.com').to_timeout

expect do
Mixpanel::URI.get('http://example.com', 3)
end.to raise_error Timeout::Error
end
end

context 'when the request does not timeout' do
it 'should return a string response' do
stub_request(:get, 'http://example.com').to_return(body: 'something')

Mixpanel::URI.get('http://example.com', 3).should eq 'something'
end
end

end
end
end

0 comments on commit 6c4a127

Please sign in to comment.