Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'develop'

  • Loading branch information...
commit f881d0c6c8c49a520305782fafa666692f48b2ab 2 parents a6e58a0 + cc78efd
Alexander Kern authored
Showing with 135 additions and 3 deletions.
  1. +52 −1 lib/zeppelin.rb
  2. +1 −1  lib/zeppelin/version.rb
  3. +82 −1 test/zeppelin_test.rb
View
53 lib/zeppelin.rb
@@ -139,6 +139,49 @@ def feedback(since)
response = @connection.get(feedback_uri(since))
successful?(response) ? Yajl::Parser.parse(response.body) : nil
end
+
+ # Creates a tag that is not associated with any device
+ #
+ # @param [#to_s] name The name of the tag to add
+ #
+ # @return [Boolean] whether or not the request was successful
+ def add_tag(name)
+ response = @connection.put(tag_uri(name))
+ successful?(response)
+ end
+
+ # Removes a tag from the service
+ #
+ # @param [#to_s] name The name of the tag to remove
+ #
+ # @return [Boolean] true when the request was successful. Note that this
+ # method will return false if the tag has already been removed.
+ def remove_tag(name)
+ response = @connection.delete(tag_uri(name))
+ successful?(response)
+ end
+
+ # @param [String] device_token
+ #
+ # @param [#to_s] tag_name
+ #
+ # @return [Boolean] whether or not a tag was successfully associated with
+ # a device
+ def add_tag_to_device(device_token, tag_name)
+ response = @connection.put(device_tag_uri(device_token, tag_name))
+ successful?(response)
+ end
+
+ # @param [String] device_token
+ #
+ # @param [#to_s] tag_name
+ #
+ # @return [Boolean] whether or not a tag was successfully dissociated from
+ # a device
+ def remove_tag_from_device(device_token, tag_name)
+ response = @connection.delete(device_tag_uri(device_token, tag_name))
+ successful?(response)
+ end
private
@@ -153,10 +196,18 @@ def apid_uri(apid)
def feedback_uri(since)
"/api/device_tokens/feedback/?since=#{since.utc.iso8601}"
end
+
+ def tag_uri(name)
+ "/api/tags/#{name}"
+ end
+
+ def device_tag_uri(device_token, tag_name)
+ device_token_uri(device_token) + "/tags/#{tag_name}"
+ end
def successful?(response)
SUCCESSFUL_STATUS_CODES.include?(response.status)
end
end
-require 'zeppelin/version'
+require 'zeppelin/version'
View
2  lib/zeppelin/version.rb
@@ -1,3 +1,3 @@
class Zeppelin
- VERSION = '0.2.0'
+ VERSION = '0.3.0'
end
View
83 test/zeppelin_test.rb
@@ -303,9 +303,90 @@ def setup
response = @client.feedback(since)
assert_nil response
end
+
+ test '#add_tag' do
+ tag_name = 'chunky.bacon'
+
+ stub_requests @client.connection do |stub|
+ stub.put("/api/tags/#{tag_name}") do
+ [201, {}, '']
+ end
+ end
+
+ response = @client.add_tag(tag_name)
+ assert response
+ end
+
+ test '#remove_tag with an existing tag' do
+ tag_name = 'cats.pajamas'
+
+ stub_requests @client.connection do |stub|
+ stub.delete("/api/tags/#{tag_name}") do
+ [204, {}, '']
+ end
+ end
+
+ response = @client.remove_tag(tag_name)
+ assert response
+ end
+
+ test '#remove_tag with non-existant tag' do
+ tag_name = 'cats.pajamas'
+
+ stub_requests @client.connection do |stub|
+ stub.delete("/api/tags/#{tag_name}") do
+ [404, {}, 'Not Found']
+ end
+ end
+
+ response = @client.remove_tag(tag_name)
+ refute response
+ end
+
+ test '#add_tag_to_device' do
+ tag_name = 'radio.head'
+ device_token = 'CAFEBABE'
+
+ stub_requests @client.connection do |stub|
+ stub.put("/api/device_tokens/#{device_token}/tags/#{tag_name}") do
+ [201, {}, 'Created']
+ end
+ end
+
+ response = @client.add_tag_to_device(device_token, tag_name)
+ assert response
+ end
+
+ test '#remove_tag_from_device successfully' do
+ tag_name = 'martin.fowler'
+ device_token = 'DEADBEEF'
+
+ stub_requests @client.connection do |stub|
+ stub.delete("/api/device_tokens/#{device_token}/tags/#{tag_name}") do
+ [204, {}, 'No Content']
+ end
+ end
+
+ response = @client.remove_tag_from_device(device_token, tag_name)
+ assert response
+ end
+
+ test '#remove_tag_from_device unsuccessfully' do
+ tag_name = 'martin.fowler'
+ device_token = 'DEADBEEF'
+
+ stub_requests @client.connection do |stub|
+ stub.delete("/api/device_tokens/#{device_token}/tags/#{tag_name}") do
+ [404, {}, 'Not Found']
+ end
+ end
+
+ response = @client.remove_tag_from_device(device_token, tag_name)
+ refute response
+ end
def stub_requests(connection, &block)
connection.builder.handlers.delete(Faraday::Adapter::NetHttp)
connection.adapter(:test, &block)
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.