Class: Mixpanel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mixpanel_client.rb

Overview

The mixpanel client can be used to easily consume data through the mixpanel API

Constant Summary

OPTIONS =
[:resource, :event, :funnel, :name, :type, :unit, :interval, :limit, :format, :bucket]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(config)

A new instance of Client



35
36
37
38
# File 'lib/mixpanel_client.rb', line 35

def initialize(config)
  @api_key    = config['api_key']
  @api_secret = config['api_secret']
end

Instance Attribute Details

- (Object) api_key

Returns the value of attribute api_key



25
26
27
# File 'lib/mixpanel_client.rb', line 25

def api_key
  @api_key
end

- (Object) api_secret

Returns the value of attribute api_secret



25
26
27
# File 'lib/mixpanel_client.rb', line 25

def api_secret
  @api_secret
end

- (Object) uri (readonly)

Returns the value of attribute uri



24
25
26
# File 'lib/mixpanel_client.rb', line 24

def uri
  @uri
end

Instance Method Details

- (Object) generate_signature(args)



69
70
71
# File 'lib/mixpanel_client.rb', line 69

def generate_signature(args)
  Digest::MD5.hexdigest(args.map{|key,val| "#{key}=#{val}"}.sort.join + api_secret)
end

- (Object) normalize_params(params)



62
63
64
65
66
67
# File 'lib/mixpanel_client.rb', line 62

def normalize_params(params)
  params.merge!(
    :api_key => @api_key,
    :expire  => Time.now.to_i + 600 # Grant this request 10 minutes
  ).merge!(:sig => generate_signature(params))
end

- (Object) params



40
41
42
43
44
45
# File 'lib/mixpanel_client.rb', line 40

def params
  OPTIONS.inject({}) do |params, param|
    params.merge!(param => send(param)) if param != :resource && !send(param).nil?
    params
  end
end

- (Object) request(deprecated_endpoint = nil, deprecated_meth = nil, deprecated_params = nil, &options)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mixpanel_client.rb', line 47

def request(deprecated_endpoint=nil, deprecated_meth=nil, deprecated_params=nil, &options)
  reset_options
  if block_given?
    instance_eval &options
    @uri = URI.mixpanel(resource, normalize_params(params))
    response = URI.get(@uri)
    to_hash(response)
  else
    warn 'This usage is deprecated. Please use the new block form (see README).'
    @uri = URI.deprecated_mixpanel(deprecated_endpoint, deprecated_meth, normalize_params(deprecated_params))
    response = URI.get(@uri)
    to_hash(response)
  end
end

- (Object) reset_options



81
82
83
84
85
# File 'lib/mixpanel_client.rb', line 81

def reset_options
  (OPTIONS - [:resource]).each do |option|
    eval "remove_instance_variable(:@#{option}) if defined?(@#{option})"
  end
end

- (Object) to_hash(data)



73
74
75
76
77
78
79
# File 'lib/mixpanel_client.rb', line 73

def to_hash(data)
  if @format == 'csv'
    data
  else
    JSON.parse(data)
  end
end