-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathuri.rb
30 lines (27 loc) · 790 Bytes
/
uri.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env ruby -Ku
# Mixpanel API Ruby Client Library
#
# URI related helpers
#
# Copyright (c) 2009+ Keolo Keagy
# See LICENSE for details
module Mixpanel
# Utilities to assist generating and requesting URIs
class URI
def self.mixpanel(resource, params)
base = Mixpanel::Client.base_uri_for_resource(resource)
"#{File.join([base, resource.to_s])}?#{encode(params)}"
end
def self.encode(params)
params.map { |key, val| "#{key}=#{CGI.escape(val.to_s)}" }.sort.join('&')
end
def self.get(uri, timeout, secret)
::URI.parse(uri).read(
read_timeout: timeout,
http_basic_authentication: [secret, nil]
)
rescue OpenURI::HTTPError => error
raise HTTPError, JSON.parse(error.io.read)['error']
end
end
end