Class: TDAnalytics::BatchConsumer
- Inherits:
-
Object
- Object
- TDAnalytics::BatchConsumer
- Defined in:
- lib/thinkingdata-ruby/batch_consumer.rb
Constant Summary collapse
- DEFAULT_LENGTH =
buffer count
20- MAX_LENGTH =
2000
Instance Method Summary collapse
- #_set_compress(compress) ⇒ Object
- #add(message) ⇒ Object
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(server_url, app_id, max_buffer_length = DEFAULT_LENGTH) ⇒ BatchConsumer
constructor
A new instance of BatchConsumer.
Constructor Details
#initialize(server_url, app_id, max_buffer_length = DEFAULT_LENGTH) ⇒ BatchConsumer
Returns a new instance of BatchConsumer.
11 12 13 14 15 16 17 18 |
# File 'lib/thinkingdata-ruby/batch_consumer.rb', line 11 def initialize(server_url, app_id, max_buffer_length = DEFAULT_LENGTH) @server_uri = URI.parse(server_url) @server_uri.path = '/sync_server' @app_id = app_id @compress = true @max_length = [max_buffer_length, MAX_LENGTH].min @buffers = [] end |
Instance Method Details
#_set_compress(compress) ⇒ Object
20 21 22 |
# File 'lib/thinkingdata-ruby/batch_consumer.rb', line 20 def _set_compress(compress) @compress = compress end |
#add(message) ⇒ Object
24 25 26 27 |
# File 'lib/thinkingdata-ruby/batch_consumer.rb', line 24 def add() @buffers << flush if @buffers.length >= @max_length end |
#close ⇒ Object
29 30 31 |
# File 'lib/thinkingdata-ruby/batch_consumer.rb', line 29 def close flush end |
#flush ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/thinkingdata-ruby/batch_consumer.rb', line 33 def flush begin @buffers.each_slice(@max_length) do |chunk| if @compress wio = StringIO.new("w") gzip_io = Zlib::GzipWriter.new(wio) gzip_io.write(chunk.to_json) gzip_io.close data = wio.string else data = chunk.to_json end compress_type = @compress ? 'gzip' : 'none' headers = {'Content-Type' => 'application/plaintext', 'appid' => @app_id, 'compress' => compress_type, 'TA-Integration-Type'=>'Ruby', 'TA-Integration-Version'=>TDAnalytics::VERSION, 'TA-Integration-Count'=>@buffers.count, 'TA_Integration-Extra'=>'batch'} request = CaseSensitivePost.new(@server_uri.request_uri, headers) request.body = data begin response_code, response_body = _request(@server_uri, request) 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['code'] != 0 raise ServerError.new("Could not write to TA, server responded with #{response_code} returning: '#{response_body}'") end end rescue raise end @buffers = [] end |