Module: WhenAUser

Defined in:
lib/whenauser.rb,
lib/whenauser/helpers.rb,
lib/whenauser/version.rb,
lib/whenauser/pageviews.rb,
lib/whenauser/exceptions.rb,
lib/whenauser/memory_queue.rb,
lib/whenauser/girl_friday_queue.rb

Defined Under Namespace

Modules: Helpers Classes: Exceptions, GirlFridayQueue, MemoryQueue, Pageviews, Rack

Constant Summary collapse

VERSION =
'0.6.3'

Class Method Summary collapse

Class Method Details

.current_user(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/whenauser.rb', line 42

def self.current_user(env)
  if controller = env['action_controller.instance']
    if current_user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
      [:login, :username, :email, :id].each do |field|
        return current_user.send(field) if current_user.respond_to?(field)
      end
    end
  end
  nil
rescue
  nil
end

.default_ignored_crawlersObject



15
16
17
# File 'lib/whenauser.rb', line 15

def self.default_ignored_crawlers
  %w(Baidu Gigabot Googlebot libwww-perl lwp-trivial msnbot SiteUptime Slurp WordPress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot)
end

.flush(env = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/whenauser.rb', line 24

def self.flush(env={})
  return if (events = WhenAUser.buffer).empty?
  WhenAUser.buffer = []
  events.each {|event| WhenAUser.prepare_event(event, env)}
  WhenAUser.queue.push(:payload => events)
  # WhenAUser.post_payload_to_token events.to_json, WhenAUser.token
end

.post_payload_to_token(payload, token) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/whenauser.rb', line 32

def self.post_payload_to_token(payload, token)
  uri = URI(WhenAUser.webhook_url + token)
  req = Net::HTTP::Post.new(uri.path)
  req.body = payload.to_json
  req.content_type = 'application/json'
  Net::HTTP.start(uri.host, uri.port) do |http|
    http.request(req)
  end
end

.send_event(event) ⇒ Object



19
20
21
22
# File 'lib/whenauser.rb', line 19

def self.send_event(event)
  logger.debug "============= #{event.inspect} ============="
  buffer << event
end