lib/zeppelin.rb in zeppelin-0.6.0 vs lib/zeppelin.rb in zeppelin-0.7.0
- old
+ new
@@ -65,10 +65,25 @@
def delete_device_token(device_token)
uri = device_token_uri(device_token)
delete_request(uri)
end
+ # Retrieve a page of device tokens
+ #
+ # @param [Integer] page (nil) Page of device tokens to retrieve
+ #
+ # @return [Hash] result set. See documentation for details
+ #
+ # @Note that the next page number is included in the result set instead of the
+ # raw URI to request for the next page
+ #
+ # @raise [Zeppelin::ClientError] invalid request
+ def device_tokens(page=nil)
+ uri = device_token_uri(nil, :page => page)
+ get_paged_request(uri)
+ end
+
# Registers an APID.
#
# @param [String] apid
#
# @param [Hash] payload the payload to send during registration
@@ -103,10 +118,25 @@
def delete_apid(apid)
uri = apid_uri(apid)
delete_request(uri)
end
+ # Retrieve a page of APIDs
+ #
+ # @param [Integer] page (nil) Page of APIDs to retrieve
+ #
+ # @return [Hash] result set. See documentation for details
+ #
+ # @Note that the next page number is included in the result set instead of the
+ # raw URI to request for the next page
+ #
+ # @raise [Zeppelin::ClientError] invalid request
+ def apids(page=nil)
+ uri = apid_uri(nil, :page => page)
+ get_paged_request(uri)
+ end
+
# Pushes a message.
#
# @param [Hash] payload the payload of the message
#
# @return [Boolean] whether or not pushing the message was successful
@@ -250,11 +280,11 @@
conn
end
def put_request(uri, payload={})
if !(payload.nil? || payload.empty?)
- response =connection.put(uri, payload, JSON_HEADERS)
+ response = connection.put(uri, payload, JSON_HEADERS)
else
response = connection.put(uri)
end
response.success?
@@ -267,19 +297,36 @@
def get_request(uri)
response = connection.get(uri)
response.body if response.success?
end
+ def get_paged_request(uri)
+ results = get_request(uri)
+ md = results['next_page'] && results['next_page'].match(/(start|page)=(\d+)/)
+
+ results['next_page'] = md[2].to_i unless md.nil?
+
+ results
+ end
+
def post_request(uri, payload)
connection.post(uri, payload, JSON_HEADERS).success?
end
- def device_token_uri(device_token)
- "/api/device_tokens/#{device_token}"
+ def query_string(query)
+ '?' + query.map { |k, v| "#{k}=#{v}" }.join('&')
end
- def apid_uri(apid)
- "/api/apids/#{apid}"
+ def device_token_uri(device_token, query={})
+ uri = "/api/device_tokens/#{device_token}"
+ uri << query_string(query) unless query.empty?
+ uri
+ end
+
+ def apid_uri(apid, query={})
+ uri = "/api/apids/#{apid}"
+ uri << query_string(query) unless query.empty?
+ uri
end
def feedback_uri(since)
"/api/device_tokens/feedback/?since=#{since.utc.iso8601}"
end