datasift 2.1.1 → 3.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +100 -0
- data/Gemfile.lock +32 -0
- data/README.md +38 -79
- data/VERSION +1 -1
- data/datasift.gemspec +21 -24
- data/examples/auth.rb +44 -0
- data/examples/core_api_eg.rb +46 -0
- data/examples/historics_eg.rb +50 -0
- data/examples/historics_preview_eg.rb +30 -0
- data/examples/live_stream_eg.rb +89 -0
- data/examples/managed_source_eg.rb +56 -0
- data/examples/pull.rb +44 -0
- data/examples/push_eg.rb +56 -0
- data/lib/api/api_resource.rb +23 -0
- data/lib/datasift.rb +287 -14
- data/lib/errors.rb +59 -0
- data/lib/historics.rb +76 -0
- data/lib/historics_preview.rb +20 -0
- data/lib/live_stream.rb +53 -0
- data/lib/managed_source.rb +57 -0
- data/lib/push.rb +156 -0
- data/tests/core_api_test.rb +42 -0
- metadata +51 -73
- data/Rakefile +0 -34
- data/config.yml +0 -2
- data/examples/consume-stream.rb +0 -63
- data/examples/deletes.rb +0 -52
- data/examples/dpu.rb +0 -115
- data/examples/football-buffered.rb +0 -51
- data/examples/football.rb +0 -53
- data/examples/historics.sh +0 -2
- data/examples/historics/create-from-csdl.rb +0 -71
- data/examples/historics/create-from-hash.rb +0 -65
- data/examples/historics/delete.rb +0 -30
- data/examples/historics/env.rb +0 -37
- data/examples/historics/list.rb +0 -30
- data/examples/historics/start.rb +0 -30
- data/examples/historics/stop.rb +0 -30
- data/examples/historics/view.rb +0 -28
- data/examples/push.sh +0 -2
- data/examples/push/delete.rb +0 -33
- data/examples/push/env.rb +0 -53
- data/examples/push/list.rb +0 -30
- data/examples/push/pause.rb +0 -33
- data/examples/push/push-from-hash.rb +0 -72
- data/examples/push/push-historic-from-csdl.rb +0 -98
- data/examples/push/push-stream-from-csdl.rb +0 -70
- data/examples/push/resume.rb +0 -33
- data/examples/push/stop.rb +0 -33
- data/examples/push/view-log.rb +0 -45
- data/examples/push/view.rb +0 -31
- data/examples/twitter-track.rb +0 -61
- data/lib/DataSift/apiclient.rb +0 -73
- data/lib/DataSift/definition.rb +0 -202
- data/lib/DataSift/exceptions.rb +0 -33
- data/lib/DataSift/historic.rb +0 -316
- data/lib/DataSift/managed_source.rb +0 -263
- data/lib/DataSift/mockapiclient.rb +0 -44
- data/lib/DataSift/push_definition.rb +0 -115
- data/lib/DataSift/push_subscription.rb +0 -330
- data/lib/DataSift/stream_consumer.rb +0 -166
- data/lib/DataSift/stream_consumer_http.rb +0 -188
- data/lib/DataSift/user.rb +0 -311
- data/test/helper.rb +0 -95
- data/test/test_definition.rb +0 -273
- data/test/test_historics.rb +0 -233
- data/test/test_pushdefinition.rb +0 -92
- data/test/test_pushsubscription.rb +0 -17
- data/test/test_user.rb +0 -130
- data/test/testdata.yml +0 -30
data/examples/push/view.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# This script views the details of Push subscriptions in your account.
|
2
|
-
#
|
3
|
-
# NB: Most of the error handling (exception catching) has been removed for
|
4
|
-
# the sake of simplicity. Nearly everything in this library may throw
|
5
|
-
# exceptions, and production code should catch them. See the documentation
|
6
|
-
# for full details.
|
7
|
-
#
|
8
|
-
|
9
|
-
# Include the shared Env class
|
10
|
-
require File.dirname(__FILE__) + '/env'
|
11
|
-
|
12
|
-
# Create the env object. This reads the command line arguments, creates the
|
13
|
-
# user object, and provides access to both along with helper functions.
|
14
|
-
env = Env.new()
|
15
|
-
|
16
|
-
# Make sure we have something to do
|
17
|
-
abort('Please specify one or more subscription IDs') unless env.args.size() > 0
|
18
|
-
|
19
|
-
for sub_id in env.args
|
20
|
-
begin
|
21
|
-
sub = env.user.getPushSubscription(sub_id)
|
22
|
-
env.displaySubscriptionDetails(sub)
|
23
|
-
rescue DataSift::DataSiftError => err
|
24
|
-
puts 'ERR: [' + err.class.name + '] ' + err.message
|
25
|
-
puts '--'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
if env.user.rate_limit_remaining != -1
|
30
|
-
puts 'Rate limit remainining: ' + String(env.user.rate_limit_remaining)
|
31
|
-
end
|
data/examples/twitter-track.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
# This example constructs a DataSift_Definition object with CSDL that looks
|
2
|
-
# for anything containing the word "football". It then gets an HTTP
|
3
|
-
# consumer for that definition and displays matching interactions to the
|
4
|
-
# screen as they come in. It will display 10 interactions and then stop.
|
5
|
-
#
|
6
|
-
# NB: Most of the error handling (exception catching) has been removed for
|
7
|
-
# the sake of simplicity. Nearly everything in this library may throw
|
8
|
-
# exceptions, and production code should catch them. See the documentation
|
9
|
-
# for full details.
|
10
|
-
#
|
11
|
-
|
12
|
-
# Make sure we have some arguments
|
13
|
-
if ARGV.size == 0
|
14
|
-
puts 'ERR: Please specify the words and/or phrases to track!'
|
15
|
-
puts
|
16
|
-
puts
|
17
|
-
exit!
|
18
|
-
end
|
19
|
-
|
20
|
-
# Include the DataSift library
|
21
|
-
require './' + File.dirname(__FILE__) + '/../lib/datasift'
|
22
|
-
|
23
|
-
# Include the configuration - put your username and API key in this file
|
24
|
-
require 'yaml'
|
25
|
-
config = YAML::load(File.open(File.join(File.dirname(__FILE__), '..', 'config.yml')))
|
26
|
-
|
27
|
-
# Authenticate
|
28
|
-
puts 'Creating user...'
|
29
|
-
user = DataSift::User.new(config['username'], config['api_key'])
|
30
|
-
|
31
|
-
# Create the definition
|
32
|
-
csdl = 'interaction.type == "twitter" and (interaction.content contains "' + ARGV.join('" or interaction.content contains "') + '")'
|
33
|
-
puts 'Creating definition...'
|
34
|
-
puts ' ' + csdl
|
35
|
-
definition = user.createDefinition(csdl)
|
36
|
-
|
37
|
-
# Create the consumer
|
38
|
-
puts 'Getting the consumer...'
|
39
|
-
consumer = definition.getConsumer(DataSift::StreamConsumer::TYPE_HTTP)
|
40
|
-
|
41
|
-
# Setting up the onStopped handler
|
42
|
-
consumer.onStopped do |reason|
|
43
|
-
puts
|
44
|
-
puts 'Stopped: ' + reason
|
45
|
-
puts
|
46
|
-
end
|
47
|
-
|
48
|
-
# And start consuming
|
49
|
-
puts 'Consuming...'
|
50
|
-
puts '--'
|
51
|
-
consumer.consume(true) do |interaction|
|
52
|
-
if interaction
|
53
|
-
puts 'Type: ' + interaction['interaction']['type']
|
54
|
-
puts 'Content: ' + interaction['interaction']['content']
|
55
|
-
puts '--'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
puts
|
60
|
-
puts 'Finished consuming'
|
61
|
-
puts
|
data/lib/DataSift/apiclient.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'rest_client'
|
2
|
-
require 'yajl'
|
3
|
-
|
4
|
-
module DataSift
|
5
|
-
#The ApiClient class wraps the functionality that makes calls to the
|
6
|
-
#DataSift API.
|
7
|
-
class ApiClient
|
8
|
-
#Make a call to a DataSift API endpoint.
|
9
|
-
#=== Parameters
|
10
|
-
#* +user+ - The DataSift User object.
|
11
|
-
#* +endpoint+ - The endpoint of the API call.
|
12
|
-
#* +params+ - The parameters to be passed along with the request.
|
13
|
-
#=== Returns
|
14
|
-
#A Hash contatining...
|
15
|
-
#* +response_code+ - The HTTP response code.
|
16
|
-
#* +data+ - A Hash containing the response data.
|
17
|
-
#* +rate_limit+ - The total API credits you get per hour.
|
18
|
-
#* +rate_limit_remaining+ - The number of API credits you have remaining for this hour.
|
19
|
-
def call(user, endpoint, params = {}, user_agent = User::USER_AGENT)
|
20
|
-
# Build the full endpoint URL
|
21
|
-
url = 'http' + (user.use_ssl ? 's' : '') + '://' + User::API_BASE_URL + endpoint
|
22
|
-
|
23
|
-
retval = {
|
24
|
-
'response_code' => 500,
|
25
|
-
'data' => { 'error' => 'Unknown error' },
|
26
|
-
'rate_limit' => -1,
|
27
|
-
'rate_limit_remaining' => -1,
|
28
|
-
}
|
29
|
-
|
30
|
-
begin
|
31
|
-
# Make the call
|
32
|
-
res = RestClient.post(url, params, { 'Auth' => user.username + ':' + user.api_key, 'User-Agent' => user_agent })
|
33
|
-
|
34
|
-
# Success
|
35
|
-
retval['response_code'] = res.code
|
36
|
-
|
37
|
-
# Parse the JSON response
|
38
|
-
retval['data'] = Yajl::Parser.parse(res)
|
39
|
-
|
40
|
-
# Rate limit headers
|
41
|
-
if (res.headers[:x_ratelimit_limit])
|
42
|
-
retval['rate_limit'] = res.headers[:x_ratelimit_limit]
|
43
|
-
end
|
44
|
-
|
45
|
-
if (res.headers[:x_ratelimit_remaining])
|
46
|
-
retval['rate_limit_remaining'] = res.headers[:x_ratelimit_remaining]
|
47
|
-
end
|
48
|
-
rescue RestClient::ExceptionWithResponse => err
|
49
|
-
# Set the response code
|
50
|
-
retval['response_code'] = err.http_code
|
51
|
-
|
52
|
-
# And set the data
|
53
|
-
retval['data'] = Yajl::Parser.parse(err.response)
|
54
|
-
end
|
55
|
-
|
56
|
-
return retval
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
#Convert a Hash to an HTTP query string.
|
62
|
-
#=== Parameters
|
63
|
-
#* +hash+ - The Hash to convert.
|
64
|
-
#=== Returns
|
65
|
-
#A string containing the equivalent query string.
|
66
|
-
def hashToQuerystring(hash)
|
67
|
-
hash.keys.inject('') do |query_string, key|
|
68
|
-
query_string << '&' unless key == hash.keys.first
|
69
|
-
query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key].to_s)}"
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
data/lib/DataSift/definition.rb
DELETED
@@ -1,202 +0,0 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
|
-
module DataSift
|
4
|
-
#The Definition class represents a stream definition.
|
5
|
-
class Definition
|
6
|
-
#The CSDL for this Definition.
|
7
|
-
attr_reader :csdl
|
8
|
-
|
9
|
-
#Constructor. A User object is required, and you can optionally supply a
|
10
|
-
#default CSDL string.
|
11
|
-
#=== Parameters
|
12
|
-
#* +user+ - The DataSift::User object.
|
13
|
-
#* +csdl+ - Optional default CSDL string.
|
14
|
-
#* +hash+ - Optional default hash string.
|
15
|
-
def initialize(user, csdl = '', hash = false)
|
16
|
-
raise InvalidDataError, 'Please supply a valid User object when creating a Definition object.' unless user.is_a? DataSift::User
|
17
|
-
@user = user
|
18
|
-
clearHash()
|
19
|
-
@hash = hash
|
20
|
-
self.csdl = csdl
|
21
|
-
@total_dpu = false
|
22
|
-
@created_at = false
|
23
|
-
end
|
24
|
-
|
25
|
-
#CSDL getter
|
26
|
-
def csdl
|
27
|
-
raise InvalidDataError, 'The CSDL is not available' unless !@csdl.nil?
|
28
|
-
return @csdl
|
29
|
-
end
|
30
|
-
|
31
|
-
#CSDL setter. Strips the incoming string and resets the hash if it's changed.
|
32
|
-
def csdl=(csdl)
|
33
|
-
if csdl.nil?
|
34
|
-
@csdl = nil
|
35
|
-
else
|
36
|
-
raise InvalidDataError, 'The CSDL must be a string.' unless csdl.is_a? String
|
37
|
-
csdl = csdl.strip
|
38
|
-
clearHash() unless csdl == @csdl
|
39
|
-
@csdl = csdl
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
#Total DPU getter.
|
44
|
-
def total_dpu
|
45
|
-
compile() unless @total_dpu
|
46
|
-
return @total_dpu
|
47
|
-
end
|
48
|
-
|
49
|
-
#Created at getter.
|
50
|
-
def created_at
|
51
|
-
compile() unless @created_at
|
52
|
-
return @created_at
|
53
|
-
end
|
54
|
-
|
55
|
-
#Hash getter. If the hash has not yet been obtained the CSDL will be
|
56
|
-
#compiled first.
|
57
|
-
def hash
|
58
|
-
if @hash == false
|
59
|
-
compile()
|
60
|
-
end
|
61
|
-
|
62
|
-
return @hash
|
63
|
-
end
|
64
|
-
|
65
|
-
#Reset the hash to false. The effect of this is to mark the definition as
|
66
|
-
#requiring compilation.
|
67
|
-
def clearHash()
|
68
|
-
@csdl = '' unless !@csdl.nil?
|
69
|
-
@hash = false
|
70
|
-
@total_dpu = false
|
71
|
-
@created_at = false
|
72
|
-
end
|
73
|
-
|
74
|
-
#Call the DataSift API to compile this definition. On success it will
|
75
|
-
#store the returned hash.
|
76
|
-
def compile()
|
77
|
-
raise InvalidDataError, 'Cannot compile an empty definition.' unless @csdl.length > 0
|
78
|
-
|
79
|
-
begin
|
80
|
-
res = @user.callAPI('compile', { 'csdl' => @csdl })
|
81
|
-
|
82
|
-
if res.has_key?('hash')
|
83
|
-
@hash = res['hash']
|
84
|
-
else
|
85
|
-
raise CompileFailedError, 'Compiled successfully but no hash in the response'
|
86
|
-
end
|
87
|
-
|
88
|
-
if res.has_key?('dpu')
|
89
|
-
@total_dpu = Float(res['dpu'])
|
90
|
-
else
|
91
|
-
raise CompileFailedError, 'Compiled successfully but no DPU in the response'
|
92
|
-
end
|
93
|
-
|
94
|
-
if res.has_key?('created_at')
|
95
|
-
@created_at = Date.parse(res['created_at'])
|
96
|
-
else
|
97
|
-
raise CompileFailedError, 'Compiled successfully but no created_at in the response'
|
98
|
-
end
|
99
|
-
rescue APIError => err
|
100
|
-
clearHash()
|
101
|
-
|
102
|
-
case err.http_code
|
103
|
-
when 400
|
104
|
-
raise CompileFailedError, err
|
105
|
-
else
|
106
|
-
raise APIError('Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.inspect + ']', err.http_code)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
#Call the DataSift API to validate this definition. On success it will
|
112
|
-
#store the details in the response.
|
113
|
-
def validate()
|
114
|
-
raise InvalidDataError, 'Cannot validate an empty definition.' unless @csdl.length > 0
|
115
|
-
|
116
|
-
begin
|
117
|
-
res = @user.callAPI('validate', { 'csdl' => @csdl })
|
118
|
-
|
119
|
-
if res.has_key?('dpu')
|
120
|
-
@total_dpu = Float(res['dpu'])
|
121
|
-
else
|
122
|
-
raise CompileFailedError, 'Validated successfully but no DPU in the response'
|
123
|
-
end
|
124
|
-
|
125
|
-
if res.has_key?('created_at')
|
126
|
-
@created_at = Date.parse(res['created_at'])
|
127
|
-
else
|
128
|
-
raise CompileFailedError, 'Validated successfully but no created_at in the response'
|
129
|
-
end
|
130
|
-
rescue APIError => err
|
131
|
-
clearHash()
|
132
|
-
|
133
|
-
case err.http_code
|
134
|
-
when 400
|
135
|
-
raise CompileFailedError, err
|
136
|
-
else
|
137
|
-
raise APIError('Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.inspect + ']', err.http_code)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
#Call the DataSift API to get the DPU for this definition. Returns
|
143
|
-
#=== Returns
|
144
|
-
#A Hash containing...
|
145
|
-
#* +detail+ - The breakdown of running the rule
|
146
|
-
#* +dpu+ - The total DPU of the rule
|
147
|
-
def getDPUBreakdown()
|
148
|
-
raise InvalidDataError, "Cannot get the DPU for an empty definition." unless @csdl.length > 0
|
149
|
-
|
150
|
-
@user.callAPI('dpu', { 'hash' => self.hash })
|
151
|
-
end
|
152
|
-
|
153
|
-
#Call the DataSift API to get buffered interactions.
|
154
|
-
#=== Parameters
|
155
|
-
#* +count+ - Optional number of interactions to return (max 200).
|
156
|
-
#* +from_id+ - Optional start ID.
|
157
|
-
#=== Returns
|
158
|
-
#An array of Hashes where each Hash is an interaction.
|
159
|
-
def getBuffered(count = false, from_id = false)
|
160
|
-
raise InvalidDataError, "Cannot get buffered interactions for an empty definition." unless @csdl.length > 0
|
161
|
-
|
162
|
-
params = { 'hash' => self.hash }
|
163
|
-
|
164
|
-
if count
|
165
|
-
params['count'] = count
|
166
|
-
end
|
167
|
-
|
168
|
-
if from_id
|
169
|
-
params['interaction_id'] = from_id
|
170
|
-
end
|
171
|
-
|
172
|
-
retval = @user.callAPI('stream', params)
|
173
|
-
|
174
|
-
raise APIError, 'No data in the response' unless retval.has_key?('stream')
|
175
|
-
|
176
|
-
return retval['stream']
|
177
|
-
end
|
178
|
-
|
179
|
-
#Create a Historics query based on this Definition.
|
180
|
-
#=== Parameters
|
181
|
-
#* +start_date+ - The start date for a new Historics query.
|
182
|
-
#* +end_date+ - The end date for a new Historics query.
|
183
|
-
#* +sources+ - An array of sources for a new Historics query.
|
184
|
-
#* +name+ - The name for a new Historics query.
|
185
|
-
#* +sample+ - The sample rate for the new Historics query.
|
186
|
-
#=== Returns
|
187
|
-
#A Historic object.
|
188
|
-
def createHistoric(start_date, end_date, sources, sample, name)
|
189
|
-
return Historic.new(@user, hash, start_date, end_date, sources, sample, name)
|
190
|
-
end
|
191
|
-
|
192
|
-
#Returns a StreamConsumer-derived object for this definition, for the
|
193
|
-
#given type.
|
194
|
-
#=== Parameters
|
195
|
-
#* +type+ - The consumer type for which to construct a consumer.
|
196
|
-
#=== Returns
|
197
|
-
#A StreamConsumer-derived object.
|
198
|
-
def getConsumer(type = nil, on_interaction = nil, on_stopped = nil)
|
199
|
-
StreamConsumer.factory(@user, type, self)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
data/lib/DataSift/exceptions.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module DataSift
|
2
|
-
# All exceptions inherit from DataSiftError.
|
3
|
-
class DataSiftError < StandardError; end
|
4
|
-
|
5
|
-
# Thrown when access to the API is denied.
|
6
|
-
class AccessDeniedError < DataSiftError; end
|
7
|
-
|
8
|
-
# Thrown when CSDL validation or compilation fails.
|
9
|
-
class CompileFailedError < DataSiftError; end
|
10
|
-
|
11
|
-
# Thrown whenever invalid data is encountered in the library.
|
12
|
-
class InvalidDataError < DataSiftError; end
|
13
|
-
|
14
|
-
# Thrown when you exceed your API rate limit.
|
15
|
-
class RateLimitExceededError < DataSiftError; end
|
16
|
-
|
17
|
-
# Thrown when error occur while reading streaming data.
|
18
|
-
class StreamError < DataSiftError; end
|
19
|
-
|
20
|
-
#Thrown when an error is found in API responses.
|
21
|
-
#These errors optionally carry the HTTP error code.
|
22
|
-
class APIError < DataSiftError
|
23
|
-
#The HTTP status code.
|
24
|
-
attr_reader :http_code
|
25
|
-
|
26
|
-
#Constructor.
|
27
|
-
#=== Parameters
|
28
|
-
#* +http_code+ - Optional HTTP status code.
|
29
|
-
def initialize(http_code = -1)
|
30
|
-
@http_code = http_code
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/lib/DataSift/historic.rb
DELETED
@@ -1,316 +0,0 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
|
-
module DataSift
|
4
|
-
#The Historic class represents a Historics query.
|
5
|
-
class Historic
|
6
|
-
#Get a list of Historics queries in your account.
|
7
|
-
#=== Parameters
|
8
|
-
#* +user+ - The user object making the request.
|
9
|
-
#* +page+ - The page number to get.
|
10
|
-
#* +per_page+ - The number of items per page.
|
11
|
-
#=== Returns
|
12
|
-
#A Hash containing...
|
13
|
-
#* +count+ - The total number of Historics queries in your account.
|
14
|
-
#* +historics+ - An array of Hashes where each Hash is a Historics query.
|
15
|
-
def self.list(user, page = 1, per_page = 20)
|
16
|
-
begin
|
17
|
-
res = user.callAPI(
|
18
|
-
'historics/get', {
|
19
|
-
'page' => page,
|
20
|
-
'max' => per_page
|
21
|
-
})
|
22
|
-
|
23
|
-
retval = { 'count' => res['count'], 'historics' => [] }
|
24
|
-
for historic in res['data']
|
25
|
-
retval['historics'].push(new(user, historic))
|
26
|
-
end
|
27
|
-
retval
|
28
|
-
rescue APIError => err
|
29
|
-
case err.http_code
|
30
|
-
when 400
|
31
|
-
# Missing or invalid parameters
|
32
|
-
raise InvalidDataError, err
|
33
|
-
else
|
34
|
-
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
#The ID of this Historics query.
|
40
|
-
attr_reader :playback_id
|
41
|
-
#The stream hash which this Historics query is executing.
|
42
|
-
attr_reader :stream_hash
|
43
|
-
#The friendly name for this Historics query.
|
44
|
-
attr_reader :name
|
45
|
-
#The start date for this Historics query.
|
46
|
-
attr_reader :start_date
|
47
|
-
#The end date for this Historics query.
|
48
|
-
attr_reader :end_date
|
49
|
-
#The date/time when this Historics query was created.
|
50
|
-
attr_reader :created_at
|
51
|
-
#The current status of this Historics query.
|
52
|
-
attr_reader :status
|
53
|
-
#The current progress in percent of this Historics query.
|
54
|
-
attr_reader :progress
|
55
|
-
#The data sources for which this Historics query is looking.
|
56
|
-
attr_reader :sources
|
57
|
-
#The sample percentage that this Historics query will match.
|
58
|
-
attr_reader :sample
|
59
|
-
#The DPU cost of running this Historics query.
|
60
|
-
attr_reader :dpus
|
61
|
-
#True if this Historics query has been deleted.
|
62
|
-
attr_reader :is_deleted
|
63
|
-
|
64
|
-
#Constructor. Pass all parameters to create a new Historics query, or provide a User object and a playback_id as the hash parameter to load an existing query from the API.
|
65
|
-
#=== Parameters
|
66
|
-
#* +user+ - The DataSift::User object.
|
67
|
-
#* +hash+ - Either a stream_hash, an array containing the Historics query data or a playback ID.
|
68
|
-
#* +start_date+ - The start date for a new Historics query.
|
69
|
-
#* +end_date+ - The end date for a new Historics query.
|
70
|
-
#* +sources+ - An array of sources for a new Historics query.
|
71
|
-
#* +name+ - The name for a new Historics query.
|
72
|
-
#* +sample+ - The sample rate for the new Historics query.
|
73
|
-
def initialize(user, hash, start_date = false, end_date = false, sources = false, sample = false, name = false)
|
74
|
-
raise InvalidDataError, 'Please supply a valid User object when creating a Historic object.' unless user.is_a? DataSift::User
|
75
|
-
@user = user
|
76
|
-
|
77
|
-
if not start_date
|
78
|
-
if hash.kind_of?(Hash)
|
79
|
-
# Initialising from an array
|
80
|
-
@playback_id = hash['id']
|
81
|
-
initFromArray(hash)
|
82
|
-
else
|
83
|
-
# Fetching from the API
|
84
|
-
@playback_id = hash
|
85
|
-
reloadData()
|
86
|
-
end
|
87
|
-
else
|
88
|
-
# Creating a new Historic query, make sure we have all the parameters
|
89
|
-
raise InvalidDataError,
|
90
|
-
'Please supply all parameters when creating a new Historics query' unless
|
91
|
-
start_date != false and end_date != false and sources != false and
|
92
|
-
name != false and sample != false
|
93
|
-
|
94
|
-
# Convert and validate the parameters as required
|
95
|
-
hash = hash.hash if hash.is_a? DataSift::Definition
|
96
|
-
start_date = DateTime.strptime(start_date, '%s') unless start_date.is_a? Date
|
97
|
-
end_date = DateTime.strptime(end_date, '%s') unless end_date.is_a? Date
|
98
|
-
raise InvalidDataError, 'Please supply an array of sources' unless sources.kind_of?(Array)
|
99
|
-
|
100
|
-
@playback_id = false
|
101
|
-
@stream_hash = hash
|
102
|
-
@start_date = start_date
|
103
|
-
@end_date = end_date
|
104
|
-
@sources = sources
|
105
|
-
@name = name
|
106
|
-
@sample = sample
|
107
|
-
@progress = 0
|
108
|
-
@dpus = false
|
109
|
-
@availability = {}
|
110
|
-
@is_deleted = false
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
#Reload the data for this object from the API.
|
115
|
-
def reloadData()
|
116
|
-
#Can't do this if we've been deleted
|
117
|
-
raise InvalidDataError, 'Cannot reload the data for a deleted Historics query' unless not @is_deleted
|
118
|
-
|
119
|
-
#Can't do this without a playback ID
|
120
|
-
raise InvalidDataError, 'Cannot reload the data with a Historics query with no playback ID' unless @playback_id
|
121
|
-
|
122
|
-
begin
|
123
|
-
initFromArray(@user.callAPI('historics/get', { 'id' => @playback_id }))
|
124
|
-
rescue APIError => err
|
125
|
-
case err.http_code
|
126
|
-
when 400
|
127
|
-
raise InvalidDataError, err
|
128
|
-
else
|
129
|
-
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
#Initialise this obejct from the data in a Hash.
|
135
|
-
#=== Parameters
|
136
|
-
#* +data+ - The Hash containing the data.
|
137
|
-
def initFromArray(data)
|
138
|
-
raise APIError, 'No playback ID in the response' unless data.has_key?('id')
|
139
|
-
raise APIError, 'Incorrect playback ID in the response' unless not @playback_id or data['id'] == @playback_id
|
140
|
-
@playback_id = data['id']
|
141
|
-
|
142
|
-
raise APIError, 'No definition hash in the response' unless data.has_key?('definition_id')
|
143
|
-
@stream_hash = data['definition_id']
|
144
|
-
|
145
|
-
raise APIError, 'No name in the response' unless data.has_key?('name')
|
146
|
-
@name = data['name']
|
147
|
-
|
148
|
-
raise APIError, 'No start timestamp in the response' unless data.has_key?('start')
|
149
|
-
@start_date = DateTime.strptime(String(data['start']), '%s')
|
150
|
-
|
151
|
-
raise APIError, 'No end timestamp in the response' unless data.has_key?('end')
|
152
|
-
@end_date = DateTime.strptime(String(data['end']), '%s')
|
153
|
-
|
154
|
-
raise APIError, 'No created at timstamp in the response' unless data.has_key?('created_at')
|
155
|
-
@created_at = DateTime.strptime(String(data['created_at']), '%s')
|
156
|
-
|
157
|
-
raise APIError, 'No status in the response' unless data.has_key?('status')
|
158
|
-
@status = data['status']
|
159
|
-
|
160
|
-
raise APIError, 'No progress in the response' unless data.has_key?('progress')
|
161
|
-
@progress = data['progress']
|
162
|
-
|
163
|
-
raise APIError, 'No sources in the response' unless data.has_key?('sources')
|
164
|
-
@sources = data['sources']
|
165
|
-
|
166
|
-
raise APIError, 'No sample in the response' unless data.has_key?('sample')
|
167
|
-
@sample = data['sample']
|
168
|
-
|
169
|
-
@is_deleted = (@status == 'deleted')
|
170
|
-
|
171
|
-
return true
|
172
|
-
end
|
173
|
-
|
174
|
-
#Getter for the playback ID. If the Historics query has not yet been
|
175
|
-
#prepared that will be done automagically to obtain the playback ID.
|
176
|
-
def hash
|
177
|
-
if @playback_id == false
|
178
|
-
prepare()
|
179
|
-
end
|
180
|
-
|
181
|
-
@playback_id
|
182
|
-
end
|
183
|
-
|
184
|
-
#Name setter. Updates via the API if this Historics query has already
|
185
|
-
#been prepared.
|
186
|
-
def name=(new_name)
|
187
|
-
raise InvalidDataError, 'Cannot set the name of a deleted Historics query' unless not @is_deleted
|
188
|
-
|
189
|
-
if not @playback_id
|
190
|
-
@name = new_name
|
191
|
-
else
|
192
|
-
@user.callAPI('historics/update', { 'id' => @playback_id, 'name' => new_name })
|
193
|
-
reloadData()
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
#Call the DataSift API to prepare this Historics query
|
198
|
-
def prepare()
|
199
|
-
raise InvalidDataError, 'Cannot prepare a deleted Historics query' unless not @is_deleted
|
200
|
-
raise InvalidDataError, 'This Historics query has already been prepared' unless not @playback_id
|
201
|
-
|
202
|
-
begin
|
203
|
-
res = @user.callAPI(
|
204
|
-
'historics/prepare', {
|
205
|
-
'hash' => @stream_hash,
|
206
|
-
'start' => Integer(@start_date.strftime('%s')),
|
207
|
-
'end' => Integer(@end_date.strftime('%s')),
|
208
|
-
'name' => @name,
|
209
|
-
'sources' => @sources.join(','),
|
210
|
-
'sample' => @sample
|
211
|
-
})
|
212
|
-
|
213
|
-
raise InvalidDataError, 'Prepared successfully but no playback ID in the response' unless res.has_key?('id')
|
214
|
-
@playback_id = res['id']
|
215
|
-
|
216
|
-
raise InvalidDataError, 'Prepared successfully but no DPU cost in the response' unless res.has_key?('dpus')
|
217
|
-
@dpus = res['dpus']
|
218
|
-
|
219
|
-
raise InvalidDataError, 'Prepared successfully but no availability in the response' unless res.has_key?('availability')
|
220
|
-
@availability = res['availability']
|
221
|
-
rescue APIError => err
|
222
|
-
case err.http_code
|
223
|
-
when 400
|
224
|
-
raise InvalidDataError, err
|
225
|
-
else
|
226
|
-
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
# Reload the data so we get the created_at date, initial status and the rest.
|
231
|
-
reloadData()
|
232
|
-
end
|
233
|
-
|
234
|
-
#Start this Historics query.
|
235
|
-
def start()
|
236
|
-
raise InvalidDataError, 'Cannot start a deleted Historics query' unless not @is_deleted
|
237
|
-
raise InvalidDataError, 'Cannot start a Historics query that hasn\'t been prepared' unless @playback_id
|
238
|
-
|
239
|
-
begin
|
240
|
-
res = @user.callAPI('historics/start', { 'id' => @playback_id })
|
241
|
-
rescue APIError => err
|
242
|
-
case err.http_code
|
243
|
-
when 400
|
244
|
-
# Missing or invalid parameters
|
245
|
-
raise InvalidDataError, err
|
246
|
-
when 404
|
247
|
-
# Historics query not found
|
248
|
-
raise InvalidDataError, err
|
249
|
-
else
|
250
|
-
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
#Stop this Historics query.
|
256
|
-
def stop()
|
257
|
-
raise InvalidDataError, 'Cannot stop a deleted Historics query' unless not @is_deleted
|
258
|
-
raise InvalidDataError, 'Cannot stop a Historics query that hasn\'t been prepared' unless @playback_id
|
259
|
-
|
260
|
-
begin
|
261
|
-
res = @user.callAPI('historics/stop', { 'id' => @playback_id })
|
262
|
-
rescue APIError => err
|
263
|
-
case err.http_code
|
264
|
-
when 400
|
265
|
-
# Missing or invalid parameters
|
266
|
-
raise InvalidDataError, err
|
267
|
-
when 404
|
268
|
-
# Historics query not found
|
269
|
-
raise InvalidDataError, err
|
270
|
-
else
|
271
|
-
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
#Delete this Historics query.
|
277
|
-
def delete()
|
278
|
-
raise InvalidDataError, 'Cannot delete a deleted Historics query' unless not @is_deleted
|
279
|
-
raise InvalidDataError, 'Cannot delete a Historics query that hasn\'t been prepared' unless @playback_id
|
280
|
-
|
281
|
-
begin
|
282
|
-
@user.callAPI('historics/delete', { 'id' => @playback_id })
|
283
|
-
@is_deleted = true
|
284
|
-
rescue APIError => err
|
285
|
-
case err.http_code
|
286
|
-
when 400
|
287
|
-
# Missing or invalid parameters
|
288
|
-
raise InvalidDataError, err
|
289
|
-
when 404
|
290
|
-
# Historics query not found
|
291
|
-
raise InvalidDataError, err
|
292
|
-
else
|
293
|
-
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
#Get a page of Push subscriptions for this Historics query, where each
|
299
|
-
#page contains up to per_page items. Results will be returned in the
|
300
|
-
#order requested.
|
301
|
-
#=== Parameters
|
302
|
-
#* +page+ - The page number to get.
|
303
|
-
#* +per_page+ - The number of items per page.
|
304
|
-
#* +order_by+ - The field by which to order the results.
|
305
|
-
#* +order_dir+ - Ascending or descending.
|
306
|
-
#* +include_finished+ - True to include subscriptions against finished Historics queries.
|
307
|
-
#=== Returns
|
308
|
-
#A Hash containing...
|
309
|
-
#* +count+ - The total number of Push subscriptions in your account.
|
310
|
-
#* +subscriptions+ - An array of Hashes where each Hash is a Push subscription.
|
311
|
-
def getPushSubscriptions(page = 1, per_page = 20, order_by = PushSubscription::ORDERBY_CREATED_AT, order_dir = PushSubscription::ORDERDIR_ASC)
|
312
|
-
return PushSubscription.list(@user, page, per_page, order_by, order_dir, true, 'playback_id', @playback_id)
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
end
|