Class: Embulk::Input::MixpanelApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/mixpanel_api/client.rb

Constant Summary collapse

ENDPOINT_EXPORT =
"https://data.mixpanel.com/api/2.0/export/".freeze
TIMEOUT_SECONDS =
3600

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/embulk/input/mixpanel_api/client.rb', line 13

def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
end

Instance Method Details

#export(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/embulk/input/mixpanel_api/client.rb', line 18

def export(params = {})
  # https://mixpanel.com/docs/api-documentation/exporting-raw-data-you-inserted-into-mixpanel
  params[:expire] ||= Time.now.to_i + TIMEOUT_SECONDS
  params[:sig] = signature(params)

  Embulk.logger.debug "Export param: #{params.to_s}"

  response = httpclient.get(ENDPOINT_EXPORT, params)

  Embulk.logger.debug "response code: #{response.code}"

  if (400..499).include?(response.code)
    raise ConfigError, response.body
  elsif response.code >= 500
    raise RuntimeError, response.body
  end

  Enumerator.new do |y|
    response.body.lines.each do |json|
      y << JSON.parse(json)
    end
  end
end