Class: Sift::Client
Overview
This class wraps accesses through the API
Constant Summary collapse
- API_ENDPOINT =
"https://api.siftscience.com"- API_TIMEOUT =
2
Instance Method Summary collapse
-
#initialize(api_key, path = Sift.current_rest_api_path) ⇒ Client
constructor
Constructor.
-
#label(user_id, properties = {}, timeout = nil) ⇒ Object
Labels a user as either good or bad.
-
#score(user_id) ⇒ Object
Retrieves a user’s fraud score from the Sift Science API.
-
#track(event, properties = {}, timeout = nil, path = nil) ⇒ Object
Tracks an event and associated properties through the Sift Science API.
Constructor Details
#initialize(api_key, path = Sift.current_rest_api_path) ⇒ Client
Constructor
Parameters:
api_key
The Sift Science API key associated with your customer account. This parameter
cannot be nil or blank.
path
The path to the event API, e.g., "/v201/events"
60 61 62 63 64 |
# File 'lib/sift/client.rb', line 60 def initialize(api_key, path = Sift.current_rest_api_path) @api_key = api_key.to_s @path = path raise(RuntimeError, "api_key is required") if @api_key.nil? || @api_key.empty? end |
Instance Method Details
#label(user_id, properties = {}, timeout = nil) ⇒ Object
Labels a user as either good or bad. This call is blocking.
Parameters:
user_id
A user's id. This id should be the same as the user_id used in
event calls.
properties
A hash of name-value pairs that specify the label attributes.
This parameter must be specified.
timeout (optional)
The number of seconds to wait before failing the request. By default this is
configured to 2 seconds (see above). This parameter is optional.
Returns:
A Response object is returned and captures the status and
status code. In general, you can ignore the returned result, though.
153 154 155 156 157 158 159 |
# File 'lib/sift/client.rb', line 153 def label(user_id, properties = {}, timeout = nil) raise(RuntimeError, "user_id must be a string") if user_id.nil? || user_id.to_s.empty? path = Sift.current_users_label_api_path(user_id) track("$label", properties, timeout, path) end |
#score(user_id) ⇒ Object
Retrieves a user’s fraud score from the Sift Science API. This call is blocking.
Parameters:
user_id
A user's id. This id should be the same as the user_id used in
event calls.
Returns:
A Response object is returned and captures the status and
status code. In general, you can ignore the returned result, though.
125 126 127 128 129 130 131 132 |
# File 'lib/sift/client.rb', line 125 def score(user_id) raise(RuntimeError, "user_id must be a string") if user_id.nil? || user_id.to_s.empty? response = self.class.get('/v203/score/' + user_id) Response.new(response.body, response.code) end |
#track(event, properties = {}, timeout = nil, path = nil) ⇒ Object
Tracks an event and associated properties through the Sift Science API. This call is blocking.
Parameters:
event
The name of the event to send. This can be either a reserved event name, like
$transaction or $label or a custom event name (that does not start with a $).
This parameter must be specified.
properties
A hash of name-value pairs that specify the event-specific attributes to track.
This parameter must be specified.
timeout (optional)
The number of seconds to wait before failing the request. By default this is
configured to 2 seconds (see above). This parameter is optional.
path (optional)
Overrides the default API path with a different URL.
Returns:
In the case of an HTTP error (timeout, broken connection, etc.), this
method returns nil; otherwise, a Response object is returned and captures
the status and status code. In general, you can ignore the returned
result, though.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sift/client.rb', line 93 def track(event, properties = {}, timeout = nil, path = nil) raise(RuntimeError, "event must be a string") if event.nil? || event.to_s.empty? raise(RuntimeError, "properties cannot be empty") if properties.empty? path ||= @path = { :body => MultiJson.dump(properties.merge({"$type" => event, "$api_key" => @api_key})), } .merge!(:timeout => timeout) unless timeout.nil? begin response = self.class.post(path, ) Response.new(response.body, response.code) rescue StandardError => e Sift.warn("Failed to track event: " + e.to_s) Sift.warn(e.backtrace) end end |