Class: GameAnalytics::Worker

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/game_analytics/worker.rb

Instance Method Summary collapse

Methods included from Common

#client, #disabled, #logger, #options

Constructor Details

#initialize(q) ⇒ Worker

Returns a new instance of Worker.



10
11
12
13
14
# File 'lib/game_analytics/worker.rb', line 10

def initialize(q)
  @queue = q
  @http = HTTPClient.new
  @url_base = "http://api.gameanalytics.com/1/#{options[:game_key]}"
end

Instance Method Details

#process(unit) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/game_analytics/worker.rb', line 16

def process(unit)
  metric = unit.is_a?(Array) ? unit.first : unit
  klass = metric.class

  json_data = unit.to_json
  header = {'Authorization' => Digest::MD5.hexdigest(json_data + options[:secret_key])}
  header['X-Forwarded-For'] = metric.origin_ip if metric.origin_ip
  category = klass.name.demodulize.downcase
  url = "#{@url_base}/#{category}"
  logger.info "GameAnalytics <: #{url} #{json_data} #{header.inspect}"
  resp = @http.post(url, :body => json_data, :header => header)
  logger.info "GameAnalytics >: #{resp.content} (#{resp.status})"
end

#runObject



30
31
32
33
34
35
# File 'lib/game_analytics/worker.rb', line 30

def run
  logger.info "GameAnalytics worker running"
  loop do
    process @queue.pop
  end
end