Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Kern committed Aug 17, 2011
2 parents a6e58a0 + cc78efd commit f881d0c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
53 changes: 52 additions & 1 deletion lib/zeppelin.rb
Expand Up @@ -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

Expand All @@ -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'
2 changes: 1 addition & 1 deletion lib/zeppelin/version.rb
@@ -1,3 +1,3 @@
class Zeppelin
VERSION = '0.2.0'
VERSION = '0.3.0'
end
83 changes: 82 additions & 1 deletion test/zeppelin_test.rb
Expand Up @@ -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

0 comments on commit f881d0c

Please sign in to comment.