Skip to content

Commit

Permalink
Merge pull request urbanairship#43 from apptentive-engineering/master
Browse files Browse the repository at this point in the history
Device Info Endpoint, V3 Support
  • Loading branch information
michaelphines committed Apr 24, 2014
2 parents c5f8e78 + 422ab95 commit 8c0ec32
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.markdown
Expand Up @@ -38,6 +38,12 @@ Unregistering a device token
Urbanairship.unregister_device('DEVICE-TOKEN')
```

Retrieving Device Info
----------------------------
```ruby
Urbanairship.device_info('DEVICE-TOKEN')
```

Sending a push notification
---------------------------
```ruby
Expand All @@ -52,6 +58,12 @@ Urbanairship.push(notification) # =>
# "scheduled_notifications" => ["https://go.urbanairship.com/api/push/scheduled/123456"]
# }
```

If you wish to use v3 of the Urbanairship API, just add `version: 3` as an option:

```ruby
Urbanairship.push(notification, version: 3)
```
### Using aliases instead of device tokens ###

```ruby
Expand Down
18 changes: 16 additions & 2 deletions lib/urbanairship.rb
Expand Up @@ -34,27 +34,39 @@ def unregister_device(device_token, options = {})
end
end

def device_info(device_token, options = {})
if ( (options[:provider] || @provider) == :android ) || ( (options[:provider] || @provider) == 'android' )
do_request(:get, "/api/apids/#{device_token}", :authenticate_with => :application_secret)
else
do_request(:get, "/api/device_tokens/#{device_token}", :authenticate_with => :application_secret)
end
end

def delete_scheduled_push(param)
warn "[DEPRECATED] http://docs.urbanairship.com/reference/api/v3/api-v3-migration-guide.html#api-push-batch"
path = param.is_a?(Hash) ? "/api/push/scheduled/alias/#{param[:alias].to_s}" : "/api/push/scheduled/#{param.to_s}"
do_request(:delete, path, :authenticate_with => :master_secret)
end

def push(options = {})
body = parse_push_options(options).to_json
do_request(:post, "/api/push/", :body => body, :authenticate_with => :master_secret)
body = parse_push_options(options.dup).to_json
do_request(:post, "/api/push/", :body => body, :authenticate_with => :master_secret, :version => options[:version])
end

def push_to_segment(options = {})
warn "[DEPRECATED] http://docs.urbanairship.com/reference/api/v3/api-v3-migration-guide.html#api-push-segments"
body = parse_push_options(options).to_json
do_request(:post, "/api/push/segments", :body => body, :authenticate_with => :master_secret)
end

def batch_push(notifications = [])
warn "[DEPRECATION] http://docs.urbanairship.com/reference/api/v3/api-v3-migration-guide.html#api-push-batch"
body = notifications.map{|notification| parse_push_options(notification)}.to_json
do_request(:post, "/api/push/batch/", :body => body, :authenticate_with => :master_secret)
end

def broadcast_push(options = {})
warn "[DEPRECATED] http://docs.urbanairship.com/reference/api/v3/api-v3-migration-guide.html#api-push-broadcast"
body = parse_push_options(options).to_json
do_request(:post, "/api/push/broadcast/", :body => body, :authenticate_with => :master_secret)
end
Expand Down Expand Up @@ -128,6 +140,7 @@ def do_request(http_method, path, options = {})
request.basic_auth @application_key, instance_variable_get("@#{options[:authenticate_with]}")
request.add_field "Content-Type", options[:content_type] || "application/json"
request.body = options[:body] if options[:body]
request["Accept"] = "application/vnd.urbanairship+json; version=#{options[:version]};" if options[:version]

Timer.timeout(request_timeout) do
start_time = Time.now
Expand Down Expand Up @@ -159,6 +172,7 @@ def parse_register_options(hash = {})
def parse_push_options(hash = {})
hash[:aliases] = hash[:aliases].map{|a| a.to_s} unless hash[:aliases].nil?
hash[:schedule_for] = hash[:schedule_for].map{|elem| process_scheduled_elem(elem)} unless hash[:schedule_for].nil?
hash.delete(:version)
hash
end

Expand Down
72 changes: 72 additions & 0 deletions spec/urbanairship_spec.rb
Expand Up @@ -16,6 +16,11 @@
FakeWeb.register_uri(:delete, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["204", "No Content"])
FakeWeb.register_uri(:delete, /bad_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["401", "Unauthorized"])

# device_info
FakeWeb.register_uri(:get, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/apids\/.+/, :status => ["200", "OK"], :body => "{\"active\":true,\"alias\":null}")
FakeWeb.register_uri(:get, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["200", "OK"], :body => "{\"active\":true,\"alias\":null}")
FakeWeb.register_uri(:get, /bad_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["401", "Unauthorized"])

# push
FakeWeb.register_uri(:post, "https://my_app_key:my_master_secret@go.urbanairship.com/api/push/", :status => ["200", "OK"])
FakeWeb.register_uri(:post, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/push/", :status => ["400", "Bad Request"])
Expand Down Expand Up @@ -485,6 +490,68 @@

end

describe "::device_info" do
before(:each) do
@valid_params = {:alias => 'one'}
subject.application_key = "my_app_key"
subject.application_secret = "my_app_secret"
end

it "raises an error if call is made without an app key and secret configured" do
subject.application_key = nil
subject.application_secret = nil

lambda {
subject.device_info("asdf1234")
}.should raise_error(RuntimeError, "Must configure application_key, application_secret before making this request.")
end

it "uses app key and secret to sign the request" do
subject.device_info("device_token")
FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_app_secret').chomp}"
end

it "takes and sends a device token" do
subject.device_info("device_token")
FakeWeb.last_request.path.should == "/api/device_tokens/device_token"
end

it "returns false when the authorization is invalid" do
subject.application_key = "bad_key"
subject.device_info("device_token").success?.should == false
end

it "uses the iOS interface by default" do
subject.device_info("device_token")
FakeWeb.last_request.path.should == "/api/device_tokens/device_token"
end

it "uses the android interface if 'provider' configuration option is set to :android Symbol" do
subject.provider = :android
subject.device_info("device_token")
FakeWeb.last_request.path.should == "/api/apids/device_token"
subject.provider = nil
end

it "uses the android interface if 'provider' configuration option is set to 'android' String" do
subject.provider = 'android'
subject.device_info("device_token")
FakeWeb.last_request.path.should == "/api/apids/device_token"
subject.provider = nil
end

it "uses the android interface if :provider Symbol key is passed an :android Symbol value" do
subject.device_info("device_token", :provider => :android)
FakeWeb.last_request.path.should == "/api/apids/device_token"
end

it "uses the android interface if 'provider' Symbol key is passed an 'android' String value" do
subject.device_info("device_token", :provider => "android")
FakeWeb.last_request.path.should == "/api/apids/device_token"
end

end

describe "::delete_scheduled_push" do
before(:each) do
subject.application_key = "my_app_key"
Expand Down Expand Up @@ -561,6 +628,11 @@
subject.push(@valid_params).success?.should == false
end

it "uses v3 of the API when requested" do
subject.push(@valid_params.merge(:version => 3)).success?.should == true
FakeWeb.last_request["Accept"].should == "application/vnd.urbanairship+json; version=3;"
end

it "adds schedule_for to the JSON payload" do
time = Time.parse("Oct 17th, 2010, 8:00 PM UTC")
subject.push(@valid_params.merge(:schedule_for => [time]))
Expand Down

0 comments on commit 8c0ec32

Please sign in to comment.