Class: Simperium::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/simperium.rb

Instance Method Summary collapse

Constructor Details

#initialize(appname, auth_token, bucket, options = {}) ⇒ Bucket

Returns a new instance of Bucket.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/simperium.rb', line 93

def initialize(appname, auth_token, bucket, options={})
    defaults = { :userid => nil, :host => nil, :scheme => 'https', :clientid => nil }
    unless options.empty?
        options = defaults.merge(options)
    else
        options = defaults
    end

    if options[:host] == nil
        options[:host] = ENV['SIMPERIUM_APIHOST'] || 'api.simperium.com'
    end

    @userid = options[:userid]
    @host = options[:host]
    @scheme = options[:scheme]
    @appname = appname
    @bucket = bucket
    @auth_token = auth_token

    if options[:clientid] == nil
        uuid = UUID.new
        random_string = uuid.generate(:compact)
        @clientid = "rb-#{random_string}"
    else
        @clientid = options[:clientid]
    end
end

Instance Method Details

#_auth_headerObject



121
122
123
124
125
126
127
# File 'lib/simperium.rb', line 121

def _auth_header
    headers = {"X-Simperium-Token" => "#{@auth_token}"}
    unless @userid.nil?
        headers["X-Simperium-User"] = @userid
    end
    return headers
end

#_gen_ccidObject



129
130
131
132
# File 'lib/simperium.rb', line 129

def _gen_ccid
    ccid = UUID.new
    return ccid.generate(:compact)
end

#_request(url, data = nil, headers = nil, method = nil, timeout = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/simperium.rb', line 134

def _request(url, data=nil, headers=nil, method=nil, timeout=nil)
    url = "#{@scheme}://#{@host}/1/#{url}"
    opts = {:url => url,
            :method => :post,
            :open_timeout => 30, 
            :timeout => 80}
    
    if data
        opts = opts.merge({:payload => data})
    end
    
    if headers.nil?
        headers = {}
    end
    opts = opts.merge({:headers => headers})
    
    if method
        opts = opts.merge({:method => method})
    end
    
    if timeout
        opts = opts.merge({:timeout => timeout})
    end
    
    puts opts
    begin
        response = RestClient::Request.execute(opts)
    rescue SocketError => e
        ErrorHandling.handle_restclient_error(e)
    rescue NoMethodError => e
        if e.message =~ /\WRequestFailed\W/
            e = StandardError.new('Unexpected HTTP response code')
            ErrorHandling.handle_restclient_error(e)
        else
            raise
        end
    rescue RestClient::ExceptionWithResponse => e
        if rcode = e.http_code and rbody = e.http_body
            ErrorHandling.handle_api_error(rcode, rbody)
        else
            ErrorHandling.handle_restclient_error(e)
        end
    rescue RestClient::Exception, Errno::ECONNREFUSED => e
        ErrorHandling.handle_restclient_error(e)
    end

    return response
end

#all(options = {}) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/simperium.rb', line 322

def all(options={})
    defaults = {:cv=>nil, :data=>nil, :username=>false, :most_recent=>false, :timeout=>nil}
    unless options.empty?
        options = defaults.merge(options)
    else
        options = defaults
    end

    cv = options[:cv]
    data = options[:data]
    username = options[:username]
    most_recent = options[:most_recent]
    timeout = options[:timeout]

    url = "#{@appname}/#{@bucket}/all?clientid=#{@clientid}"
    unless cv.nil?
        url += "&cv=#{cv}"
    end

    if username
        url += "&username=1"
    end

    if data
        url += "&data=1"
    end
    
    if most_recent
        url += "&most_recent=1"
    end
    
    headers = _auth_header()
    
    response = self._request(url, data=nil, headers=headers, method='GET', timeout=timeout)
    return JSON.load(response.body)
end

#changes(options = {}) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/simperium.rb', line 301

def changes(options={})
    defautls = {:cv=>nil, :timeout=>nil}
    unless options.empty?
        options = defaults.merge(options)
    else
        options = defaults
    end

    cv = option[:cv]
    timeout = option[:timeout]

    url = "#{@appname}/#{@bucket}/changes?clientid=#{@clientid}"
    unless cv.nil?
        url += "&cv=#{cv}"
    end
    headers = _auth_header()
    
    response = self._request(url, data=nil, headers=headers, method='GET', timeout=timeout)
    return JSON.load(response.body)
end

#delete(item, version = nil) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/simperium.rb', line 286

def delete(item, version=nil)
    ccid = self._gen_ccid()
    url = "#{@appname}/#{@bucket}/i/{item}"
    
    if version
        url += "/v/#{version}"
    end

    url += "?clientid=#{@clientid}&ccid=#{ccid}"
    response = self._request(url, data=nil, headers=_auth_header(), method='DELETE')
    if response.body.strip.nil?
        return ccid
    end
end

#get(item, options = {}) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/simperium.rb', line 218

def get(item, options={})
    defaults = {:default=>nil, :version=>nil}
    unless options.empty?
        options = defaults.merge(options)
    else
        options = defaults
    end
    default = options[:default]
    version = options[:version]

    url = "#{@appname}/#{@bucket}/i/#{item}"
    unless version.nil?
        url += "/v/#{version}"
    end

    response = self._request(url, data=nil, headers=_auth_header(), method='GET')
    return JSON.load(response.body)
end

#index(options = {}) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/simperium.rb', line 183

def index(options={})
    defaults = {:data=>nil, :mark=>nil, :limit=>nil, :since=>nil}
    unless options.empty?
        options = defaults.merge(options)
    else
        options = defaults
    end

    data = options[:data]
    mark = options[:mark]
    limit = options[:limit]
    since = options[:since]

    url = "#{@appname}/#{@bucket}/index?"
    
    if data
        url += "&data=1"
    end

    if mark
        url += "&mark=#{mark.to_str}"
    end

    if limit
        url += "&limit=#{limit.to_s}"
    end

    if since
        url += "&since=#{since.to_str}"
    end
    
    response = self._request(url, data=nil, headers=_auth_header(), method='GET')
    return JSON.load(response.body)
end

#new(data, ccid = nil) ⇒ Object



277
278
279
280
# File 'lib/simperium.rb', line 277

def new(data, ccid=nil)
    uuid = UUID.new
    return self.post(uuid.generate(:compact), data, ccid=ccid)
end

#post(item, data, options = {}) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/simperium.rb', line 237

def post(item, data, options={})
    defaults = {:version=>nil, :ccid=>nil, :include_response=>false, :replace=>false}
    unless options.empty?
        options = defaults.merge(options)
    else
        options = defaults
    end

    version = options[:version]
    ccid = options[:ccid]
    include_response = options[:include_response]
    replace = options[:replace]

    if ccid.nil?
        ccid = self._gen_ccid()
    end
    url = "#{@appname}/#{@bucket}/i/#{item}"
    
    if version
        url += "/v/#{version}"
    end
    url += "?clientid=#{@clientid}&ccid=#{ccid}"
    
    if include_response
        url += "&response=1"
    end
    
    if replace
        url += "&replace=1"
    end
    data = JSON.dump(data)
    
    response = self._request(url, data, headers=_auth_header())
    if include_response
        return item, JSON.load(response.body)
    else
        return item
    end
end

#set(item, data, options = {}) ⇒ Object



282
283
284
# File 'lib/simperium.rb', line 282

def set(item, data, options={})
    return self.post(item, data, options)
end