Skip to content

Commit

Permalink
added Castle::Utils::Timestamp + small corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Jan 10, 2018
1 parent bfcd09e commit 5103347
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/castle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
require 'openssl'
require 'net/http'
require 'json'
require 'time'

require 'castle/version'
require 'castle/errors'
require 'castle/command'
require 'castle/utils'
require 'castle/utils/merger'
require 'castle/utils/cloner'
require 'castle/utils/timestamp'
require 'castle/context_merger'
require 'castle/context_sanitizer'
require 'castle/default_context'
Expand Down
14 changes: 7 additions & 7 deletions lib/castle/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ def from_request(request, options = {})
)
end

def to_context(request, context_options = {})
default_context = Castle::DefaultContext.new(request, context_options[:cookies]).call
Castle::ContextMerger.call(default_context, context_options[:context])
def to_context(request, options = {})
default_context = Castle::DefaultContext.new(request, options[:cookies]).call
Castle::ContextMerger.call(default_context, options[:context])
end

def to_options(options = {})
options[:timestamp] ||= Time.now.iso8601
options[:timestamp] ||= Castle::Utils::Timestamp.call
options
end
end
Expand All @@ -34,7 +34,7 @@ def authenticate(options = {})
options = Castle::Utils.deep_symbolize_keys(options || {})

if tracked?
options[:timestamp] = @timestamp if @timestamp
options[:timestamp] ||= @timestamp if @timestamp
command = Castle::Commands::Authenticate.new(@context).build(options)
begin
@api.request(command).merge(failover: false, failover_reason: nil)
Expand All @@ -50,7 +50,7 @@ def identify(options = {})
options = Castle::Utils.deep_symbolize_keys(options || {})

return unless tracked?
options[:timestamp] = @timestamp if @timestamp
options[:timestamp] ||= @timestamp if @timestamp

command = Castle::Commands::Identify.new(@context).build(options)
@api.request(command)
Expand All @@ -60,7 +60,7 @@ def track(options = {})
options = Castle::Utils.deep_symbolize_keys(options || {})

return unless tracked?
options[:timestamp] = @timestamp if @timestamp
options[:timestamp] ||= @timestamp if @timestamp

command = Castle::Commands::Track.new(@context).build(options)
@api.request(command)
Expand Down
3 changes: 2 additions & 1 deletion lib/castle/commands/authenticate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def build(options = {})
context = ContextSanitizer.call(context)

Castle::Command.new('authenticate',
options.merge(context: context, sent_at: Time.now.iso8601),
options.merge(context: context,
sent_at: Castle::Utils::Timestamp.call),
:post)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/castle/commands/identify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def build(options = {})
context = ContextSanitizer.call(context)

Castle::Command.new('identify',
options.merge(context: context, sent_at: Time.now.iso8601),
options.merge(context: context,
sent_at: Castle::Utils::Timestamp.call),
:post)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/castle/commands/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def build(options = {})
context = ContextSanitizer.call(context)

Castle::Command.new('track',
options.merge(context: context, sent_at: Time.now.iso8601),
options.merge(context: context,
sent_at: Castle::Utils::Timestamp.call),
:post)
end

Expand Down
1 change: 1 addition & 0 deletions lib/castle/context_sanitizer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Castle
# removes not proper active flag values
class ContextSanitizer
class << self
def call(context)
Expand Down
12 changes: 12 additions & 0 deletions lib/castle/utils/timestamp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Castle
module Utils
# generates proper timestamp
class Timestamp
def self.call
Time.now.utc.iso8601(3)
end
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/castle/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
end

let(:time_now) { Time.now }
let(:time_auto) { time_now.iso8601 }
let(:time_user) { (Time.now - 10_000).iso8601 }
let(:time_auto) { time_now.utc.iso8601(3) }
let(:time_user) { (Time.now - 10_000).utc.iso8601(3) }

before do
Timecop.freeze(time_now)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/castle/commands/authenticate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:default_payload) { { event: '$login.authenticate', user_id: '1234', sent_at: time_auto } }

let(:time_now) { Time.now }
let(:time_auto) { time_now.iso8601 }
let(:time_auto) { time_now.utc.iso8601(3) }

before do
Timecop.freeze(time_now)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/castle/commands/identify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:default_payload) { { user_id: '1234', sent_at: time_auto } }

let(:time_now) { Time.now }
let(:time_auto) { time_now.iso8601 }
let(:time_auto) { time_now.utc.iso8601(3) }

before do
Timecop.freeze(time_now)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/castle/commands/track_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:default_payload) { { event: '$login.track', sent_at: time_auto } }

let(:time_now) { Time.now }
let(:time_auto) { time_now.iso8601 }
let(:time_auto) { time_now.utc.iso8601(3) }

before do
Timecop.freeze(time_now)
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/castle/utils/timestamp_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

describe Castle::Utils::Timestamp do
subject { described_class.call }

let(:time_string) { '2018-01-10T14:14:24.407Z' }
let(:time) { Time.parse(time_string) }

before do
Timecop.freeze(time)
end

after do
Timecop.return
end

describe '#call' do
it do
is_expected.to eql(time_string)
end
end
end

0 comments on commit 5103347

Please sign in to comment.