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
@@ -1,46 +1,43 @@
|
|
1
|
-
#
|
2
|
-
# stream_consumer.rb - This file contains the StreamConsumer class.
|
3
|
-
#
|
4
|
-
# Copyright (C) 2011 MediaSift Ltd
|
5
|
-
#
|
6
|
-
# == Overview
|
7
|
-
#
|
8
|
-
# The StreamConsumer class is base class for various stream consumers.
|
9
|
-
|
10
1
|
module DataSift
|
11
|
-
|
12
|
-
# StreamConsumer class.
|
13
|
-
#
|
2
|
+
#This is the base class for all StreamConsumer implementation.
|
14
3
|
class StreamConsumer
|
4
|
+
#Constant for the HTTP StreamConsumer implementation option.
|
15
5
|
TYPE_HTTP = 'HTTP'
|
16
6
|
|
7
|
+
#Constant for the "stopped" status.
|
17
8
|
STATE_STOPPED = 0
|
9
|
+
#Constant for the "starting" status.
|
18
10
|
STATE_STARTING = 1
|
11
|
+
#Constant for the "running" status.
|
19
12
|
STATE_RUNNING = 2
|
13
|
+
#Constant for the "stopping" status.
|
20
14
|
STATE_STOPPING = 3
|
21
15
|
|
22
|
-
#
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
#
|
16
|
+
#Factory function. Creates a StreamConsumer-derived object for the given
|
17
|
+
#type.
|
18
|
+
#=== Parameters
|
19
|
+
#* +type+ - Use the TYPE_ constants
|
20
|
+
#* +definition+ - CSDL string or a Definition object.
|
21
|
+
#=== Returns
|
22
|
+
#A StreamConsumer-derived object.
|
29
23
|
def self.factory(user, type, definition)
|
30
24
|
type ||= TYPE_HTTP
|
31
25
|
@klass = Module.const_get('DataSift').const_get('StreamConsumer_' + type)
|
32
26
|
@klass.new(user, definition)
|
33
27
|
end
|
34
28
|
|
29
|
+
#Whether the consumer should automatically try to reconnect if the
|
30
|
+
#connection is dropped.
|
35
31
|
attr_accessor :auto_reconnect
|
36
|
-
|
37
|
-
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
|
43
|
-
|
32
|
+
#The current state of the consumer.
|
33
|
+
attr_reader :state
|
34
|
+
#The reason the consumer was stopped.
|
35
|
+
attr_reader :stop_reason
|
36
|
+
|
37
|
+
#Constructor. Do not use this directly, use the factory method instead.
|
38
|
+
#=== Parameters
|
39
|
+
#* +user+ - The user this consumer will run as.
|
40
|
+
#* +definition+ - CSDL string or a Definition object.
|
44
41
|
def initialize(user, definition)
|
45
42
|
raise InvalidDataError, 'Please supply a valid User object when creating a Definition object.' unless user.is_a? DataSift::User
|
46
43
|
|
@@ -61,11 +58,9 @@ module DataSift
|
|
61
58
|
@definition.hash
|
62
59
|
end
|
63
60
|
|
64
|
-
#
|
65
|
-
|
66
|
-
|
67
|
-
# * +interaction+ - Minimal details about the interaction that was deleted.
|
68
|
-
#
|
61
|
+
#Called when a deletion notification is received.
|
62
|
+
#=== Parameters
|
63
|
+
#* +interaction+ - Minimal details about the interaction that was deleted.
|
69
64
|
def onDeleted(&block)
|
70
65
|
if block_given?
|
71
66
|
@on_deleted = block
|
@@ -75,11 +70,9 @@ module DataSift
|
|
75
70
|
end
|
76
71
|
end
|
77
72
|
|
78
|
-
#
|
79
|
-
|
80
|
-
|
81
|
-
# * +message+ - The error message.
|
82
|
-
#
|
73
|
+
#This is called when an error message is received.
|
74
|
+
#=== Parameters
|
75
|
+
#* +message+ - The error message.
|
83
76
|
def onError(&block)
|
84
77
|
if block_given?
|
85
78
|
@on_error = block
|
@@ -89,11 +82,9 @@ module DataSift
|
|
89
82
|
end
|
90
83
|
end
|
91
84
|
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
# * +message+ - The error message.
|
96
|
-
#
|
85
|
+
#This is called when an error message is received.
|
86
|
+
#=== Parameters
|
87
|
+
#* +message+ - The error message.
|
97
88
|
def onWarning(&block)
|
98
89
|
if block_given?
|
99
90
|
@on_warning = block
|
@@ -103,11 +94,9 @@ module DataSift
|
|
103
94
|
end
|
104
95
|
end
|
105
96
|
|
106
|
-
#
|
107
|
-
|
108
|
-
|
109
|
-
# * +reason+ - The reason why the consumer stopped.
|
110
|
-
#
|
97
|
+
#This is called when the consumer is stopped.
|
98
|
+
#=== Parameters
|
99
|
+
#* +reason+ - The reason why the consumer stopped.
|
111
100
|
def onStopped(&block)
|
112
101
|
if block_given?
|
113
102
|
@on_stopped = block
|
@@ -117,14 +106,12 @@ module DataSift
|
|
117
106
|
end
|
118
107
|
end
|
119
108
|
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
# * +block+ - An optional block to receive incoming interactions.
|
127
|
-
#
|
109
|
+
#Once an instance of a StreamConsumer is ready for use, call this to
|
110
|
+
#start consuming. Extending classes should implement onStart to handle
|
111
|
+
#actually starting.
|
112
|
+
#=== Parameters
|
113
|
+
#* +auto_reconnect+ - Whether the consumer should automatically reconnect.
|
114
|
+
#* +block+ - An optional block to receive incoming interactions.
|
128
115
|
def consume(auto_reconnect = true, &block)
|
129
116
|
@auto_reconnect = auto_reconnect;
|
130
117
|
|
@@ -149,29 +136,25 @@ module DataSift
|
|
149
136
|
end
|
150
137
|
end
|
151
138
|
|
152
|
-
#
|
153
|
-
#
|
139
|
+
#Called when the consumer should start consuming the stream.
|
154
140
|
def onStart()
|
155
|
-
|
141
|
+
abort('onStart method has not been overridden!')
|
156
142
|
end
|
157
143
|
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
144
|
+
#This method can be called at any time to *request* that the consumer
|
145
|
+
#stop consuming. This method sets the state to STATE_STOPPING and it's
|
146
|
+
#up to the consumer implementation to notice that this has changed, stop
|
147
|
+
#consuming and call the onStopped method.
|
163
148
|
def stop()
|
164
149
|
raise InvalidDataError, 'Consumer state must be RUNNING before it can be stopped' unless @state = StreamConsumer::STATE_RUNNING
|
165
150
|
@state = StreamConsumer::STATE_STOPPING
|
166
151
|
end
|
167
152
|
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
171
|
-
|
172
|
-
|
173
|
-
# * +reason+ - The reason why the consumer stopped.
|
174
|
-
#
|
153
|
+
#Default implementation of onStop. It's unlikely that this method will
|
154
|
+
#ever be used in isolation, but rather it should be called as the final
|
155
|
+
#step in the extending class's implementation.
|
156
|
+
#=== Parameters
|
157
|
+
#* +reason+ - The reason why the consumer stopped.
|
175
158
|
def onStop(reason = '')
|
176
159
|
reason = 'Unexpected' unless @state != StreamConsumer::STATE_STOPPING and reason.length == 0
|
177
160
|
@state = StreamConsumer::STATE_STOPPED
|
@@ -1,12 +1,3 @@
|
|
1
|
-
#
|
2
|
-
# stream_consumer_http.rb - This file contains the StreamConsumer_HTTP class.
|
3
|
-
#
|
4
|
-
# Copyright (C) 2011 MediaSift Ltd
|
5
|
-
#
|
6
|
-
# == Overview
|
7
|
-
#
|
8
|
-
# The StreamConsumer_HTTP class implements HTTP streaming.
|
9
|
-
|
10
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
|
11
2
|
|
12
3
|
require 'uri'
|
@@ -15,14 +6,19 @@ require 'yajl'
|
|
15
6
|
require 'cgi'
|
16
7
|
|
17
8
|
module DataSift
|
18
|
-
|
9
|
+
#The HTTP implementation of the StreamConsumer.
|
19
10
|
class StreamConsumer_HTTP < StreamConsumer
|
20
|
-
|
21
|
-
|
11
|
+
#Constructor. Requires valid user and definition objects.
|
12
|
+
#=== Parameters
|
13
|
+
#* +user+ - The user consuming the data.
|
14
|
+
#* +definition+ - The Definition to consume.
|
22
15
|
def initialize(user, definition)
|
23
16
|
super
|
24
17
|
end
|
25
18
|
|
19
|
+
#Called when the consumer is started.
|
20
|
+
#=== Parameters
|
21
|
+
#* +block+ - A block to receive incoming data.
|
26
22
|
def onStart(&block)
|
27
23
|
begin
|
28
24
|
reconnect() unless !@socket.nil? and !@socket.closed?
|
@@ -85,8 +81,9 @@ module DataSift
|
|
85
81
|
onStop(@stop_reason)
|
86
82
|
end
|
87
83
|
|
88
|
-
|
84
|
+
private
|
89
85
|
|
86
|
+
#Reconnect the stream socket.
|
90
87
|
def reconnect()
|
91
88
|
uri = URI.parse('http' + (@user.use_ssl ? 's' : '') + '://' + User::STREAM_BASE_URL + @definition.hash)
|
92
89
|
|
@@ -175,11 +172,10 @@ module DataSift
|
|
175
172
|
end while @state != StreamConsumer::STATE_RUNNING
|
176
173
|
end
|
177
174
|
|
175
|
+
#Disconnect the stream socket.
|
178
176
|
def disconnect()
|
179
177
|
@socket.close if !@socket.nil? and !@socket.closed?
|
180
178
|
@raw_socket.close if !@raw_socket.nil? and !@raw_socket.closed?
|
181
179
|
end
|
182
|
-
|
183
180
|
end
|
184
|
-
|
185
181
|
end
|
data/lib/DataSift/user.rb
CHANGED
@@ -1,36 +1,36 @@
|
|
1
|
-
#
|
2
|
-
# user.rb - This file contains the User 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
|
-
|
1
|
+
#This is the official DataSift client library for Ruby.
|
12
2
|
module DataSift
|
13
|
-
# User class.
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# The User class represents a user of the API. Applications should start their
|
18
|
-
# API interactions by creating an instance of this class. Once initialised it
|
19
|
-
# provides factory methods for all of the functionality in the API.
|
20
|
-
#
|
3
|
+
#The User class represents a user of the API. Applications should start their
|
4
|
+
#API interactions by creating an instance of this class. Once initialised it
|
5
|
+
#provides factory methods for all of the functionality in the API.
|
21
6
|
class User
|
7
|
+
#The user agent to pass through with all HTTP requests.
|
22
8
|
USER_AGENT = 'DataSiftRuby/' + File.open(File.dirname(File.dirname(File.dirname(__FILE__))) + '/VERSION').first;
|
9
|
+
#The base URL for API requests.
|
23
10
|
API_BASE_URL = 'api.datasift.com/';
|
11
|
+
#The base URL for streams.
|
24
12
|
STREAM_BASE_URL = 'stream.datasift.com/';
|
25
13
|
|
26
|
-
|
14
|
+
#The User's DataSift username.
|
15
|
+
attr_reader :username
|
16
|
+
#The User's DataSift API key.
|
17
|
+
attr_reader :api_key
|
18
|
+
#The User's total number of available hourly API credits. This is not
|
19
|
+
#populated until an API request is made.
|
20
|
+
attr_reader :rate_limit
|
21
|
+
#The User's API credits remaining. This is not populated until an API
|
22
|
+
#request is made.
|
23
|
+
attr_reader :rate_limit_remaining
|
24
|
+
#The APIClient class to use when making API requests.
|
25
|
+
attr_reader :api_client
|
26
|
+
#True if streaming connections should use SSL.
|
27
|
+
attr_reader :use_ssl
|
27
28
|
|
28
|
-
#
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# * +api_key+ - The user's API key
|
29
|
+
#Constructor. A username and API key are required when constructing an
|
30
|
+
#instance of this class.
|
31
|
+
#=== Parameters
|
32
|
+
#* +username+ - The User's DataSift username
|
33
|
+
#* +api_key+ - The User's DataSift API key
|
34
34
|
def initialize(username, api_key, use_ssl = true)
|
35
35
|
username.strip!
|
36
36
|
api_key.strip!
|
@@ -44,65 +44,191 @@ module DataSift
|
|
44
44
|
@use_ssl = use_ssl
|
45
45
|
end
|
46
46
|
|
47
|
-
#
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
#Creates and returns a definition object.
|
48
|
+
#=== Parameters
|
49
|
+
#* +csdl+ - Optional CSDL string with which to prime the object.
|
50
|
+
#=== Returns
|
51
|
+
#A Definition object.
|
51
52
|
def createDefinition(csdl = '')
|
52
53
|
DataSift::Definition.new(self, csdl, false)
|
53
54
|
end
|
54
55
|
|
55
|
-
#
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
#Create a Historics query based on this Definition.
|
57
|
+
#=== Parameters
|
58
|
+
#* +hash+ - The stream hash for a new Historics query.
|
59
|
+
#* +start_date+ - The start date for a new Historics query.
|
60
|
+
#* +end_date+ - The end date for a new Historics query.
|
61
|
+
#* +sources+ - An array of sources for a new Historics query.
|
62
|
+
#* +name+ - The name for a new Historics query.
|
63
|
+
#* +sample+ - The sample rate for the new Historics query.
|
64
|
+
#=== Returns
|
65
|
+
#A Historic object.
|
66
|
+
def createHistoric(hash, start_date, end_date, sources, sample, name)
|
67
|
+
return Historic.new(self, hash, start_date, end_date, sources, sample, name)
|
68
|
+
end
|
69
|
+
|
70
|
+
#Get a Historics query from the API.
|
71
|
+
#=== Parameters
|
72
|
+
#* +playback_id+ - The playback ID of the Historics query to retrieve.
|
73
|
+
#=== Returns
|
74
|
+
#A Historic object.
|
75
|
+
def getHistoric(playback_id)
|
76
|
+
return Historic.new(self, playback_id)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Get a list of Historics queries in your account.
|
80
|
+
#=== Parameters
|
81
|
+
#* +page+ - The page number to get.
|
82
|
+
#* +per_page+ - The number of items per page.
|
83
|
+
#=== Returns
|
84
|
+
#A Hash containing...
|
85
|
+
#* +count+ - The total number of Historics queries in your account.
|
86
|
+
#* +historics+ - An array of Hashes where each Hash is a Historics query.
|
87
|
+
def listHistorics(page = 1, per_page = 20)
|
88
|
+
return Historic::list(self, page, per_page)
|
89
|
+
end
|
90
|
+
|
91
|
+
#Create a new PushDefinition object for this user.
|
92
|
+
#=== Returns
|
93
|
+
#A PushDefinition object.
|
94
|
+
def createPushDefinition()
|
95
|
+
return PushDefinition.new(self)
|
96
|
+
end
|
97
|
+
|
98
|
+
#Get an existing PushSubscription from the API.
|
99
|
+
#=== Parameters
|
100
|
+
#* +subscription_id+ - The ID of the subscription to fetch.
|
101
|
+
#=== Returns
|
102
|
+
#A PushSubscription object.
|
103
|
+
def getPushSubscription(subscription_id)
|
104
|
+
return PushSubscription.get(self, subscription_id)
|
105
|
+
end
|
106
|
+
|
107
|
+
#Get the log entries for all push subscription or the given subscription.
|
108
|
+
#=== Parameters
|
109
|
+
#* +subscription_id+ - Optional subscription ID.
|
110
|
+
#=== Returns
|
111
|
+
#A Hash containing...
|
112
|
+
#* +count+ - The total number of matching log entries.
|
113
|
+
#* +log_entries+ - An array of Hashes where each Hash is a log entry.
|
114
|
+
def getPushSubscriptionLog(subscription_id = false)
|
115
|
+
if subscription_id
|
116
|
+
return getPushSubscription(subscription_id).getLog()
|
117
|
+
else
|
118
|
+
return PushSubscription.getLogs(self)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
#Get a page of Push subscriptions in the given user's account, where each
|
123
|
+
#page contains up to per_page items. Results will be ordered according to
|
124
|
+
#the supplied ordering parameters.
|
125
|
+
#=== Parameters
|
126
|
+
#* +page+ - The page number to get.
|
127
|
+
#* +per_page+ - The number of items per page.
|
128
|
+
#* +order_by+ - The field by which to order the results.
|
129
|
+
#* +order_dir+ - Ascending or descending.
|
130
|
+
#* +include_finished+ - True to include subscriptions against finished Historics queries.
|
131
|
+
#=== Returns
|
132
|
+
#A Hash containing...
|
133
|
+
#* +count+ - The total number of matching Push subscriptions in your account.
|
134
|
+
#* +subscriptions+ - An array of Hashes where each Hash is a Push subscription.
|
135
|
+
def listPushSubscriptions(page = 1, per_page = 20, order_by = PushSubscription::ORDERBY_CREATED_AT, order_dir = PushSubscription::ORDERDIR_ASC, include_finished = false)
|
136
|
+
return PushSubscription.list(self, page, per_page, order_by, order_dir, include_finished)
|
137
|
+
end
|
138
|
+
|
139
|
+
#Get a page of Push subscriptions in the given user's account, where each
|
140
|
+
#page contains up to per_page items. Results will be ordered according to
|
141
|
+
#the supplied ordering parameters.
|
142
|
+
#=== Parameters
|
143
|
+
#* +hash+ - The stream hash.
|
144
|
+
#* +page+ - The page number to get.
|
145
|
+
#* +per_page+ - The number of items per page.
|
146
|
+
#* +order_by+ - The field by which to order the results.
|
147
|
+
#* +order_dir+ - Ascending or descending.
|
148
|
+
#* +include_finished+ - True to include subscriptions against finished Historics queries.
|
149
|
+
#=== Returns
|
150
|
+
#A Hash containing...
|
151
|
+
#* +count+ - The total number of matching Push subscriptions in your account.
|
152
|
+
#* +subscriptions+ - An array of Hashes where each Hash is a Push subscription.
|
153
|
+
def listPushSubscriptionsToStreamHash(hash, page = 1, per_page = 20, order_by = PushSubscription::ORDERBY_CREATED_AT, order_dir = PushSubscription::ORDERDIR_ASC, include_finished = false)
|
154
|
+
return PushSubscription.listByStreamHash(self, hash, page, per_page, order_by, order_dir)
|
155
|
+
end
|
156
|
+
|
157
|
+
#Get a page of Push subscriptions in the given user's account, where each
|
158
|
+
#page contains up to per_page items. Results will be ordered according to
|
159
|
+
#the supplied ordering parameters.
|
160
|
+
#=== Parameters
|
161
|
+
#* +hash+ - The stream hash.
|
162
|
+
#* +page+ - The page number to get.
|
163
|
+
#* +per_page+ - The number of items per page.
|
164
|
+
#* +order_by+ - The field by which to order the results.
|
165
|
+
#* +order_dir+ - Ascending or descending.
|
166
|
+
#* +include_finished+ - True to include subscriptions against finished Historics queries.
|
167
|
+
#=== Returns
|
168
|
+
#A Hash containing...
|
169
|
+
#* +count+ - The total number of matching Push subscriptions in your account.
|
170
|
+
#* +subscriptions+ - An array of Hashes where each Hash is a Push subscription.
|
171
|
+
def listPushSubscriptionsToPlaybackId(playback_id, page = 1, per_page = 20, order_by = PushSubscription::ORDERBY_CREATED_AT, order_dir = PushSubscription::ORDERDIR_ASC, include_finished = false)
|
172
|
+
return PushSubscription.listByPlaybackId(self, playback_id, page, per_page, order_by, order_dir)
|
173
|
+
end
|
174
|
+
|
175
|
+
#Returns a StreamConsumer-derived object for the given hash, for the
|
176
|
+
#given type.
|
177
|
+
#=== Parameters
|
178
|
+
#* +type+ - The consumer type for which to construct a consumer.
|
179
|
+
#* +hash+ - The hash to be consumed.
|
180
|
+
#=== Returns
|
181
|
+
#A StreamConsumer-derived object.
|
62
182
|
def getConsumer(type = nil, hash = nil, on_interaction = nil, on_stopped = nil)
|
63
183
|
StreamConsumer.factory(self, type, Definition.new(self, nil, hash))
|
64
184
|
end
|
65
185
|
|
66
|
-
#
|
186
|
+
#Returns the account balance information for this user.
|
187
|
+
#=== Returns
|
188
|
+
#A Hash containing the balance information.
|
67
189
|
def getBalance
|
68
|
-
callAPI('balance')['balance']
|
190
|
+
return callAPI('balance')['balance']
|
69
191
|
end
|
70
192
|
|
71
|
-
#
|
72
|
-
#
|
73
|
-
|
74
|
-
|
75
|
-
|
193
|
+
#Returns the usage data for this user. If a hash is provided then a more
|
194
|
+
#detailed breakdown using interaction types is retrieved and returned.
|
195
|
+
#=== Parameters
|
196
|
+
#* +period+ - An optional period for which to fetch data ('hour' or 'day')
|
197
|
+
#=== Returns
|
198
|
+
#A Hash containing the usage information.
|
76
199
|
def getUsage(period = 'hour')
|
77
|
-
if period != 'hour' and period != 'day'
|
78
|
-
raise EInvalidData, 'Period must be hour or day'
|
79
|
-
end
|
80
|
-
|
81
200
|
params = { 'period' => period }
|
82
201
|
|
83
|
-
callAPI('usage', params)
|
202
|
+
return callAPI('usage', params)
|
84
203
|
end
|
85
204
|
|
86
|
-
#
|
205
|
+
#Returns the user agent this library should use for all API calls.
|
206
|
+
#=== Returns
|
207
|
+
#The user agent string.
|
87
208
|
def getUserAgent()
|
88
|
-
USER_AGENT
|
209
|
+
return USER_AGENT
|
89
210
|
end
|
90
211
|
|
91
|
-
#
|
212
|
+
#Sets the ApiClient object to use to access the API
|
213
|
+
#=== Parameters
|
214
|
+
#* +client+ - The API client object to be used.
|
92
215
|
def setApiClient(client)
|
93
216
|
@api_client = client
|
94
217
|
end
|
95
218
|
|
96
|
-
#
|
219
|
+
#Sets whether to use SSL for API and stream communication.
|
220
|
+
#=== Parameters
|
221
|
+
#* +use_ssl+ - Pass true to use SSL.
|
97
222
|
def enableSSL(use_ssl = true)
|
98
223
|
@use_ssl = use_ssl
|
99
224
|
end
|
100
225
|
|
101
|
-
#
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
226
|
+
#Make a call to a DataSift API endpoint.
|
227
|
+
#=== Parameters
|
228
|
+
#* +endpoint+ - The endpoint of the API call.
|
229
|
+
#* +params+ - A Hash of parameters to be passed along with the request.
|
230
|
+
#=== Returns
|
231
|
+
#A Hash containing the response data.
|
106
232
|
def callAPI(endpoint, params = {})
|
107
233
|
if !@api_client
|
108
234
|
@api_client = ApiClient.new()
|
@@ -119,6 +245,8 @@ module DataSift
|
|
119
245
|
|
120
246
|
case res['response_code']
|
121
247
|
when 200
|
248
|
+
when 201
|
249
|
+
when 204
|
122
250
|
# Do nothing
|
123
251
|
when 401
|
124
252
|
# Authentication failure
|
@@ -127,12 +255,12 @@ module DataSift
|
|
127
255
|
# Check the rate limit
|
128
256
|
raise RateLimitExceededError, retval['comment'] if @rate_limit_remaining == 0
|
129
257
|
# Rate limit is ok, raise a generic exception
|
130
|
-
raise APIError.new(
|
258
|
+
raise APIError.new(res['response_code']), retval.has_key?('error') ? retval['error'] : 'Unknown error'
|
131
259
|
else
|
132
|
-
raise APIError.new(res['
|
260
|
+
raise APIError.new(res['response_code']), retval.has_key?('error') ? retval['error'] : 'Unknown error'
|
133
261
|
end
|
134
262
|
|
135
|
-
retval
|
263
|
+
return retval
|
136
264
|
end
|
137
265
|
end
|
138
266
|
end
|