datasift 2.1.1 → 3.0.0.beta

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +100 -0
  4. data/Gemfile.lock +32 -0
  5. data/README.md +38 -79
  6. data/VERSION +1 -1
  7. data/datasift.gemspec +21 -24
  8. data/examples/auth.rb +44 -0
  9. data/examples/core_api_eg.rb +46 -0
  10. data/examples/historics_eg.rb +50 -0
  11. data/examples/historics_preview_eg.rb +30 -0
  12. data/examples/live_stream_eg.rb +89 -0
  13. data/examples/managed_source_eg.rb +56 -0
  14. data/examples/pull.rb +44 -0
  15. data/examples/push_eg.rb +56 -0
  16. data/lib/api/api_resource.rb +23 -0
  17. data/lib/datasift.rb +287 -14
  18. data/lib/errors.rb +59 -0
  19. data/lib/historics.rb +76 -0
  20. data/lib/historics_preview.rb +20 -0
  21. data/lib/live_stream.rb +53 -0
  22. data/lib/managed_source.rb +57 -0
  23. data/lib/push.rb +156 -0
  24. data/tests/core_api_test.rb +42 -0
  25. metadata +51 -73
  26. data/Rakefile +0 -34
  27. data/config.yml +0 -2
  28. data/examples/consume-stream.rb +0 -63
  29. data/examples/deletes.rb +0 -52
  30. data/examples/dpu.rb +0 -115
  31. data/examples/football-buffered.rb +0 -51
  32. data/examples/football.rb +0 -53
  33. data/examples/historics.sh +0 -2
  34. data/examples/historics/create-from-csdl.rb +0 -71
  35. data/examples/historics/create-from-hash.rb +0 -65
  36. data/examples/historics/delete.rb +0 -30
  37. data/examples/historics/env.rb +0 -37
  38. data/examples/historics/list.rb +0 -30
  39. data/examples/historics/start.rb +0 -30
  40. data/examples/historics/stop.rb +0 -30
  41. data/examples/historics/view.rb +0 -28
  42. data/examples/push.sh +0 -2
  43. data/examples/push/delete.rb +0 -33
  44. data/examples/push/env.rb +0 -53
  45. data/examples/push/list.rb +0 -30
  46. data/examples/push/pause.rb +0 -33
  47. data/examples/push/push-from-hash.rb +0 -72
  48. data/examples/push/push-historic-from-csdl.rb +0 -98
  49. data/examples/push/push-stream-from-csdl.rb +0 -70
  50. data/examples/push/resume.rb +0 -33
  51. data/examples/push/stop.rb +0 -33
  52. data/examples/push/view-log.rb +0 -45
  53. data/examples/push/view.rb +0 -31
  54. data/examples/twitter-track.rb +0 -61
  55. data/lib/DataSift/apiclient.rb +0 -73
  56. data/lib/DataSift/definition.rb +0 -202
  57. data/lib/DataSift/exceptions.rb +0 -33
  58. data/lib/DataSift/historic.rb +0 -316
  59. data/lib/DataSift/managed_source.rb +0 -263
  60. data/lib/DataSift/mockapiclient.rb +0 -44
  61. data/lib/DataSift/push_definition.rb +0 -115
  62. data/lib/DataSift/push_subscription.rb +0 -330
  63. data/lib/DataSift/stream_consumer.rb +0 -166
  64. data/lib/DataSift/stream_consumer_http.rb +0 -188
  65. data/lib/DataSift/user.rb +0 -311
  66. data/test/helper.rb +0 -95
  67. data/test/test_definition.rb +0 -273
  68. data/test/test_historics.rb +0 -233
  69. data/test/test_pushdefinition.rb +0 -92
  70. data/test/test_pushsubscription.rb +0 -17
  71. data/test/test_user.rb +0 -130
  72. data/test/testdata.yml +0 -30
@@ -0,0 +1,59 @@
1
+ class DataSiftError < StandardError
2
+ attr_reader :status
3
+ attr_reader :body
4
+
5
+ def initialize(http_status = nil, http_body = nil)
6
+ @status = http_status
7
+ @body = http_body
8
+ end
9
+
10
+ def message
11
+ @body == nil ? @status : @body
12
+ end
13
+
14
+ def to_s
15
+ #if both body and status were provided then message is the body otherwise the status contains the message
16
+ msg = !@body.nil? && !@status.nil? ? @body : @status
17
+ #if body is nil then status is the message body so no status is included
18
+ status_string = @body.nil? ? '' : "(Status #{@status}) "
19
+ "#{status_string} : #{msg}"
20
+ end
21
+ end
22
+
23
+ class NotSupportedError < DataSiftError
24
+ end
25
+
26
+ class BadRequestError < DataSiftError
27
+ end
28
+
29
+ class AuthError < DataSiftError
30
+ end
31
+
32
+ class ConnectionError < DataSiftError
33
+ end
34
+
35
+ class ApiResourceNotFoundError < DataSiftError
36
+ end
37
+
38
+ class InvalidConfigError < DataSiftError
39
+ end
40
+
41
+ class InvalidParamError < DataSiftError
42
+ end
43
+
44
+ class NotConnectedError < DataSiftError
45
+ end
46
+
47
+ class ReconnectTimeoutError < DataSiftError
48
+ end
49
+
50
+ class NotConfiguredError < DataSiftError
51
+ end
52
+
53
+ class InvalidTypeError < DataSiftError
54
+ end
55
+
56
+ class StreamingMessageError < DataSiftError
57
+ end
58
+ class WebSocketOnWindowsError < DataSiftError
59
+ end
@@ -0,0 +1,76 @@
1
+ module DataSift
2
+ class Historics < DataSift::ApiResource
3
+
4
+ ##
5
+ # Create a new historics query and return its id.
6
+ def prepare (hash, start, end_time, name, sources = 'twitter', sample = 100)
7
+ params = {
8
+ :hash => hash,
9
+ :start => start,
10
+ :end => end_time,
11
+ :name => name,
12
+ :sources => sources,
13
+ :sample => sample
14
+ }
15
+ requires params
16
+ DataSift.request(:POST, 'historics/prepare', @config, params)
17
+ end
18
+
19
+ ##
20
+ # Starts historics query.
21
+ def start(id)
22
+ params = {:id => id}
23
+ requires params
24
+ DataSift.request(:POST, 'historics/start', @config, params)
25
+ end
26
+
27
+ ##
28
+ # Stop historics query.
29
+ def stop(id)
30
+ params = {:id => id}
31
+ requires params
32
+ DataSift.request(:POST, 'historics/stop', @config, params)
33
+ end
34
+
35
+ ##
36
+ # Check the data coverage in the archive for a specified interval.
37
+ def status(start, end_time, sources='twitter')
38
+ params = {:start => start, :end => end_time, :sources => sources}
39
+ requires params
40
+ DataSift.request(:GET, 'historics/status', @config, params)
41
+ end
42
+
43
+ ##
44
+ # Update a historics query's name.
45
+ def update(id, name)
46
+ params = {:id => id, :name => name}
47
+ requires params
48
+ DataSift.request(:POST, 'historics/update', @config, params)
49
+ end
50
+
51
+ ##
52
+ # Delete a historics query.
53
+ def delete(id)
54
+ params = {:id => id}
55
+ requires params
56
+ DataSift.request(:DELETE, 'historics/delete', @config, params)
57
+ end
58
+
59
+ ##
60
+ # Get details for a given historics query.
61
+ def get_by_id(id, with_estimate = 1)
62
+ params = {:id => id, :with_estimate => with_estimate}
63
+ requires params
64
+ DataSift.request(:GET, 'historics/get', @config, params)
65
+ end
66
+
67
+ ##
68
+ # Get details for a set of historics within the given page constraints.
69
+ def get(max=20, page=1, with_estimate = 1)
70
+ params = {:max => max, :page => page, :with_estimate => with_estimate}
71
+ requires params
72
+ DataSift.request(:GET, 'historics/get', @config, params)
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,20 @@
1
+ module DataSift
2
+ class HistoricsPreview < DataSift::ApiResource
3
+
4
+ def create(hash, parameters, start, end_time = nil)
5
+ params = {
6
+ :hash => hash,
7
+ :parameters => parameters,
8
+ :start => start
9
+ }
10
+ params.merge!(end_time) if end_time != nil
11
+
12
+ DataSift.request(:POST, 'preview/create', @config, params)
13
+ end
14
+
15
+ def get(id)
16
+ DataSift.request(:POST, 'preview/get', @config, {:id => id})
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ module DataSift
2
+ class LiveStream < DataSift::ApiResource
3
+
4
+ @stream = nil
5
+ @on_datasift_message = lambda {}
6
+
7
+ def initialize (config, stream)
8
+ @config = config
9
+ @stream = stream
10
+ @retry_timeout = 0
11
+ @subscriptions = {}
12
+ @connected = false
13
+ end
14
+
15
+ attr_reader :connected, :stream, :retry_timeout, :subscriptions
16
+ attr_writer :connected, :retry_timeout, :on_datasift_message
17
+
18
+ def connected?
19
+ @connected
20
+ end
21
+
22
+ def fire_ds_message(message)
23
+ hash = false
24
+ if message.has_key?(:hash)
25
+ hash = message[:hash]
26
+ end
27
+ message.merge!({
28
+ :is_failure => message[:status] == 'failure',
29
+ :is_success => message[:status] == 'success',
30
+ :is_warning => message[:status] == 'warning',
31
+ :is_tick => message[:status] == 'connected'
32
+ })
33
+ @on_datasift_message.call(self, message, hash)
34
+ end
35
+
36
+ def fire_on_message(hash, interaction)
37
+ callback = @subscriptions[hash]
38
+ if callback == nil
39
+ raise StreamingMessageError.new "no valid on_message callback provided for stream #{hash} with message #{interaction}"
40
+ end
41
+ callback.call(interaction, self, hash)
42
+ end
43
+
44
+ def subscribe(hash, on_message)
45
+ @subscriptions[hash] = on_message
46
+ @stream.send "{ \"action\":\"subscribe\",\"hash\":\"#{hash}\"}"
47
+ end
48
+
49
+ def unsubscribe hash
50
+ @stream.send "{ \"action\":\"unsubscribe\",\"hash\":\"#{hash}\"}"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,57 @@
1
+ module DataSift
2
+ class ManagedSource < DataSift::ApiResource
3
+
4
+ ##
5
+ # Creates a new managed source
6
+ #+source_type+:: can be facebook_page, googleplus, instagram or yammer
7
+ def create(source_type, name, parameters = {}, resources = [], auth = [])
8
+ params = {
9
+ :source_type => source_type,
10
+ :name => name
11
+ }
12
+ params.merge!({:auth => MultiJson.dump(auth)}) if !auth.empty?
13
+ params.merge!({:parameters => MultiJson.dump(parameters)}) if !parameters.empty?
14
+ params.merge!({:resources => MultiJson.dump(resources)}) if resources.length > 0
15
+
16
+ DataSift.request(:POST, 'source/create', @config, params)
17
+ end
18
+
19
+ def update(id, source_type, name, parameters = {}, resources = [], auth = [])
20
+ params = {
21
+ :id => id,
22
+ :source_type => source_type,
23
+ :name => name
24
+ }
25
+ params.merge!({:auth => MultiJson.dump(auth)}) if !auth.empty?
26
+ params.merge!({:parameters => MultiJson.dump(parameters)}) if !parameters.empty?
27
+ params.merge!({:resources => MultiJson.dump(resources)}) if resources.length > 0
28
+
29
+ DataSift.request(:POST, 'source/update', @config, params)
30
+ end
31
+
32
+ def delete(id)
33
+ DataSift.request(:DELETE, 'source/delete', @config, {:id => id})
34
+ end
35
+
36
+ def stop(id)
37
+ DataSift.request(:POST, 'source/stop', @config, {:id => id})
38
+ end
39
+
40
+ def start(id)
41
+ DataSift.request(:POST, 'source/start', @config, {:id => id})
42
+ end
43
+
44
+ def get(id = nil, source_type = nil, page = 1, per_page = 20)
45
+ params = {:page => page, :per_page => per_page}
46
+ params.merge!({:id => id}) if id != nil
47
+ params.merge!({:source_type => source_type}) if source_type != nil
48
+
49
+ DataSift.request(:GET, 'source/get', @config, params)
50
+ end
51
+
52
+ def log(id, page = 1, per_page = 20)
53
+ DataSift.request(:POST, 'source/get', @config, {:id => id, :page => page, :per_page => per_page})
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,156 @@
1
+ module DataSift
2
+ ##
3
+ # Push is a simple and robust mechanism for periodically delivering your data directly to a given destination
4
+ # Several widely adopted storage locations are available including Amazon S3, FTP, HTTP, SFTP, DynamoDB, CouchDB
5
+ # MongoDB, Splunk, Elastic search and more! See http://dev.datasift.com/docs/push/connectors
6
+ class Push < DataSift::ApiResource
7
+
8
+ ##
9
+ # Check that a subscription is defined correctly
10
+ def valid?(params)
11
+ requires params
12
+ res = DataSift.request(:POST, 'push/validate', @config, params)
13
+ res[:http][:status] == 200
14
+ end
15
+
16
+ ##
17
+ #Create a new subscription to a live stream or historics query
18
+ # Possible params are
19
+ # hash, historics_id, name, output_type, initial_status, start, end and output_params.*
20
+ # where output_params.* depends on the output_type for specific options see the documentation at
21
+ # http://dev.datasift.com/docs/rest-api/pushcreate
22
+ # For a list of available connectors see http://dev.datasift.com/docs/push/connectors
23
+ def create(params)
24
+ DataSift.request(:POST, 'push/create', @config, params)
25
+ end
26
+
27
+
28
+ ##
29
+ # Update the name or output parameters for an existing subscription
30
+ def update (params)
31
+ DataSift.request(:POST, 'push/update', @config, params)
32
+ end
33
+
34
+ ##
35
+ #Pause a subscription and buffer the data for up to an hour
36
+ def pause(id)
37
+ params = {:id => id}
38
+ requires params
39
+ DataSift.request(:POST, 'push/pause', @config, params)
40
+ end
41
+
42
+ ##
43
+ #Restart a job that was previously paused
44
+ def resume(id)
45
+ params = {:id => id}
46
+ requires params
47
+ DataSift.request(:POST, 'push/resume', @config, params)
48
+ end
49
+
50
+ ##
51
+ # Stop a historics query or a live stream that is running with push
52
+ def stop(id)
53
+ params = {:id => id}
54
+ requires params
55
+ DataSift.request(:POST, 'push/stop', @config, params)
56
+ end
57
+
58
+ ##
59
+ # Deletes an existing push subscription
60
+ def delete(id)
61
+ params = {:id => id}
62
+ requires params
63
+ DataSift.request(:DELETE, 'push/delete', @config, params)
64
+ end
65
+
66
+ ##
67
+ # Retrieve log messages for a specific subscription
68
+ def logs_for (id, page = 1, per_page = 20, order_by = :request_time, order_dir = :desc)
69
+ params = {
70
+ :id => id,
71
+ :page => page,
72
+ :per_page => per_page,
73
+ :order_by => order_by,
74
+ :order_dir => order_dir
75
+ }
76
+ DataSift.request(:GET, 'push/log', @config, params)
77
+ end
78
+
79
+ ##
80
+ # Retrieve log messages for all subscriptions
81
+ def logs (page = 1, per_page = 20, order_by = :request_time, order_dir = :desc)
82
+ params = {
83
+ :page => page,
84
+ :per_page => per_page,
85
+ :order_by => order_by,
86
+ :order_dir => order_dir
87
+ }
88
+ DataSift.request(:GET, 'push/log', @config, params)
89
+ end
90
+
91
+ ##
92
+ # Get details of the subscription with the given ID
93
+ def get_by_subscription(id, page = 1, per_page = 20, order_by = :request_time, order_dir = :desc)
94
+ params = {
95
+ :id => id,
96
+ :page => page,
97
+ :per_page => per_page,
98
+ :order_by => order_by,
99
+ :order_dir => order_dir
100
+ }
101
+ DataSift.request(:GET, 'push/get', @config, params)
102
+ end
103
+
104
+ ##
105
+ # Get details of the subscription with the given stream ID/hash
106
+ def get_by_hash(hash, page = 1, per_page = 20, order_by = :request_time, order_dir = :desc)
107
+ params = {
108
+ :hash => hash,
109
+ :page => page,
110
+ :per_page => per_page,
111
+ :order_by => order_by,
112
+ :order_dir => order_dir
113
+ }
114
+ DataSift.request(:GET, 'push/get', @config, params)
115
+ end
116
+
117
+ ##
118
+ # Get details of the subscription with the given Historics ID
119
+ def get_by_historics_id(id, page = 1, per_page = 20, order_by = :request_time, order_dir = :desc)
120
+ params = {
121
+ :historics_id => id,
122
+ :page => page,
123
+ :per_page => per_page,
124
+ :order_by => order_by,
125
+ :order_dir => order_dir
126
+ }
127
+ DataSift.request(:GET, 'push/get', @config, params)
128
+ end
129
+
130
+ ##
131
+ # Get details of all subscriptions within the given page constraints
132
+ def get(page = 1, per_page = 20, order_by = :request_time, order_dir = :desc)
133
+ params = {
134
+ :page => page,
135
+ :per_page => per_page,
136
+ :order_by => order_by,
137
+ :order_dir => order_dir
138
+ }
139
+ DataSift.request(:GET, 'push/get', @config, params)
140
+ end
141
+
142
+ ##
143
+ # Pull data from a 'pull' type Push Subscription
144
+ def pull(id, size = 20971520, cursor = '', callback = nil)
145
+ params = {
146
+ :id => id,
147
+ :size => size,
148
+ :cursor => cursor
149
+ }
150
+ if callback
151
+ params.merge!({:on_interaction => callback})
152
+ end
153
+ DataSift.request(:GET, 'pull', @config, params, {}, 30, 30, true)
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,42 @@
1
+ require '../examples/auth'
2
+ require 'test/unit'
3
+ class CoreApiTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ auth = DataSiftExample.new
7
+ @datasift = auth.datasift
8
+ @csdl = 'interaction.content contains "test"'
9
+ end
10
+
11
+
12
+ def teardown
13
+ # Do nothing
14
+ end
15
+
16
+ def test_csdl_validation
17
+ assert_true @datasift.valid?(@csdl)
18
+ assert_raise_kind_of BadRequestError do
19
+ datasift.valid?(@csdl+' random string')
20
+ end
21
+ end
22
+
23
+ def test_compile
24
+ stream = @datasift.compile(@csdl)
25
+ hash_asserts(stream)
26
+ end
27
+
28
+ def test_dpus
29
+ stream = @datasift.compile(@csdl)
30
+ hash_asserts(stream)
31
+
32
+ dpus = @datasift.dpu stream[:data][:hash]
33
+ assert_not_nil dpus[:data][:dpu]
34
+ assert_not_nil dpus[:data][:detail]
35
+ end
36
+
37
+ def hash_asserts(stream)
38
+ assert_not_nil stream[:data]
39
+ assert_not_nil stream[:data][:hash]
40
+ end
41
+
42
+ end