Class: SensorsAnalytics::DebugConsumer

Inherits:
SensorsAnalyticsConsumer show all
Defined in:
lib/sensors_analytics/consumers.rb

Overview

Debug 模式的 Consumer,Debug 模式的具体信息请参考文档

http://www.sensorsdata.cn/manual/debug_mode.html

write_data 参数为 true,则 Debug 模式下导入的数据会导入 Sensors Analytics;否则,Debug 模式下导入的数据将只进行格式校验,不会导入 Sensors Analytics 中

Instance Method Summary collapse

Methods inherited from SensorsAnalyticsConsumer

#request!

Constructor Details

#initialize(server_url, write_data) ⇒ DebugConsumer

Returns a new instance of DebugConsumer.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sensors_analytics/consumers.rb', line 91

def initialize(server_url, write_data)
  uri = URI.parse(server_url)
  # 将 URL Path 替换成 Debug 模式的 '/debug'
  uri.path = '/debug'

  @headers = {}
  unless write_data
    @headers['Dry-Run'] = 'true'
  end

  super(uri.to_s)
end

Instance Method Details

#send(event) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sensors_analytics/consumers.rb', line 104

def send(event)
  event_list = [event]

  begin
    response_code, response_body = request!(event_list, @headers)
  rescue => e
    raise DebugModeError.new("Could not connect to Sensors Analytics, with error \"#{e.message}\".")
  end

  puts "=========================================================================="

  if response_code.to_i == 200
    puts "valid message: #{event_list.to_json}"
  else
    puts "invalid message: #{event_list.to_json}"
    puts "response code: #{response_code}"
    puts "response body: #{response_body}"
  end

  if response_code.to_i >= 300
    raise DebugModeError.new("Could not write to Sensors Analytics, server responded with #{response_code} returning: '#{response_body}'")
  end
end