Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

reorganize session creation

  • Loading branch information...
commit 1853cbd158808d925dec8bf176193da361dfb56e 1 parent 9ff3227
Jacques Crocker authored

Showing 1 changed file with 17 additions and 12 deletions. Show diff stats Hide diff stats

  1. +17 12 lib/aarrr/session.rb
29 lib/aarrr/session.rb
@@ -6,13 +6,12 @@ module AARRR
6 6 class Session
7 7 attr_accessor :id
8 8
9   - def initialize(env_or_object = nil)
10   - # find or creates a session in the db based on the env or object
11   - #
12   - # TODO
13   - #
  9 + # find or creates a session in the db based on the env or object
  10 + def initialize(env_or_object = nil, data = nil)
14 11 self.id = parse_id(env_or_object) || BSON::ObjectId.new.to_s
15   - AARRR.users.update({"_id" => id}, {"$set" => user_data(env_or_object)}, :upsert => true)
  12 +
  13 + # perform upsert
  14 + update({"$set" => user_attributes(env_or_object).merge(data || {})}, :upsert => true)
16 15 end
17 16
18 17 # returns a reference the othe AARRR user
@@ -20,12 +19,18 @@ def user
20 19 AARRR.users.find(id)
21 20 end
22 21
  22 + # sets some additional data
23 23 def set_data(data)
24   - AARRR.users.update({"_id" => id}, {"data" => {"$set" => data}})
  24 + update({"data" => {"$set" => data}})
25 25 end
26 26
27 27 protected
28 28
  29 + # mark update
  30 + def update(data, options = {})
  31 + AARRR.users.update({"_id" => id}, data, options)
  32 + end
  33 +
29 34 # returns id
30 35 def parse_id(env_or_object)
31 36 # check for empty case or string
@@ -46,21 +51,21 @@ def parse_id(env_or_object)
46 51 end
47 52
48 53 # returns updates
49   - def user_data(env_or_object)
  54 + def user_attributes(env_or_object)
50 55 if env_or_object.is_a?(Hash)
51   - user_data = {}
  56 + user_attributes = {}
52 57
53 58 # referrer: HTTP_REFERER
54 59 referrer = env_or_object["HTTP_REFERER"]
55   - user_data["referrer"] = referrer if referrer
  60 + user_attributes["referrer"] = referrer if referrer
56 61
57 62 # ip_address: HTTP_X_REAL_IP || REMOTE_ADDR
58 63 ip_address = env_or_object["HTTP_X_REAL_IP"] || env_or_object["REMOTE_ADDR"]
59   - user_data["ip_address"] = ip_address if ip_address
  64 + user_attributes["ip_address"] = ip_address if ip_address
60 65
61 66 # TODO: additional data from the env for the user
62 67
63   - user_data
  68 + user_attributes
64 69 else
65 70 {}
66 71 end

0 comments on commit 1853cbd

Please sign in to comment.
Something went wrong with that request. Please try again.