diff --git a/lib/zeppelin.rb b/lib/zeppelin.rb index b19d0ed..e42925d 100644 --- a/lib/zeppelin.rb +++ b/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' \ No newline at end of file +require 'zeppelin/version' diff --git a/lib/zeppelin/version.rb b/lib/zeppelin/version.rb index 5eae141..aa939b3 100644 --- a/lib/zeppelin/version.rb +++ b/lib/zeppelin/version.rb @@ -1,3 +1,3 @@ class Zeppelin - VERSION = '0.2.0' + VERSION = '0.3.0' end \ No newline at end of file diff --git a/test/zeppelin_test.rb b/test/zeppelin_test.rb index 9ad1461..b301da3 100644 --- a/test/zeppelin_test.rb +++ b/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 \ No newline at end of file +end