Skip to content

Commit

Permalink
Generate unix timestamps using #to_i directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jgwhite committed Apr 3, 2013
1 parent 247ca8a commit 4f6ca94
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
tzinfo (0.3.37)

PLATFORMS
ruby
Expand All @@ -72,3 +73,4 @@ DEPENDENCIES
rspec-mocks
sinatra (~> 1.3.1)
thin (~> 1.4.1)
tzinfo
1 change: 1 addition & 0 deletions intercom-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'pry'
s.add_development_dependency 'sinatra', '~> 1.3.1'
s.add_development_dependency 'thin', '~> 1.4.1'
s.add_development_dependency 'tzinfo'
end
2 changes: 1 addition & 1 deletion lib/intercom-rails/script_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def widget_options_from_config

def convert_dates_to_unix_timestamps(object)
return Hash[object.map { |k, v| [k, convert_dates_to_unix_timestamps(v)] }] if object.is_a?(Hash)
return object.strftime('%s').to_i if object.respond_to?(:strftime)
return object.to_i if object.is_a?(Time) || object.is_a?(DateTime)
object
end

Expand Down
7 changes: 7 additions & 0 deletions test/intercom-rails/script_tag_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_support/core_ext/string/output_safety'
require 'active_support/time'
require 'test_setup'

class ScriptTagTest < MiniTest::Unit::TestCase
Expand All @@ -23,6 +24,12 @@ def test_converts_times_to_unix_timestamps
now = Time.now
nested_time = ScriptTag.new(:user_details => {:custom_data => {"something" => now}})
assert_equal now.to_i, nested_time.intercom_settings[:custom_data]["something"]

utc_time = Time.utc(2013,04,03)
time_zone = ActiveSupport::TimeZone.new('London')
time_with_zone = ActiveSupport::TimeWithZone.new(utc_time, time_zone)
time_from_time_with_zone = ScriptTag.new(:user_details => {:created_at => time_with_zone})
assert_equal utc_time.to_i, time_from_time_with_zone.intercom_settings[:created_at]
end

def test_strips_out_nil_entries_for_standard_attributes
Expand Down

0 comments on commit 4f6ca94

Please sign in to comment.