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
'4.1.2'
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.
- #make_normal_request(resource) ⇒ Object
- #make_parallel_request ⇒ Object
-
#prepare_parallel_request ⇒ Object
rubocop:disable MethodLength.
-
#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
rubocop:enable MethodLength.
Constructor Details
#initialize(config) ⇒ Client
Configure the client
26 27 28 29 30 31 32 |
# File 'lib/mixpanel/client.rb', line 26 def initialize(config) @api_key = config[:api_key] @api_secret = config[:api_secret] @parallel = config[:parallel] || false fail ConfigurationError if @api_key.nil? || @api_secret.nil? 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
131 132 133 |
# File 'lib/mixpanel/client.rb', line 131 def hydra @hydra ||= ::Typhoeus::Hydra.new end |
#make_normal_request(resource) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/mixpanel/client.rb', line 65 def make_normal_request(resource) response = URI.get(@uri) if %w(export import).include?(resource) && @format != 'raw' response = %Q([#{response.split("\n").join(',')}]) end Utils.to_hash(response, @format) end |
#make_parallel_request ⇒ Object
58 59 60 61 62 63 |
# File 'lib/mixpanel/client.rb', line 58 def make_parallel_request require 'typhoeus' parallel_request = prepare_parallel_request hydra.queue parallel_request parallel_request end |
#prepare_parallel_request ⇒ Object
rubocop:disable MethodLength
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mixpanel/client.rb', line 100 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? fail TimeoutError elsif response.code == 0 # Could not get an http response, something's wrong fail HTTPError, response. else # Received a non-successful http response if response.body && response.body != '' = JSON.parse(response.body)['error'] else = response.code.to_s end fail HTTPError, end end request end |
#request(resource, options) ⇒ JSON, String
Return mixpanel data as a JSON object or CSV string
53 54 55 56 |
# File 'lib/mixpanel/client.rb', line 53 def request(resource, ) @uri = request_uri(resource, ) @parallel ? make_parallel_request : make_normal_request(resource) end |
#request_uri(resource, options) ⇒ JSON, String
Return mixpanel URI to the data
94 95 96 97 |
# File 'lib/mixpanel/client.rb', line 94 def request_uri(resource, ) @format = [:format] || :json URI.mixpanel(resource, ()) end |
#run_parallel_requests ⇒ Object
rubocop:enable MethodLength
127 128 129 |
# File 'lib/mixpanel/client.rb', line 127 def run_parallel_requests hydra.run end |