datasift 1.5.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/.gitignore +1 -0
  2. data/LICENSE +1 -1
  3. data/README.md +6 -4
  4. data/VERSION +1 -1
  5. data/config.yml +2 -2
  6. data/datasift.gemspec +11 -11
  7. data/examples/historics.sh +2 -0
  8. data/examples/historics/create-from-csdl.rb +71 -0
  9. data/examples/historics/create-from-hash.rb +65 -0
  10. data/examples/historics/delete.rb +30 -0
  11. data/examples/historics/env.rb +37 -0
  12. data/examples/historics/list.rb +30 -0
  13. data/examples/historics/start.rb +30 -0
  14. data/examples/historics/stop.rb +30 -0
  15. data/examples/historics/view.rb +28 -0
  16. data/examples/push.sh +2 -0
  17. data/examples/push/delete.rb +33 -0
  18. data/examples/push/env.rb +53 -0
  19. data/examples/push/list.rb +30 -0
  20. data/examples/push/pause.rb +33 -0
  21. data/examples/push/push-from-hash.rb +72 -0
  22. data/examples/push/push-historic-from-csdl.rb +98 -0
  23. data/examples/push/push-stream-from-csdl.rb +70 -0
  24. data/examples/push/resume.rb +33 -0
  25. data/examples/push/stop.rb +33 -0
  26. data/examples/push/view-log.rb +45 -0
  27. data/examples/push/view.rb +31 -0
  28. data/lib/DataSift/apiclient.rb +20 -25
  29. data/lib/DataSift/definition.rb +97 -57
  30. data/lib/DataSift/exceptions.rb +25 -8
  31. data/lib/DataSift/historic.rb +321 -0
  32. data/lib/DataSift/mockapiclient.rb +23 -34
  33. data/lib/DataSift/push_definition.rb +115 -0
  34. data/lib/DataSift/push_subscription.rb +330 -0
  35. data/lib/DataSift/stream_consumer.rb +53 -70
  36. data/lib/DataSift/stream_consumer_http.rb +11 -15
  37. data/lib/DataSift/user.rb +189 -61
  38. data/lib/datasift.rb +5 -10
  39. data/test/helper.rb +80 -6
  40. data/test/test_definition.rb +0 -9
  41. data/test/test_historics.rb +233 -0
  42. data/test/test_pushdefinition.rb +92 -0
  43. data/test/test_pushsubscription.rb +17 -0
  44. data/test/test_user.rb +0 -6
  45. data/test/testdata.yml +26 -0
  46. metadata +38 -23
  47. data/test/test_live_api.rb +0 -100
data/lib/datasift.rb CHANGED
@@ -1,13 +1,5 @@
1
- #
2
- # datasift.rb - DataSift module base file
3
- #
4
- # Copyright (C) 2011 MediaSift Ltd
5
- #
6
- # == Overview
7
- #
8
- # This is the base file for the DataSift API library. Require this file to get
9
- # access to the full library functionality.
10
- #
1
+ #This is the base file for the DataSift API library. Require this file to get
2
+ #access to the full library functionality.
11
3
  require 'rubygems'
12
4
 
13
5
  dir = File.dirname(__FILE__)
@@ -15,5 +7,8 @@ require dir + '/DataSift/exceptions'
15
7
  require dir + '/DataSift/apiclient'
16
8
  require dir + '/DataSift/user'
17
9
  require dir + '/DataSift/definition'
10
+ require dir + '/DataSift/historic'
11
+ require dir + '/DataSift/push_definition'
12
+ require dir + '/DataSift/push_subscription'
18
13
  require dir + '/DataSift/stream_consumer'
19
14
  require dir + '/DataSift/stream_consumer_http'
data/test/helper.rb CHANGED
@@ -12,13 +12,87 @@ class Test::Unit::TestCase
12
12
  def init()
13
13
  @config = YAML::load(File.open(File.join(File.dirname(__FILE__), '..', 'config.yml')))
14
14
  @testdata = YAML::load(File.open(File.join(File.dirname(__FILE__), 'testdata.yml')))
15
+
16
+ # Initialise the test data (parse the dates, etc).
17
+ @testdata['historic_start'] = DateTime.parse(@testdata['historic_start'])
18
+ @testdata['historic_end'] = DateTime.parse(@testdata['historic_end'])
19
+ @testdata['historic_created_at'] = DateTime.parse(@testdata['historic_created_at'])
20
+ @testdata['push_created_at'] = DateTime.parse(@testdata['push_created_at'])
21
+ @testdata['push_last_request'] = DateTime.parse(@testdata['push_last_request'])
22
+ @testdata['push_last_success'] = DateTime.parse(@testdata['push_last_success'])
23
+ @testdata['historic_sources'] = @testdata['historic_sources'].split(',')
24
+
25
+ @user = DataSift::User.new(@config['username'], @config['api_key'])
26
+ @user.setApiClient(DataSift::MockApiClient.new())
27
+ @user.api_client.clearResponse()
28
+ end
29
+
30
+ def set204Response()
31
+ @user.api_client.setResponse(204, {}, 200, 150)
32
+ end
33
+
34
+ def setResponseToASingleHistoric(changes = {})
35
+ data = {
36
+ 'id' => @testdata['historic_playback_id'],
37
+ 'definition_id' => @testdata['definition_hash'],
38
+ 'name' => @testdata['historic_name'],
39
+ 'start' => @testdata['historic_start'].strftime('%s'),
40
+ 'end' => @testdata['historic_end'].strftime('%s'),
41
+ 'created_at' => @testdata['historic_created_at'].strftime('%s'),
42
+ 'status' => @testdata['historic_status'],
43
+ 'progress' => 0,
44
+ 'sources' => @testdata['historic_sources'],
45
+ 'sample' => @testdata['historic_sample'],
46
+ 'volume_info' => {
47
+ 'digg' => 9
48
+ },
49
+ }
50
+ @user.api_client.setResponse(200, data.merge(changes), 200, 150)
51
+ end
52
+
53
+ def configurePushDefinition(push)
54
+ push.output_type = @testdata['push_output_type']
55
+ @testdata['push_output_params'].each { |k,v| push.output_params[k] = v }
56
+ end
57
+
58
+ def setResponseToASingleSubscription(changes = {})
59
+ data = {
60
+ 'id' => @testdata['push_id'],
61
+ 'name' => @testdata['push_name'],
62
+ 'created_at' => @testdata['push_created_at'].strftime('%s'),
63
+ 'status' => @testdata['push_status'],
64
+ 'hash' => @testdata['definition_hash'],
65
+ 'hash_type' => @testdata['push_hash_type'],
66
+ 'output_type' => @testdata['push_output_type'],
67
+ 'output_params' => {
68
+ 'delivery_frequency' => @testdata['push_output_params']['delivery_frequency'],
69
+ 'url' => @testdata['push_output_params']['url'],
70
+ 'auth' => {
71
+ 'type' => @testdata['push_output_params']['auth_type'],
72
+ 'username' => @testdata['push_output_params']['auth_username'],
73
+ 'password' => @testdata['push_output_params']['auth_password'],
74
+ },
75
+ },
76
+ 'last_request' => @testdata['push_last_request'].strftime('%s'),
77
+ 'last_success' => @testdata['push_last_success'].strftime('%s'),
78
+ }
79
+ @user.api_client.setResponse(200, data.merge(changes), 200, 150)
15
80
  end
16
81
 
17
- def initUser(mock = true)
18
- @user = DataSift::User.new(@config['username'], @config['api_key'])
19
- if mock
20
- @user.setApiClient(DataSift::MockApiClient.new())
21
- @user.api_client.clearResponse()
22
- end
82
+ def checkSubscription(subscription)
83
+ assert_equal @testdata['push_id'], subscription.id
84
+ assert_equal @testdata['push_name'], subscription.name
85
+ assert_equal @testdata['push_created_at'], subscription.created_at
86
+ assert_equal @testdata['push_status'], subscription.status
87
+ assert_equal @testdata['definition_hash'], subscription.hash
88
+ assert_equal @testdata['push_hash_type'], subscription.hash_type
89
+ assert_equal @testdata['push_output_type'], subscription.output_type
90
+ assert_equal @testdata['push_output_params']['delivery_frequency'], subscription.output_params['delivery_frequency']
91
+ assert_equal @testdata['push_output_params']['url'], subscription.output_params['url']
92
+ assert_equal @testdata['push_output_params']['auth_type'], subscription.output_params['auth.type']
93
+ assert_equal @testdata['push_output_params']['auth_username'], subscription.output_params['auth.username']
94
+ assert_equal @testdata['push_output_params']['auth_password'], subscription.output_params['auth.password']
95
+ assert_equal @testdata['push_last_request'], subscription.last_request
96
+ assert_equal @testdata['push_last_success'], subscription.last_success
23
97
  end
24
98
  end
@@ -4,7 +4,6 @@ class TestDefinition < Test::Unit::TestCase
4
4
  context "Given an empty Definition object" do
5
5
  setup do
6
6
  init()
7
- initUser()
8
7
  @definition = @user.createDefinition()
9
8
  end
10
9
 
@@ -22,7 +21,6 @@ class TestDefinition < Test::Unit::TestCase
22
21
  context "Given a Definition object with CSDL" do
23
22
  setup do
24
23
  init()
25
- initUser()
26
24
  @definition = @user.createDefinition(@testdata['definition'])
27
25
  end
28
26
 
@@ -35,7 +33,6 @@ class TestDefinition < Test::Unit::TestCase
35
33
  context "Given a Definition object with CSDL plus padding" do
36
34
  setup do
37
35
  init()
38
- initUser()
39
36
  @definition = @user.createDefinition(" " + @testdata['definition'] + " ")
40
37
  end
41
38
 
@@ -54,7 +51,6 @@ class TestDefinition < Test::Unit::TestCase
54
51
  context "When trying to create a Definition object with an invalid CSDL" do
55
52
  setup do
56
53
  init()
57
- initUser()
58
54
  end
59
55
 
60
56
  should "raise an InvalidDataError" do
@@ -65,7 +61,6 @@ class TestDefinition < Test::Unit::TestCase
65
61
  context "Given a Definition object with a valid CSDL" do
66
62
  setup do
67
63
  init()
68
- initUser()
69
64
  @definition = @user.createDefinition(@testdata['definition'])
70
65
  end
71
66
 
@@ -119,7 +114,6 @@ class TestDefinition < Test::Unit::TestCase
119
114
  context "Given a Definition object with an invalid CSDL" do
120
115
  setup do
121
116
  init()
122
- initUser()
123
117
  @definition = @user.createDefinition(@testdata['invalid_definition'])
124
118
  end
125
119
 
@@ -141,7 +135,6 @@ class TestDefinition < Test::Unit::TestCase
141
135
  context "The DPU returned from a valid Definition object" do
142
136
  setup do
143
137
  init()
144
- initUser()
145
138
  @definition = @user.createDefinition(@testdata['definition'])
146
139
  # Compile the definition first
147
140
  @user.api_client.setResponse(200, {
@@ -182,7 +175,6 @@ class TestDefinition < Test::Unit::TestCase
182
175
  context "Buffered data returned by a valid Definition object" do
183
176
  setup do
184
177
  init()
185
- initUser()
186
178
  @definition = @user.createDefinition(@testdata['definition'])
187
179
  # Compile the definition first
188
180
  @user.api_client.setResponse(200, {
@@ -262,7 +254,6 @@ class TestDefinition < Test::Unit::TestCase
262
254
  context "A StreamConsumer object returned by a valid Definition object" do
263
255
  setup do
264
256
  init()
265
- initUser()
266
257
  @definition = @user.createDefinition(@testdata['definition'])
267
258
  # Compile the definition first
268
259
  @user.api_client.setResponse(200, {
@@ -0,0 +1,233 @@
1
+ require 'helper'
2
+
3
+ class TestHistorics < Test::Unit::TestCase
4
+ context "Given a new Historic object from a stream hash" do
5
+ setup do
6
+ init()
7
+ # Create the historic (API response is for compiling the definition)
8
+ @historic = @user.createHistoric(@testdata['definition_hash'], @testdata['historic_start'], @testdata['historic_end'], @testdata['historic_sources'], @testdata['historic_sample'], @testdata['historic_name'])
9
+ end
10
+
11
+ should "be a Historic object" do
12
+ assert_not_nil @historic
13
+ assert @historic.kind_of?(DataSift::Historic)
14
+ end
15
+
16
+ should "have the correct definition_hash" do
17
+ assert_equal @testdata['definition_hash'], @historic.stream_hash
18
+ end
19
+
20
+ should "have the correct name" do
21
+ assert_equal @testdata['historic_name'], @historic.name
22
+ end
23
+
24
+ should "have the correct start_date" do
25
+ assert_equal @testdata['historic_start'], @historic.start_date
26
+ end
27
+
28
+ should "have the correct end_date" do
29
+ assert_equal @testdata['historic_end'], @historic.end_date
30
+ end
31
+
32
+ should "have the correct status" do
33
+ assert_equal @testdata['historic_status'], @historic.status
34
+ end
35
+
36
+ should "have the correct progress" do
37
+ assert_equal 0, @historic.progress
38
+ end
39
+
40
+ should "have the correct sample" do
41
+ assert_equal @testdata['historic_sample'], @historic.sample
42
+ end
43
+
44
+ should "be able to change the name before preparing" do
45
+ assert_equal @testdata['historic_name'], @historic.name
46
+
47
+ @historic.name = 'new name'
48
+
49
+ assert_equal 'new name', @historic.name
50
+ end
51
+
52
+ should "be able to prepare the query" do
53
+ setResponseToASingleHistoric({
54
+ 'dpus' => @testdata['historic_dpus'],
55
+ 'availability' => @testdata['historic_availability']
56
+ })
57
+ @historic.prepare()
58
+ end
59
+
60
+ should "not be able to prepare it more than once" do
61
+ setResponseToASingleHistoric({
62
+ 'dpus' => @testdata['historic_dpus'],
63
+ 'availability' => @testdata['historic_availability']
64
+ })
65
+ @historic.prepare()
66
+ assert_raise(DataSift::InvalidDataError) { @historic.prepare() }
67
+ end
68
+
69
+ should "be able to change the name after preparing" do
70
+ setResponseToASingleHistoric({
71
+ 'dpus' => @testdata['historic_dpus'],
72
+ 'availability' => @testdata['historic_availability']
73
+ })
74
+ @historic.prepare()
75
+
76
+ assert_equal @testdata['historic_name'], @historic.name
77
+
78
+ new_name = 'new name'
79
+ setResponseToASingleHistoric({ 'name' => new_name })
80
+ @historic.name = new_name
81
+
82
+ assert_equal new_name, @historic.name
83
+ end
84
+
85
+ should "be able to start the query" do
86
+ setResponseToASingleHistoric({
87
+ 'dpus' => @testdata['historic_dpus'],
88
+ 'availability' => @testdata['historic_availability']
89
+ })
90
+ @historic.prepare()
91
+
92
+ set204Response()
93
+ @historic.start()
94
+ end
95
+
96
+ should "be able to stop the query" do
97
+ setResponseToASingleHistoric({
98
+ 'dpus' => @testdata['historic_dpus'],
99
+ 'availability' => @testdata['historic_availability']
100
+ })
101
+ @historic.prepare()
102
+
103
+ set204Response()
104
+ @historic.stop()
105
+ end
106
+
107
+ should "be able to delete the query" do
108
+ setResponseToASingleHistoric({
109
+ 'dpus' => @testdata['historic_dpus'],
110
+ 'availability' => @testdata['historic_availability']
111
+ })
112
+ @historic.prepare()
113
+
114
+ set204Response()
115
+ @historic.delete()
116
+ end
117
+
118
+ should "not be able to start the query after deletion" do
119
+ setResponseToASingleHistoric({
120
+ 'dpus' => @testdata['historic_dpus'],
121
+ 'availability' => @testdata['historic_availability']
122
+ })
123
+ @historic.prepare()
124
+
125
+ set204Response()
126
+ @historic.delete()
127
+
128
+ assert_raise(DataSift::InvalidDataError) { @historic.start() }
129
+ end
130
+ end
131
+
132
+ context "Given a new Historic object from a Definition" do
133
+ setup do
134
+ init()
135
+ # Create the definition
136
+ @definition = @user.createDefinition(@testdata['definition'])
137
+ # Create the historic (API response is for compiling the definition)
138
+ @user.api_client.setResponse(200, {
139
+ 'hash' => @testdata['definition_hash'],
140
+ 'created_at' => Time.now.strftime('%Y-%m-%d %H:%M:%S'),
141
+ 'dpu' => 10,
142
+ }, 200, 150)
143
+ @historic = @definition.createHistoric(@testdata['historic_start'], @testdata['historic_end'], @testdata['historic_sources'], @testdata['historic_sample'], @testdata['historic_name'])
144
+ end
145
+
146
+ should "be a Historic object" do
147
+ assert_not_nil @historic
148
+ end
149
+
150
+ should "have the correct definition_hash" do
151
+ assert_equal @testdata['definition_hash'], @historic.stream_hash
152
+ end
153
+
154
+ should "have the correct name" do
155
+ assert_equal @testdata['historic_name'], @historic.name
156
+ end
157
+
158
+ should "have the correct start_date" do
159
+ assert_equal @testdata['historic_start'], @historic.start_date
160
+ end
161
+
162
+ should "have the correct end_date" do
163
+ assert_equal @testdata['historic_end'], @historic.end_date
164
+ end
165
+
166
+ should "have the correct status" do
167
+ assert_equal @testdata['historic_status'], @historic.status
168
+ end
169
+
170
+ should "have the correct progress" do
171
+ assert_equal 0, @historic.progress
172
+ end
173
+
174
+ should "have the correct sample" do
175
+ assert_equal @testdata['historic_sample'], @historic.sample
176
+ end
177
+ end
178
+
179
+ context "Given a Historic object retrieved from the API" do
180
+ setup do
181
+ init()
182
+ # Create the historic (API response is for compiling the definition)
183
+ setResponseToASingleHistoric()
184
+ @historic = @user.getHistoric(@testdata['historic_playback_id'])
185
+ end
186
+
187
+ should "be a Historic object" do
188
+ assert_not_nil @historic
189
+ end
190
+
191
+ should "have the correct definition_hash" do
192
+ assert_equal @testdata['definition_hash'], @historic.stream_hash
193
+ end
194
+
195
+ should "have the correct name" do
196
+ assert_equal @testdata['historic_name'], @historic.name
197
+ end
198
+
199
+ should "have the correct start_date" do
200
+ assert_equal @testdata['historic_start'], @historic.start_date
201
+ end
202
+
203
+ should "have the correct end_date" do
204
+ assert_equal @testdata['historic_end'], @historic.end_date
205
+ end
206
+
207
+ should "have the correct status" do
208
+ assert_equal @testdata['historic_status'], @historic.status
209
+ end
210
+
211
+ should "have the correct progress" do
212
+ assert_equal 0, @historic.progress
213
+ end
214
+
215
+ should "have the correct sample" do
216
+ assert_equal @testdata['historic_sample'], @historic.sample
217
+ end
218
+
219
+ should "not be able to prepare the query" do
220
+ assert_raise(DataSift::InvalidDataError) { @historic.prepare() }
221
+ end
222
+
223
+ should "be able to change the name" do
224
+ assert_equal @testdata['historic_name'], @historic.name
225
+
226
+ new_name = 'new name'
227
+ setResponseToASingleHistoric({ 'name' => new_name })
228
+ @historic.name = new_name
229
+
230
+ assert_equal new_name, @historic.name
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,92 @@
1
+ require 'helper'
2
+
3
+ class TestPushDefinition < Test::Unit::TestCase
4
+ context "Given a new PushDefinition object" do
5
+ setup do
6
+ init()
7
+ # Create the historic (API response is for compiling the definition)
8
+ @push = @user.createPushDefinition()
9
+ end
10
+
11
+ should "be of the right type" do
12
+ assert_not_nil @push
13
+ assert @push.kind_of?(DataSift::PushDefinition)
14
+ end
15
+
16
+ should "have an empty initial status" do
17
+ assert_equal '', @push.initial_status
18
+ end
19
+
20
+ should "allow the initial status to be changed" do
21
+ @push.initial_status = DataSift::PushSubscription::STATUS_PAUSED
22
+ assert_equal DataSift::PushSubscription::STATUS_PAUSED, @push.initial_status
23
+ @push.initial_status = DataSift::PushSubscription::STATUS_STOPPED
24
+ assert_equal DataSift::PushSubscription::STATUS_STOPPED, @push.initial_status
25
+ end
26
+
27
+ should "have an empty output type" do
28
+ assert_equal '', @push.output_type
29
+ end
30
+
31
+ should "allow the output type to be set" do
32
+ @push.output_type = @testdata['push_output_type']
33
+ assert_equal @testdata['push_output_type'], @push.output_type
34
+ end
35
+
36
+ should "return nil for non-existant output parameters" do
37
+ assert_nil @push.output_params['url']
38
+ end
39
+
40
+ should "allow output parameters to be set" do
41
+ @push.output_params['url'] = @testdata['push_output_param_url']
42
+ assert_equal @testdata['push_output_param_url'], @push.output_params['url']
43
+ assert_equal 1, @push.output_params.size()
44
+ end
45
+
46
+ should "support validation of the output parameters" do
47
+ configurePushDefinition(@push)
48
+ set204Response()
49
+ @push.validate()
50
+ end
51
+
52
+ should "support subscribing to a Definition object" do
53
+ definition = @user.createDefinition(@testdata['definition'])
54
+ # Set the response to the compile method so we can get the hash
55
+ begin
56
+ @user.api_client.setResponse(200, {
57
+ 'hash' => @testdata['definition_hash'],
58
+ 'created_at' => Time.now.strftime('%Y-%m-%d %H:%M:%S'),
59
+ 'dpu' => 10,
60
+ }, 200, 150)
61
+ assert_equal @testdata['definition_hash'], definition.hash
62
+ rescue InvalidDataError
63
+ assert false, "InvalidDataError"
64
+ rescue CompileFailedError
65
+ assert false, "CompileFailedError"
66
+ rescue APIError
67
+ assert false, "APIError"
68
+ end
69
+
70
+ configurePushDefinition(@push)
71
+
72
+ setResponseToASingleSubscription()
73
+
74
+ subscription = @push.subscribeDefinition(definition, @testdata['push_name'])
75
+
76
+ checkSubscription(subscription)
77
+ end
78
+
79
+ should "support subscribing to a Historic object" do
80
+ setResponseToASingleHistoric()
81
+ historic = @user.getHistoric(@testdata['historic_playback_id'])
82
+
83
+ configurePushDefinition(@push)
84
+
85
+ setResponseToASingleSubscription()
86
+
87
+ subscription = @push.subscribeHistoric(historic, @testdata['push_name'])
88
+
89
+ checkSubscription(subscription)
90
+ end
91
+ end
92
+ end