Class: ThinkingData::TDDebugConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/thinkingdata-ruby/td_debug_consumer.rb

Overview

The data is reported one by one, and when an error occurs, the log will be printed on the console.

Instance Method Summary collapse

Constructor Details

#initialize(server_url, app_id, write_data = true, device_id: nil) ⇒ TDDebugConsumer

Init debug consumer

@param server_url: server url
@param app_id: app id
@param write_data: is write data to TE
@param device_id: device id


15
16
17
18
19
20
21
22
# File 'lib/thinkingdata-ruby/td_debug_consumer.rb', line 15

def initialize(server_url, app_id, write_data = true, device_id: nil)
  @server_uri = URI.parse(server_url)
  @server_uri.path = '/data_debug'
  @app_id = app_id
  @write_data = write_data
  @device_id = device_id
  TDLog.info("TDDebugConsumer init success. ServerUrl: #{server_url}, appId: #{app_id}, deviceId: #{device_id}")
end

Instance Method Details

#add(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/thinkingdata-ruby/td_debug_consumer.rb', line 24

def add(message)
  msg_json_str = message.to_json
  TDLog.info("Send data, request: #{msg_json_str}")
  headers =  {
      'TE-Integration-Type'=>'Ruby',
      'TE-Integration-Version'=>ThinkingData::VERSION,
      'TE-Integration-Count'=>'1',
      'TA_Integration-Extra'=>'debug'
  }
  form_data = {"data" => msg_json_str, "appid" => @app_id, "dryRun" => @write_data ? "0" : "1", "source" => "server"}
  @device_id.is_a?(String) ? form_data["deviceId"] = @device_id : nil

  begin
    response_code, response_body = request(@server_uri, form_data,headers)
    TDLog.info("Send data, response: #{response_body}")
  rescue => e
    raise ConnectionError.new("Could not connect to TE server, with error \"#{e.message}\".")
  end

  result = {}
  if response_code.to_i == 200
    begin
      result = JSON.parse(response_body.to_s)
    rescue JSON::JSONError
      raise ServerError.new("Could not interpret TE server response: '#{response_body}'")
    end
  end

  if result['errorLevel'] != 0
    raise ServerError.new("Could not write to TE, server responded with #{response_code} returning: '#{response_body}'")
  end
end

#request(uri, form_data, headers) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/thinkingdata-ruby/td_debug_consumer.rb', line 57

def request(uri, form_data,headers)
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.set_form_data(form_data)

  client = Net::HTTP.new(uri.host, uri.port)
  client.use_ssl = uri.scheme === 'https' ? true : false
  client.open_timeout = 10
  client.continue_timeout = 10
  client.read_timeout = 10
  client.ssl_timeout = 10

  response = client.request(request)
  [response.code, response.body]
end