Class: Mixpanel::Client
- Inherits:
-
Object
- Object
- Mixpanel::Client
- Defined in:
- lib/mixpanel/utils.rb,
lib/mixpanel/client.rb,
lib/mixpanel/version.rb
Overview
Return metrics from Mixpanel Data API
Defined Under Namespace
Modules: Utils
Constant Summary collapse
- BASE_URI =
'https://mixpanel.com/api/2.0'- DATA_URI =
'https://data.mixpanel.com/api/2.0'- IMPORT_URI =
'https://api.mixpanel.com'- VERSION =
Mixpanel::Client library version
'3.1.4'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
Returns the value of attribute api_secret.
-
#parallel ⇒ Object
Returns the value of attribute parallel.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #hydra ⇒ Object
-
#initialize(config) ⇒ Client
constructor
Configure the client.
- #prepare_parallel_request ⇒ Object
-
#request(resource, options) ⇒ JSON, String
Return mixpanel data as a JSON object or CSV string.
-
#request_uri(resource, options) ⇒ JSON, String
Return mixpanel URI to the data.
- #run_parallel_requests ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Configure the client
26 27 28 29 30 |
# File 'lib/mixpanel/client.rb', line 26 def initialize(config) @api_key = config[:api_key] @api_secret = config[:api_secret] @parallel = config[:parallel] || false end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
17 18 19 |
# File 'lib/mixpanel/client.rb', line 17 def api_key @api_key end |
#api_secret ⇒ Object
Returns the value of attribute api_secret.
17 18 19 |
# File 'lib/mixpanel/client.rb', line 17 def api_secret @api_secret end |
#parallel ⇒ Object
Returns the value of attribute parallel.
17 18 19 |
# File 'lib/mixpanel/client.rb', line 17 def parallel @parallel end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
16 17 18 |
# File 'lib/mixpanel/client.rb', line 16 def uri @uri end |
Instance Method Details
#hydra ⇒ Object
106 107 108 |
# File 'lib/mixpanel/client.rb', line 106 def hydra @hydra ||= ::Typhoeus::Hydra.new end |
#prepare_parallel_request ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mixpanel/client.rb', line 84 def prepare_parallel_request request = ::Typhoeus::Request.new(@uri) request.on_complete do |response| if response.success? Utils.to_hash(response.body, @format) elsif response.timed_out? raise TimeoutError elsif response.code == 0 # Could not get an http response, something's wrong. raise HTTPError, response. else # Received a non-successful http response. raise HTTPError, response.body.present? ? JSON.parse(response.body)['error'] : response.code.to_s end end request end |
#request(resource, options) ⇒ JSON, String
Return mixpanel data as a JSON object or CSV string
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mixpanel/client.rb', line 49 def request(resource, ) @uri = request_uri(resource, ) if @parallel parallel_request = prepare_parallel_request hydra.queue parallel_request parallel_request else response = URI.get(@uri) response = %Q|[#{response.split("\n").join(',')}]| if %w(export import).include?(resource) Utils.to_hash(response, @format) end end |
#request_uri(resource, options) ⇒ JSON, String
Return mixpanel URI to the data
79 80 81 82 |
# File 'lib/mixpanel/client.rb', line 79 def request_uri(resource, ) @format = [:format] || :json URI.mixpanel(resource, ()) end |
#run_parallel_requests ⇒ Object
102 103 104 |
# File 'lib/mixpanel/client.rb', line 102 def run_parallel_requests hydra.run end |