-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add auto-generated timestamp and sent_at (ruby-sdk) #97
Changes from 5 commits
2e08971
61b7cbf
dca0c74
0b55c88
bfcd09e
5103347
a915953
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,6 @@ group :test do | |
gem 'coveralls_reborn' | ||
gem 'rspec' | ||
gem 'simplecov' | ||
gem 'timecop' | ||
gem 'webmock' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,28 @@ module Castle | |
class Client | ||
class << self | ||
def from_request(request, options = {}) | ||
new(to_context(request, options), options[:do_not_track]) | ||
new( | ||
to_context(request, options), | ||
to_options(options) | ||
) | ||
end | ||
|
||
def to_context(request, options = {}) | ||
default_context = Castle::DefaultContext.new(request, options[:cookies]).call | ||
Castle::ContextMerger.new(default_context).call(options[:context] || {}) | ||
def to_context(request, context_options = {}) | ||
default_context = Castle::DefaultContext.new(request, context_options[:cookies]).call | ||
Castle::ContextMerger.call(default_context, context_options[:context]) | ||
end | ||
|
||
def to_options(options = {}) | ||
options[:timestamp] ||= Time.now.iso8601 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
options | ||
end | ||
end | ||
|
||
attr_accessor :api | ||
|
||
def initialize(context, do_not_track = false) | ||
@do_not_track = do_not_track | ||
def initialize(context, options = {}) | ||
@do_not_track = options[:do_not_track] | ||
@timestamp = options[:timestamp] | ||
@context = context | ||
@api = API.new | ||
end | ||
|
@@ -25,6 +34,7 @@ def authenticate(options = {}) | |
options = Castle::Utils.deep_symbolize_keys(options || {}) | ||
|
||
if tracked? | ||
options[:timestamp] = @timestamp if @timestamp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if we should allow it for |
||
command = Castle::Commands::Authenticate.new(@context).build(options) | ||
begin | ||
@api.request(command).merge(failover: false, failover_reason: nil) | ||
|
@@ -40,6 +50,7 @@ def identify(options = {}) | |
options = Castle::Utils.deep_symbolize_keys(options || {}) | ||
|
||
return unless tracked? | ||
options[:timestamp] = @timestamp if @timestamp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd change it for all methods to |
||
|
||
command = Castle::Commands::Identify.new(@context).build(options) | ||
@api.request(command) | ||
|
@@ -49,6 +60,7 @@ def track(options = {}) | |
options = Castle::Utils.deep_symbolize_keys(options || {}) | ||
|
||
return unless tracked? | ||
options[:timestamp] = @timestamp if @timestamp | ||
|
||
command = Castle::Commands::Track.new(@context).build(options) | ||
@api.request(command) | ||
|
@@ -70,7 +82,7 @@ def tracked? | |
|
||
def setup_context(request, cookies, additional_context) | ||
default_context = Castle::DefaultContext.new(request, cookies).call | ||
Castle::ContextMerger.new(default_context).call(additional_context || {}) | ||
Castle::ContextMerger.call(default_context, additional_context) | ||
end | ||
|
||
def failover_response_or_raise(failover_response, error) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,18 @@ | |
module Castle | ||
module Commands | ||
class Authenticate | ||
include WithContext | ||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def build(options = {}) | ||
validate!(options) | ||
build_context!(options) | ||
context = ContextMerger.call(@context, options[:context]) | ||
context = ContextSanitizer.call(context) | ||
|
||
Castle::Command.new('authenticate', options, :post) | ||
Castle::Command.new('authenticate', | ||
options.merge(context: context, sent_at: Time.now.iso8601), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
:post) | ||
end | ||
|
||
private | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,18 @@ | |
module Castle | ||
module Commands | ||
class Identify | ||
include WithContext | ||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def build(options = {}) | ||
validate!(options) | ||
build_context!(options) | ||
context = ContextMerger.call(@context, options[:context]) | ||
context = ContextSanitizer.call(context) | ||
|
||
Castle::Command.new('identify', options, :post) | ||
Castle::Command.new('identify', | ||
options.merge(context: context, sent_at: Time.now.iso8601), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment on |
||
:post) | ||
end | ||
|
||
private | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,18 @@ | |
module Castle | ||
module Commands | ||
class Track | ||
include WithContext | ||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def build(options = {}) | ||
validate!(options) | ||
build_context!(options) | ||
context = ContextMerger.call(@context, options[:context]) | ||
context = ContextSanitizer.call(context) | ||
|
||
Castle::Command.new('track', options, :post) | ||
Castle::Command.new('track', | ||
options.merge(context: context, sent_at: Time.now.iso8601), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment on |
||
:post) | ||
end | ||
|
||
private | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
class ContextSanitizer | ||
class << self | ||
def call(context) | ||
sanitized_active_mode(context) || {} | ||
end | ||
|
||
private | ||
|
||
def sanitized_active_mode(context) | ||
return context unless context && context.key?(:active) | ||
return context if [true, false].include?(context[:active]) | ||
context.reject { |key| key == :active } | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why renaming to
context_options
, I think if any I would call itadditional_context
see other methods