From c2e1efd003f90566e563201614d7505ca165b906 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 16 Aug 2011 17:08:17 -0400 Subject: [PATCH 1/5] Can add a device-less tag --- lib/zeppelin.rb | 16 +++++++++++++++- test/zeppelin_test.rb | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/zeppelin.rb b/lib/zeppelin.rb index b19d0ed..be7e227 100644 --- a/lib/zeppelin.rb +++ b/lib/zeppelin.rb @@ -139,6 +139,16 @@ 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 private @@ -153,10 +163,14 @@ 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 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/test/zeppelin_test.rb b/test/zeppelin_test.rb index 9ad1461..344f449 100644 --- a/test/zeppelin_test.rb +++ b/test/zeppelin_test.rb @@ -303,9 +303,22 @@ 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 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 From 636c8e94febaf21652eb7aa3b5ac744d7d267291 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 16 Aug 2011 17:15:01 -0400 Subject: [PATCH 2/5] Can remove a tag from the service --- lib/zeppelin.rb | 11 +++++++++++ test/zeppelin_test.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/zeppelin.rb b/lib/zeppelin.rb index be7e227..fa6b437 100644 --- a/lib/zeppelin.rb +++ b/lib/zeppelin.rb @@ -149,6 +149,17 @@ 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 private diff --git a/test/zeppelin_test.rb b/test/zeppelin_test.rb index 344f449..7787c90 100644 --- a/test/zeppelin_test.rb +++ b/test/zeppelin_test.rb @@ -316,6 +316,32 @@ def setup 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 def stub_requests(connection, &block) connection.builder.handlers.delete(Faraday::Adapter::NetHttp) From 0ea18af5291aaa7b02ebc18aad682d2f1b12a11e Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 16 Aug 2011 17:56:23 -0400 Subject: [PATCH 3/5] Can associate a Device with a Tag --- lib/zeppelin.rb | 15 +++++++++++++++ test/zeppelin_test.rb | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/zeppelin.rb b/lib/zeppelin.rb index fa6b437..bbea919 100644 --- a/lib/zeppelin.rb +++ b/lib/zeppelin.rb @@ -160,6 +160,17 @@ 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 private @@ -178,6 +189,10 @@ def feedback_uri(since) 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) diff --git a/test/zeppelin_test.rb b/test/zeppelin_test.rb index 7787c90..9584e9a 100644 --- a/test/zeppelin_test.rb +++ b/test/zeppelin_test.rb @@ -342,6 +342,20 @@ def setup 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 def stub_requests(connection, &block) connection.builder.handlers.delete(Faraday::Adapter::NetHttp) From bade7649a90e39b9784fdbbebe770b10cf440d83 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 16 Aug 2011 18:00:19 -0400 Subject: [PATCH 4/5] Can disassociate Device from Tag --- lib/zeppelin.rb | 11 +++++++++++ test/zeppelin_test.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/zeppelin.rb b/lib/zeppelin.rb index bbea919..e42925d 100644 --- a/lib/zeppelin.rb +++ b/lib/zeppelin.rb @@ -171,6 +171,17 @@ 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 diff --git a/test/zeppelin_test.rb b/test/zeppelin_test.rb index 9584e9a..b301da3 100644 --- a/test/zeppelin_test.rb +++ b/test/zeppelin_test.rb @@ -356,6 +356,34 @@ def setup 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) From cc78efdde5fcfa462a32ecd87ecc252aaace2e52 Mon Sep 17 00:00:00 2001 From: Alexander Kern Date: Tue, 16 Aug 2011 19:37:40 -0700 Subject: [PATCH 5/5] Bump version. --- lib/zeppelin/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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