From f01d2e235a2212afd7fbe7cbc0b080b31d976670 Mon Sep 17 00:00:00 2001 From: Michael Saffitz Date: Fri, 25 Oct 2013 09:31:41 -0700 Subject: [PATCH 1/2] added device_info endpoint, deprecation warnings for endpoints removed in v3 of the API, and the option to use v3 endpoint for push --- README.markdown | 12 +++++++ lib/urbanairship.rb | 15 +++++++- spec/urbanairship_spec.rb | 72 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 5154087a..c0501f1e 100644 --- a/README.markdown +++ b/README.markdown @@ -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 @@ -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 diff --git a/lib/urbanairship.rb b/lib/urbanairship.rb index cf68759e..5fe94199 100644 --- a/lib/urbanairship.rb +++ b/lib/urbanairship.rb @@ -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) + 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 @@ -124,6 +136,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 diff --git a/spec/urbanairship_spec.rb b/spec/urbanairship_spec.rb index e0c3f9b3..9b4ee857 100644 --- a/spec/urbanairship_spec.rb +++ b/spec/urbanairship_spec.rb @@ -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"]) @@ -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" @@ -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])) From 422ab953069a566b22ef6c3f5d3b3effd6f38d68 Mon Sep 17 00:00:00 2001 From: Michael Saffitz Date: Fri, 25 Oct 2013 14:16:45 -0700 Subject: [PATCH 2/2] don't send version to urban airship for push --- lib/urbanairship.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/urbanairship.rb b/lib/urbanairship.rb index 5fe94199..2d83690b 100644 --- a/lib/urbanairship.rb +++ b/lib/urbanairship.rb @@ -49,7 +49,7 @@ def delete_scheduled_push(param) end def push(options = {}) - body = parse_push_options(options).to_json + body = parse_push_options(options.dup).to_json do_request(:post, "/api/push/", :body => body, :authenticate_with => :master_secret, :version => options[:version]) end @@ -168,6 +168,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