Skip to content

Commit

Permalink
Merge pull request #120 from reconbot/time.to_i
Browse files Browse the repository at this point in the history
Time.to_i
  • Loading branch information
zevarito committed Sep 5, 2013
2 parents 256d3f2 + 482f93f commit a7ce28e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/mixpanel/tracker.rb
Expand Up @@ -43,10 +43,12 @@ def ip
(@env['HTTP_X_FORWARDED_FOR'] || @env['REMOTE_ADDR'] || '').split(',').last
end

# Walk through each property and see if it is in the special_properties. If so, change the key to have a $ in front of it.
# Walk through each property and see if it is in the special_properties.
# If so, change the key to have a $ in front of it.
def properties_hash(properties, special_properties)
properties.inject({}) do |props, (key, value)|
key = "$#{key}" if special_properties.include?(key.to_s)
value = value.to_i if value.class == Time
props[key.to_sym] = value
props
end
Expand Down
25 changes: 23 additions & 2 deletions spec/mixpanel/tracker_spec.rb
Expand Up @@ -127,14 +127,18 @@
end

it "should append simple events" do
props = { :time => Time.now, :ip => 'ASDF' }
time = Time.now
props = { :time => time, :ip => 'ASDF' }
@mixpanel.append_track "Sign up", props
props[:time] = time.to_i
mixpanel_queue_should_include(@mixpanel, "track", "Sign up", props)
end

it "should append events with properties" do
props = { :referer => 'http://example.com', :time => Time.now, :ip => 'ASDF' }
time = Time.now
props = { :referer => 'http://example.com', :time => time, :ip => 'ASDF' }
@mixpanel.append_track "Sign up", props
props[:time] = time.to_i
mixpanel_queue_should_include(@mixpanel, "track", "Sign up", props)
end

Expand Down Expand Up @@ -194,4 +198,21 @@
w2.should_not == w
end
end

describe '#properties_hash' do
it "base64encodes json formatted data" do
properties = { :a => 4, :b => "foo"}
special_properties = ["a"]
hash = @mixpanel.send(:properties_hash, properties, special_properties)
hash.should eq({ :'$a' => 4, :b => "foo"})
end

it "converts Time objects into integers" do
time = Time.new
properties = { :a => time, :b => "foo"}
special_properties = []
hash = @mixpanel.send(:properties_hash, properties, special_properties)
hash.should eq({ :a => time.to_i, :b => "foo"})
end
end
end
10 changes: 5 additions & 5 deletions spec/spec_helper.rb
Expand Up @@ -8,12 +8,12 @@

MIX_PANEL_TOKEN = "e2d8b0bea559147844ffab3d607d26a6"


# json hashes have string keys, convert to json and back to compare hashes
def mixpanel_queue_should_include(mixpanel, type, *arguments)
mixpanel.queue.each do |event_type, event_arguments|
# hashes store keys in an undetermined order. convert to json and back and compare hash to hash, not json to json
unjsonify(event_arguments).should == json_and_back(arguments)
end
event = mixpanel.queue.detect { |event| event[0] == type }
event.should_not be_nil
event_arguments = event[1]
unjsonify(event_arguments).should == json_and_back(arguments)
end

def json_and_back array
Expand Down

0 comments on commit a7ce28e

Please sign in to comment.