sift 1.0.12 → 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,16 @@ require 'rubygems'
6
6
  require 'sift'
7
7
  require 'multi_json'
8
8
 
9
- api_key = "" # TODO: Set your API key here
9
+ TIMEOUT = 5
10
+
11
+ if ARGV.length.zero?
12
+ puts "Please specify an API key"
13
+ puts "usage:"
14
+ puts " ruby simple <API KEY>"
15
+ puts
16
+ exit
17
+ end
18
+ api_key = ARGV[0];
10
19
 
11
20
  class MyLogger
12
21
  def warn(e)
@@ -26,25 +35,30 @@ class MyLogger
26
35
  end
27
36
  end
28
37
 
38
+ def handle_response(response)
39
+ if response.nil?
40
+ puts 'Error: there was an HTTP error calling through the API'
41
+ else
42
+ puts 'Successfully sent request; was ok? : ' + response.ok?.to_s
43
+ puts 'API error message : ' + response.api_error_message.to_s
44
+ puts 'API status code : ' + response.api_status.to_s
45
+ puts 'HTTP status code : ' + response.http_status_code.to_s
46
+ puts 'original request : ' + response.original_request.to_s
47
+ end
48
+ end
49
+
29
50
  Sift.logger = MyLogger.new
30
51
 
31
52
  client = Sift::Client.new(api_key)
53
+ user_id = "3"
32
54
  event = "my_custom_event"
33
55
  properties = {
34
56
  "my_custom_field1" => "my custom value 1",
35
57
  "my_custom_field2" => "my custom value 2",
36
- "$user_id" => "3",
58
+ "$user_id" => user_id,
37
59
  "$time" => Time.now.to_i,
38
60
  }
39
61
 
40
- response = client.track(event, properties)
41
- if response.nil?
42
- puts 'Error: there was an HTTP error calling through the API'
43
- else
44
- puts 'Successfully sent request; was ok? : ' + response.ok?.to_s
45
- puts 'API error message : ' + response.api_error_message.to_s
46
- puts 'API status code : ' + response.api_status.to_s
47
- puts 'HTTP status code : ' + response.http_status_code.to_s
48
- puts 'original request : ' + response.original_request.to_s
49
- end
62
+ handle_response client.track(event, properties, TIMEOUT)
63
+ handle_response client.label(user_id, {"$is_bad" => true, "$reasons" => ["$chargeback", "$spam"], "$description" => "free form text here" }, TIMEOUT)
50
64
 
@@ -8,6 +8,11 @@ module Sift
8
8
  "/v202/events"
9
9
  end
10
10
 
11
+ def self.current_users_label_api_path(user_id)
12
+ # This API version is a minor version ahead of the /events API
13
+ "/v203/users/#{URI.encode(user_id)}/labels"
14
+ end
15
+
11
16
  # Sets the Output logger to use within the client. This can be left uninitializaed
12
17
  # but is useful for debugging.
13
18
  def self.logger=(logger)
@@ -76,28 +76,33 @@ module Sift
76
76
  # A hash of name-value pairs that specify the event-specific attributes to track.
77
77
  # This parameter must be specified.
78
78
  #
79
- # timeout
79
+ # timeout (optional)
80
80
  # The number of seconds to wait before failing the request. By default this is
81
81
  # configured to 2 seconds (see above). This parameter is optional.
82
82
  #
83
+ # path (optional)
84
+ # Overrides the default API path with a different URL.
85
+ #
86
+ #
83
87
  # == Returns:
84
88
  # In the case of an HTTP error (timeout, broken connection, etc.), this
85
89
  # method returns nil; otherwise, a Response object is returned and captures
86
90
  # the status message and status code. In general, you can ignore the returned
87
91
  # result, though.
88
92
  #
89
- def track(event, properties = {}, timeout = nil)
93
+ def track(event, properties = {}, timeout = nil, path = nil)
90
94
 
91
95
  raise(RuntimeError, "event must be a string") if event.nil? || event.to_s.empty?
92
96
  raise(RuntimeError, "properties cannot be empty") if properties.empty?
93
97
 
98
+ path ||= @path
94
99
  options = {
95
100
  :body => MultiJson.dump(properties.merge({"$type" => event,
96
101
  "$api_key" => @api_key})),
97
102
  }
98
103
  options.merge!(:timeout => timeout) unless timeout.nil?
99
104
  begin
100
- response = self.class.post(@path, options)
105
+ response = self.class.post(path, options)
101
106
  Response.new(response.body, response.code)
102
107
  rescue StandardError => e
103
108
  Sift.warn("Failed to track event: " + e.to_s)
@@ -114,17 +119,43 @@ module Sift
114
119
  # event calls.
115
120
  #
116
121
  # == Returns:
117
- # A Response object is returned and captures
118
- # the status message and status code. In general, you can ignore the returned
119
- # result, though.
122
+ # A Response object is returned and captures the status message and
123
+ # status code. In general, you can ignore the returned result, though.
120
124
  #
121
125
  def score(user_id)
122
126
 
123
127
  raise(RuntimeError, "user_id must be a string") if user_id.nil? || user_id.to_s.empty?
124
128
 
125
- response = self.class.get('/v203/score/'+user_id)
129
+ response = self.class.get('/v203/score/' + user_id)
126
130
  Response.new(response.body, response.code)
127
131
 
128
132
  end
133
+
134
+ # Labels a user as either good or bad. This call is blocking.
135
+ #
136
+ # == Parameters:
137
+ # user_id
138
+ # A user's id. This id should be the same as the user_id used in
139
+ # event calls.
140
+ #
141
+ # properties
142
+ # A hash of name-value pairs that specify the label attributes.
143
+ # This parameter must be specified.
144
+ #
145
+ # timeout (optional)
146
+ # The number of seconds to wait before failing the request. By default this is
147
+ # configured to 2 seconds (see above). This parameter is optional.
148
+ #
149
+ # == Returns:
150
+ # A Response object is returned and captures the status message and
151
+ # status code. In general, you can ignore the returned result, though.
152
+ #
153
+ def label(user_id, properties = {}, timeout = nil)
154
+
155
+ raise(RuntimeError, "user_id must be a string") if user_id.nil? || user_id.to_s.empty?
156
+
157
+ path = Sift.current_users_label_api_path(user_id)
158
+ track("$label", properties, timeout, path)
159
+ end
129
160
  end
130
161
  end
@@ -1,3 +1,3 @@
1
1
  module Sift
2
- VERSION = "1.0.12"
2
+ VERSION = "1.0.13"
3
3
  end
@@ -20,12 +20,12 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  # Gems that must be intalled for sift to compile and build
23
- s.add_development_dependency "rspec", "~> 2.9.0"
23
+ s.add_development_dependency "rspec", "~> 2.14.1"
24
24
  s.add_development_dependency "fakeweb", "~> 1.3.0"
25
25
 
26
26
  # Gems that must be intalled for sift to work
27
- s.add_dependency "httparty", ">= 0.10.0"
28
- s.add_dependency "multi_json", ">= 1.3.4"
27
+ s.add_dependency "httparty", ">= 0.12.0"
28
+ s.add_dependency "multi_json", ">= 1.8.2"
29
29
 
30
30
  s.add_development_dependency("rspec", ">= 2.0")
31
31
  s.add_development_dependency("rake")
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ describe Sift::Client do
4
+
5
+ def valid_label_properties
6
+ {
7
+ :$reasons => [ "$fake" ],
8
+ :$is_bad => true,
9
+ :$description => "Listed a fake item"
10
+ }
11
+ end
12
+
13
+ def fully_qualified_users_labels_endpoint(user_id)
14
+ Sift::Client::API_ENDPOINT + Sift.current_users_label_api_path(user_id)
15
+ end
16
+
17
+ it "Successfuly handles a $label with the v203 API and returns OK" do
18
+
19
+ response_json = { :status => 0, :error_message => "OK" }
20
+ user_id = "frodo_baggins"
21
+
22
+ FakeWeb.register_uri(:post, fully_qualified_users_labels_endpoint(user_id),
23
+ :body => MultiJson.dump(response_json),
24
+ :status => [Net::HTTPOK, "OK"],
25
+ :content_type => "text/json")
26
+
27
+ api_key = "foobar"
28
+ properties = valid_label_properties
29
+
30
+ response = Sift::Client.new(api_key).label(user_id, properties)
31
+ response.ok?.should eq(true)
32
+ response.api_status.should eq(0)
33
+ response.api_error_message.should eq("OK")
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sift
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.12
4
+ version: 1.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-14 00:00:00.000000000 Z
12
+ date: 2013-10-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 2.9.0
21
+ version: 2.14.1
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 2.9.0
29
+ version: 2.14.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: fakeweb
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: 0.10.0
53
+ version: 0.12.0
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 0.10.0
61
+ version: 0.12.0
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: multi_json
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
- version: 1.3.4
69
+ version: 1.8.2
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
- version: 1.3.4
77
+ version: 1.8.2
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: rspec
80
80
  requirement: !ruby/object:Gem::Requirement
@@ -126,6 +126,7 @@ files:
126
126
  - sift.gemspec
127
127
  - spec/integration/sift_spec.rb
128
128
  - spec/spec_helper.rb
129
+ - spec/unit/client_label_spec.rb
129
130
  - spec/unit/client_spec.rb
130
131
  - spec/unit/sift_spec.rb
131
132
  homepage: http://siftscience.com
@@ -142,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
143
  version: '0'
143
144
  segments:
144
145
  - 0
145
- hash: 2652595894011283965
146
+ hash: 4230512473263984627
146
147
  required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  none: false
148
149
  requirements:
@@ -151,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  version: '0'
152
153
  segments:
153
154
  - 0
154
- hash: 2652595894011283965
155
+ hash: 4230512473263984627
155
156
  requirements: []
156
157
  rubyforge_project: sift
157
158
  rubygems_version: 1.8.25
@@ -161,5 +162,6 @@ summary: Sift Science Ruby API Gem
161
162
  test_files:
162
163
  - spec/integration/sift_spec.rb
163
164
  - spec/spec_helper.rb
165
+ - spec/unit/client_label_spec.rb
164
166
  - spec/unit/client_spec.rb
165
167
  - spec/unit/sift_spec.rb