Class: TDAnalytics::DebugConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/thinkingdata-ruby/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) ⇒ DebugConsumer

Returns a new instance of DebugConsumer.



12
13
14
15
16
17
18
# File 'lib/thinkingdata-ruby/debug_consumer.rb', line 12

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
end

Instance Method Details

#add(message) ⇒ Object



20
21
22
23
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
# File 'lib/thinkingdata-ruby/debug_consumer.rb', line 20

def add(message)
  puts message.to_json
  headers =  {
      'TA-Integration-Type'=>'Ruby',
      'TA-Integration-Version'=>TDAnalytics::VERSION,
      'TA-Integration-Count'=>'1',
      'TA_Integration-Extra'=>'debug'
  }
  form_data = {"data" => message.to_json, "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)
  rescue => e
    raise ConnectionError.new("Could not connect to TA 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 TA server response: '#{response_body}'")
    end
  end

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

#request(uri, form_data, headers) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/thinkingdata-ruby/debug_consumer.rb', line 51

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

#testObject



8
9
10
# File 'lib/thinkingdata-ruby/debug_consumer.rb', line 8

def test

end