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.
- data/.gitignore +1 -0
- data/LICENSE +1 -1
- data/README.md +6 -4
- data/VERSION +1 -1
- data/config.yml +2 -2
- data/datasift.gemspec +11 -11
- data/examples/historics.sh +2 -0
- data/examples/historics/create-from-csdl.rb +71 -0
- data/examples/historics/create-from-hash.rb +65 -0
- data/examples/historics/delete.rb +30 -0
- data/examples/historics/env.rb +37 -0
- data/examples/historics/list.rb +30 -0
- data/examples/historics/start.rb +30 -0
- data/examples/historics/stop.rb +30 -0
- data/examples/historics/view.rb +28 -0
- data/examples/push.sh +2 -0
- data/examples/push/delete.rb +33 -0
- data/examples/push/env.rb +53 -0
- data/examples/push/list.rb +30 -0
- data/examples/push/pause.rb +33 -0
- data/examples/push/push-from-hash.rb +72 -0
- data/examples/push/push-historic-from-csdl.rb +98 -0
- data/examples/push/push-stream-from-csdl.rb +70 -0
- data/examples/push/resume.rb +33 -0
- data/examples/push/stop.rb +33 -0
- data/examples/push/view-log.rb +45 -0
- data/examples/push/view.rb +31 -0
- data/lib/DataSift/apiclient.rb +20 -25
- data/lib/DataSift/definition.rb +97 -57
- data/lib/DataSift/exceptions.rb +25 -8
- data/lib/DataSift/historic.rb +321 -0
- data/lib/DataSift/mockapiclient.rb +23 -34
- data/lib/DataSift/push_definition.rb +115 -0
- data/lib/DataSift/push_subscription.rb +330 -0
- data/lib/DataSift/stream_consumer.rb +53 -70
- data/lib/DataSift/stream_consumer_http.rb +11 -15
- data/lib/DataSift/user.rb +189 -61
- data/lib/datasift.rb +5 -10
- data/test/helper.rb +80 -6
- data/test/test_definition.rb +0 -9
- data/test/test_historics.rb +233 -0
- data/test/test_pushdefinition.rb +92 -0
- data/test/test_pushsubscription.rb +17 -0
- data/test/test_user.rb +0 -6
- data/test/testdata.yml +26 -0
- metadata +38 -23
- data/test/test_live_api.rb +0 -100
@@ -0,0 +1,31 @@
|
|
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/lib/DataSift/apiclient.rb
CHANGED
@@ -1,32 +1,22 @@
|
|
1
|
-
#
|
2
|
-
# apiclient.rb - This file contains the ApiClient class.
|
3
|
-
#
|
4
|
-
# Copyright (C) 2011 MediaSift Ltd
|
5
|
-
#
|
6
|
-
# == Overview
|
7
|
-
#
|
8
|
-
# The ApiClient class wraps the functionality that makes calls to the
|
9
|
-
# DataSift API.
|
10
|
-
|
11
1
|
require 'rest_client'
|
12
2
|
require 'yajl'
|
13
3
|
|
14
4
|
module DataSift
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# == Introduction
|
18
|
-
#
|
19
|
-
# The ApiClient class wraps the functionality that makes calls to the
|
20
|
-
# DataSift API.
|
21
|
-
#
|
5
|
+
#The ApiClient class wraps the functionality that makes calls to the
|
6
|
+
#DataSift API.
|
22
7
|
class ApiClient
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
8
|
+
#Make a call to a DataSift API endpoint.
|
9
|
+
#=== Parameters
|
10
|
+
#* +endpoint+ - The endpoint of the API call.
|
11
|
+
#* +params+ - The parameters to be passed along with the request.
|
12
|
+
#* +username+ - The username for the Auth header
|
13
|
+
#* +api_key+ - The API key for the Auth header
|
14
|
+
#=== Returns
|
15
|
+
#A Hash contatining...
|
16
|
+
#* +response_code+ - The HTTP response code.
|
17
|
+
#* +data+ - A Hash containing the response data.
|
18
|
+
#* +rate_limit+ - The total API credits you get per hour.
|
19
|
+
#* +rate_limit_remaining+ - The number of API credits you have remaining for this hour.
|
30
20
|
def call(username, api_key, endpoint, params = {}, user_agent = 'DataSiftPHP/0.0')
|
31
21
|
# Build the full endpoint URL
|
32
22
|
url = 'http://' + User::API_BASE_URL + endpoint
|
@@ -64,11 +54,16 @@ module DataSift
|
|
64
54
|
retval['data'] = Yajl::Parser.parse(err.response)
|
65
55
|
end
|
66
56
|
|
67
|
-
retval
|
57
|
+
return retval
|
68
58
|
end
|
69
59
|
|
70
60
|
private
|
71
61
|
|
62
|
+
#Convert a Hash to an HTTP query string.
|
63
|
+
#=== Parameters
|
64
|
+
#* +hash+ - The Hash to convert.
|
65
|
+
#=== Returns
|
66
|
+
#A string containing the equivalent query string.
|
72
67
|
def hashToQuerystring(hash)
|
73
68
|
hash.keys.inject('') do |query_string, key|
|
74
69
|
query_string << '&' unless key == hash.keys.first
|
data/lib/DataSift/definition.rb
CHANGED
@@ -1,50 +1,34 @@
|
|
1
|
-
#
|
2
|
-
# definition.rb - This file contains the Definition class.
|
3
|
-
#
|
4
|
-
# Copyright (C) 2011 MediaSift Ltd
|
5
|
-
#
|
6
|
-
# == Overview
|
7
|
-
#
|
8
|
-
# The User class represents a user of the API. Applications should start their
|
9
|
-
# API interactions by creating an instance of this class. Once initialised it
|
10
|
-
# provides factory methods for all of the functionality in the API.
|
11
|
-
|
12
1
|
require 'date'
|
13
2
|
|
14
3
|
module DataSift
|
15
|
-
|
16
|
-
# Definition class.
|
17
|
-
#
|
18
|
-
# == Introduction
|
19
|
-
#
|
20
|
-
# The Definition class represents a stream definition.
|
21
|
-
#
|
4
|
+
#The Definition class represents a stream definition.
|
22
5
|
class Definition
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#
|
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.
|
33
15
|
def initialize(user, csdl = '', hash = false)
|
34
16
|
raise InvalidDataError, 'Please supply a valid User object when creating a Definition object.' unless user.is_a? DataSift::User
|
35
17
|
@user = user
|
36
18
|
clearHash()
|
37
19
|
@hash = hash
|
38
20
|
self.csdl = csdl
|
21
|
+
@total_dpu = false
|
22
|
+
@created_at = false
|
39
23
|
end
|
40
24
|
|
41
|
-
#
|
25
|
+
#CSDL getter
|
42
26
|
def csdl
|
43
27
|
raise InvalidDataError, 'The CSDL is not available' unless !@csdl.nil?
|
44
|
-
@csdl
|
28
|
+
return @csdl
|
45
29
|
end
|
46
30
|
|
47
|
-
#
|
31
|
+
#CSDL setter. Strips the incoming string and resets the hash if it's changed.
|
48
32
|
def csdl=(csdl)
|
49
33
|
if csdl.nil?
|
50
34
|
@csdl = nil
|
@@ -56,18 +40,30 @@ module DataSift
|
|
56
40
|
end
|
57
41
|
end
|
58
42
|
|
59
|
-
#
|
60
|
-
|
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.
|
61
57
|
def hash
|
62
58
|
if @hash == false
|
63
59
|
compile()
|
64
60
|
end
|
65
61
|
|
66
|
-
@hash
|
62
|
+
return @hash
|
67
63
|
end
|
68
64
|
|
69
|
-
#
|
70
|
-
#
|
65
|
+
#Reset the hash to false. The effect of this is to mark the definition as
|
66
|
+
#requiring compilation.
|
71
67
|
def clearHash()
|
72
68
|
@csdl = '' unless !@csdl.nil?
|
73
69
|
@hash = false
|
@@ -75,8 +71,8 @@ module DataSift
|
|
75
71
|
@created_at = false
|
76
72
|
end
|
77
73
|
|
78
|
-
#
|
79
|
-
#
|
74
|
+
#Call the DataSift API to compile this definition. On success it will
|
75
|
+
#store the returned hash.
|
80
76
|
def compile()
|
81
77
|
raise InvalidDataError, 'Cannot compile an empty definition.' unless @csdl.length > 0
|
82
78
|
|
@@ -107,28 +103,59 @@ module DataSift
|
|
107
103
|
when 400
|
108
104
|
raise CompileFailedError, err
|
109
105
|
else
|
110
|
-
raise
|
106
|
+
raise APIError('Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.inspect + ']', err.http_code)
|
111
107
|
end
|
112
108
|
end
|
113
109
|
end
|
114
110
|
|
115
|
-
#
|
116
|
-
#
|
117
|
-
|
118
|
-
|
119
|
-
|
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
|
120
147
|
def getDPUBreakdown()
|
121
148
|
raise InvalidDataError, "Cannot get the DPU for an empty definition." unless @csdl.length > 0
|
122
149
|
|
123
150
|
@user.callAPI('dpu', { 'hash' => self.hash })
|
124
151
|
end
|
125
152
|
|
126
|
-
#
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
#
|
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.
|
132
159
|
def getBuffered(count = false, from_id = false)
|
133
160
|
raise InvalidDataError, "Cannot get buffered interactions for an empty definition." unless @csdl.length > 0
|
134
161
|
|
@@ -146,15 +173,28 @@ module DataSift
|
|
146
173
|
|
147
174
|
raise APIError, 'No data in the response' unless retval.has_key?('stream')
|
148
175
|
|
149
|
-
retval['stream']
|
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)
|
150
190
|
end
|
151
191
|
|
152
|
-
#
|
153
|
-
#
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
#
|
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.
|
158
198
|
def getConsumer(type = nil, on_interaction = nil, on_stopped = nil)
|
159
199
|
StreamConsumer.factory(@user, type, self)
|
160
200
|
end
|
data/lib/DataSift/exceptions.rb
CHANGED
@@ -1,15 +1,32 @@
|
|
1
1
|
module DataSift
|
2
|
-
|
3
|
-
class
|
4
|
-
class InvalidDataError < StandardError; end
|
5
|
-
class NotYetImplementedError < StandardError; end
|
6
|
-
class RateLimitExceededError < StandardError; end
|
7
|
-
class StreamError < StandardError; end
|
2
|
+
# All exceptions inherit from DataSiftError.
|
3
|
+
class DataSiftError < StandardError; end
|
8
4
|
|
9
|
-
|
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.
|
10
24
|
attr_reader :http_code
|
11
25
|
|
12
|
-
|
26
|
+
#Constructor.
|
27
|
+
#=== Parameters
|
28
|
+
#* +http_code+ - Optional HTTP status code.
|
29
|
+
def initialize(http_code = -1)
|
13
30
|
@http_code = http_code
|
14
31
|
end
|
15
32
|
end
|
@@ -0,0 +1,321 @@
|
|
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
|
+
#The data availability for this Historics query.
|
62
|
+
attr_reader :volume_info
|
63
|
+
#True if this Historics query has been deleted.
|
64
|
+
attr_reader :is_deleted
|
65
|
+
|
66
|
+
#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.
|
67
|
+
#=== Parameters
|
68
|
+
#* +user+ - The DataSift::User object.
|
69
|
+
#* +hash+ - Either a stream_hash, an array containing the Historics query data or a playback ID.
|
70
|
+
#* +start_date+ - The start date for a new Historics query.
|
71
|
+
#* +end_date+ - The end date for a new Historics query.
|
72
|
+
#* +sources+ - An array of sources for a new Historics query.
|
73
|
+
#* +name+ - The name for a new Historics query.
|
74
|
+
#* +sample+ - The sample rate for the new Historics query.
|
75
|
+
def initialize(user, hash, start_date = false, end_date = false, sources = false, sample = false, name = false)
|
76
|
+
raise InvalidDataError, 'Please supply a valid User object when creating a Historic object.' unless user.is_a? DataSift::User
|
77
|
+
@user = user
|
78
|
+
|
79
|
+
if not start_date
|
80
|
+
if hash.kind_of?(Hash)
|
81
|
+
# Initialising from an array
|
82
|
+
@playback_id = hash['id']
|
83
|
+
initFromArray(hash)
|
84
|
+
else
|
85
|
+
# Fetching from the API
|
86
|
+
@playback_id = hash
|
87
|
+
reloadData()
|
88
|
+
end
|
89
|
+
else
|
90
|
+
# Creating a new Historic query, make sure we have all the parameters
|
91
|
+
raise InvalidDataError,
|
92
|
+
'Please supply all parameters when creating a new Historics query' unless
|
93
|
+
start_date != false and end_date != false and sources != false and
|
94
|
+
name != false and sample != false
|
95
|
+
|
96
|
+
# Convert and validate the parameters as required
|
97
|
+
hash = hash.hash if hash.is_a? DataSift::Definition
|
98
|
+
start_date = DateTime.strftime(start_date, '%s') unless start_date.is_a? Date
|
99
|
+
end_date = DateTime.strptime(end_date, '%s') unless end_date.is_a? Date
|
100
|
+
raise InvalidDataError, 'Please supply an array of sources' unless sources.kind_of?(Array)
|
101
|
+
|
102
|
+
@playback_id = false
|
103
|
+
@stream_hash = hash
|
104
|
+
@start_date = start_date
|
105
|
+
@end_date = end_date
|
106
|
+
@sources = sources
|
107
|
+
@name = name
|
108
|
+
@sample = sample
|
109
|
+
@progress = 0
|
110
|
+
@dpus = false
|
111
|
+
@availability = {}
|
112
|
+
@volume_info = {}
|
113
|
+
@is_deleted = false
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
#Reload the data for this object from the API.
|
118
|
+
def reloadData()
|
119
|
+
#Can't do this if we've been deleted
|
120
|
+
raise InvalidDataError, 'Cannot reload the data for a deleted Historics query' unless not @is_deleted
|
121
|
+
|
122
|
+
#Can't do this without a playback ID
|
123
|
+
raise InvalidDataError, 'Cannot reload the data with a Historics query with no playback ID' unless @playback_id
|
124
|
+
|
125
|
+
begin
|
126
|
+
initFromArray(@user.callAPI('historics/get', { 'id' => @playback_id }))
|
127
|
+
rescue APIError => err
|
128
|
+
case err.http_code
|
129
|
+
when 400
|
130
|
+
raise InvalidDataError, err
|
131
|
+
else
|
132
|
+
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
#Initialise this obejct from the data in a Hash.
|
138
|
+
#=== Parameters
|
139
|
+
#* +data+ - The Hash containing the data.
|
140
|
+
def initFromArray(data)
|
141
|
+
raise APIError, 'No playback ID in the response' unless data.has_key?('id')
|
142
|
+
raise APIError, 'Incorrect playback ID in the response' unless not @playback_id or data['id'] == @playback_id
|
143
|
+
@playback_id = data['id']
|
144
|
+
|
145
|
+
raise APIError, 'No definition hash in the response' unless data.has_key?('definition_id')
|
146
|
+
@stream_hash = data['definition_id']
|
147
|
+
|
148
|
+
raise APIError, 'No name in the response' unless data.has_key?('name')
|
149
|
+
@name = data['name']
|
150
|
+
|
151
|
+
raise APIError, 'No start timestamp in the response' unless data.has_key?('start')
|
152
|
+
@start_date = DateTime.strptime(String(data['start']), '%s')
|
153
|
+
|
154
|
+
raise APIError, 'No end timestamp in the response' unless data.has_key?('end')
|
155
|
+
@end_date = DateTime.strptime(String(data['end']), '%s')
|
156
|
+
|
157
|
+
raise APIError, 'No created at timstamp in the response' unless data.has_key?('created_at')
|
158
|
+
@created_at = DateTime.strptime(String(data['created_at']), '%s')
|
159
|
+
|
160
|
+
raise APIError, 'No status in the response' unless data.has_key?('status')
|
161
|
+
@status = data['status']
|
162
|
+
|
163
|
+
raise APIError, 'No progress in the response' unless data.has_key?('progress')
|
164
|
+
@progress = data['progress']
|
165
|
+
|
166
|
+
raise APIError, 'No sources in the response' unless data.has_key?('sources')
|
167
|
+
@sources = data['sources']
|
168
|
+
|
169
|
+
raise APIError, 'No sample in the response' unless data.has_key?('sample')
|
170
|
+
@sample = data['sample']
|
171
|
+
|
172
|
+
raise APIError, 'No volume info in the response' unless data.has_key?('volume_info')
|
173
|
+
@volume_info = data['volume_info']
|
174
|
+
|
175
|
+
@is_deleted = (@status == 'deleted')
|
176
|
+
|
177
|
+
return true
|
178
|
+
end
|
179
|
+
|
180
|
+
#Getter for the playback ID. If the Historics query has not yet been
|
181
|
+
#prepared that will be done automagically to obtain the playback ID.
|
182
|
+
def hash
|
183
|
+
if @playback_id == false
|
184
|
+
prepare()
|
185
|
+
end
|
186
|
+
|
187
|
+
@playback_id
|
188
|
+
end
|
189
|
+
|
190
|
+
#Name setter. Updates via the API if this Historics query has already
|
191
|
+
#been prepared.
|
192
|
+
def name=(new_name)
|
193
|
+
raise InvalidDataError, 'Cannot set the name of a deleted Historics query' unless not @is_deleted
|
194
|
+
|
195
|
+
if not @playback_id
|
196
|
+
@name = new_name
|
197
|
+
else
|
198
|
+
@user.callAPI('historics/update', { 'id' => @playback_id, 'name' => new_name })
|
199
|
+
reloadData()
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
#Call the DataSift API to prepare this Historics query
|
204
|
+
def prepare()
|
205
|
+
raise InvalidDataError, 'Cannot prepare a deleted Historics query' unless not @is_deleted
|
206
|
+
raise InvalidDataError, 'This Historics query has already been prepared' unless not @playback_id
|
207
|
+
|
208
|
+
begin
|
209
|
+
res = @user.callAPI(
|
210
|
+
'historics/prepare', {
|
211
|
+
'hash' => @stream_hash,
|
212
|
+
'start' => Integer(@start_date.strftime('%s')),
|
213
|
+
'end' => Integer(@end_date.strftime('%s')),
|
214
|
+
'name' => @name,
|
215
|
+
'sources' => @sources.join(',')
|
216
|
+
})
|
217
|
+
|
218
|
+
raise InvalidDataError, 'Prepared successfully but no playback ID in the response' unless res.has_key?('id')
|
219
|
+
@playback_id = res['id']
|
220
|
+
|
221
|
+
raise InvalidDataError, 'Prepared successfully but no DPU cost in the response' unless res.has_key?('dpus')
|
222
|
+
@dpus = res['dpus']
|
223
|
+
|
224
|
+
raise InvalidDataError, 'Prepared successfully but no availability in the response' unless res.has_key?('availability')
|
225
|
+
@availability = res['availability']
|
226
|
+
rescue APIError => err
|
227
|
+
case err.http_code
|
228
|
+
when 400
|
229
|
+
raise InvalidDataError, err
|
230
|
+
else
|
231
|
+
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# Reload the data so we get the created_at date, initial status and the rest.
|
236
|
+
reloadData()
|
237
|
+
end
|
238
|
+
|
239
|
+
#Start this Historics query.
|
240
|
+
def start()
|
241
|
+
raise InvalidDataError, 'Cannot start a deleted Historics query' unless not @is_deleted
|
242
|
+
raise InvalidDataError, 'Cannot start a Historics query that hasn\'t been prepared' unless @playback_id
|
243
|
+
|
244
|
+
begin
|
245
|
+
res = @user.callAPI('historics/start', { 'id' => @playback_id })
|
246
|
+
rescue APIError => err
|
247
|
+
case err.http_code
|
248
|
+
when 400
|
249
|
+
# Missing or invalid parameters
|
250
|
+
raise InvalidDataError, err
|
251
|
+
when 404
|
252
|
+
# Historics query not found
|
253
|
+
raise InvalidDataError, err
|
254
|
+
else
|
255
|
+
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
#Stop this Historics query.
|
261
|
+
def stop()
|
262
|
+
raise InvalidDataError, 'Cannot stop a deleted Historics query' unless not @is_deleted
|
263
|
+
raise InvalidDataError, 'Cannot stop a Historics query that hasn\'t been prepared' unless @playback_id
|
264
|
+
|
265
|
+
begin
|
266
|
+
res = @user.callAPI('historics/stop', { 'id' => @playback_id })
|
267
|
+
rescue APIError => err
|
268
|
+
case err.http_code
|
269
|
+
when 400
|
270
|
+
# Missing or invalid parameters
|
271
|
+
raise InvalidDataError, err
|
272
|
+
when 404
|
273
|
+
# Historics query not found
|
274
|
+
raise InvalidDataError, err
|
275
|
+
else
|
276
|
+
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
#Delete this Historics query.
|
282
|
+
def delete()
|
283
|
+
raise InvalidDataError, 'Cannot delete a deleted Historics query' unless not @is_deleted
|
284
|
+
raise InvalidDataError, 'Cannot delete a Historics query that hasn\'t been prepared' unless @playback_id
|
285
|
+
|
286
|
+
begin
|
287
|
+
@user.callAPI('historics/delete', { 'id' => @playback_id })
|
288
|
+
@is_deleted = true
|
289
|
+
rescue APIError => err
|
290
|
+
case err.http_code
|
291
|
+
when 400
|
292
|
+
# Missing or invalid parameters
|
293
|
+
raise InvalidDataError, err
|
294
|
+
when 404
|
295
|
+
# Historics query not found
|
296
|
+
raise InvalidDataError, err
|
297
|
+
else
|
298
|
+
raise APIError.new(err.http_code), 'Unexpected APIError code: ' + err.http_code.to_s + ' [' + err.message + ']'
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
#Get a page of Push subscriptions for this Historics query, where each
|
304
|
+
#page contains up to per_page items. Results will be returned in the
|
305
|
+
#order requested.
|
306
|
+
#=== Parameters
|
307
|
+
#* +page+ - The page number to get.
|
308
|
+
#* +per_page+ - The number of items per page.
|
309
|
+
#* +order_by+ - The field by which to order the results.
|
310
|
+
#* +order_dir+ - Ascending or descending.
|
311
|
+
#* +include_finished+ - True to include subscriptions against finished Historics queries.
|
312
|
+
#=== Returns
|
313
|
+
#A Hash containing...
|
314
|
+
#* +count+ - The total number of Push subscriptions in your account.
|
315
|
+
#* +subscriptions+ - An array of Hashes where each Hash is a Push subscription.
|
316
|
+
def getPushSubscriptions(page = 1, per_page = 20, order_by = PushSubscription::ORDERBY_CREATED_AT, order_dir = PushSubscription::ORDERDIR_ASC)
|
317
|
+
return PushSubscription.list(@user, page, per_page, order_by, order_dir, true, 'playback_id', @playback_id)
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
end
|