From 6ca85d54509608b525f9649068ac7661045598c7 Mon Sep 17 00:00:00 2001 From: jkvoorhis Date: Wed, 29 Jul 2015 11:21:33 -0700 Subject: [PATCH 1/2] resolve rails compatability issue, switch to helper fns --- lib/ext/hash.rb | 5 ----- lib/ext/object.rb | 5 ----- lib/urbanairship/common.rb | 10 +++++++++ lib/urbanairship/push/payload.rb | 37 ++++++++++++++++---------------- lib/urbanairship/push/push.rb | 12 +++++------ lib/urbanairship/version.rb | 2 +- 6 files changed, 36 insertions(+), 35 deletions(-) delete mode 100644 lib/ext/hash.rb delete mode 100644 lib/ext/object.rb diff --git a/lib/ext/hash.rb b/lib/ext/hash.rb deleted file mode 100644 index 25eb9dac..00000000 --- a/lib/ext/hash.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Hash - def compact - keep_if { |_, value| !value.nil? } - end -end diff --git a/lib/ext/object.rb b/lib/ext/object.rb deleted file mode 100644 index abb242a1..00000000 --- a/lib/ext/object.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Object - def try(method) - send method if respond_to? method - end -end diff --git a/lib/urbanairship/common.rb b/lib/urbanairship/common.rb index 05dd3e7a..1eebd0ca 100644 --- a/lib/urbanairship/common.rb +++ b/lib/urbanairship/common.rb @@ -32,6 +32,16 @@ def required(arg=nil) raise ArgumentError.new("required parameter #{arg.to_sym.inspect + ' ' if arg}not passed to method #{method}") end + def try_helper(method, obj) + if obj.respond_to?(method) + obj.send(method) + end + end + + def compact_helper(a_hash) + a_hash.keep_if {|_, value| !value.nil?} + end + class Unauthorized < StandardError # raised when we get a 401 from server end diff --git a/lib/urbanairship/push/payload.rb b/lib/urbanairship/push/payload.rb index 84fe8a03..7fbf9eec 100644 --- a/lib/urbanairship/push/payload.rb +++ b/lib/urbanairship/push/payload.rb @@ -1,14 +1,15 @@ module Urbanairship module Push module Payload - require 'ext/hash' + require 'urbanairship/common' + include Urbanairship::Common # Notification Object for a Push Payload def notification(alert: nil, ios: nil, android: nil, amazon: nil, blackberry: nil, wns: nil, mpns: nil, actions: nil, interactive: nil) - payload = { + payload = compact_helper({ alert: alert, actions: actions, ios: ios, @@ -18,7 +19,7 @@ def notification(alert: nil, ios: nil, android: nil, amazon: nil, wns: wns, mpns: mpns, interactive: interactive - }.compact + }) fail ArgumentError, 'Notification body is empty' if payload.empty? payload end @@ -26,7 +27,7 @@ def notification(alert: nil, ios: nil, android: nil, amazon: nil, # iOS specific portion of Push Notification Object def ios(alert: nil, badge: nil, sound: nil, extra: nil, expiry: nil, category: nil, interactive: nil, content_available: nil) - { + compact_helper({ alert: alert, badge: badge, sound: sound, @@ -35,13 +36,13 @@ def ios(alert: nil, badge: nil, sound: nil, extra: nil, expiry: nil, category: category, interactive: interactive, 'content-available' => content_available - }.compact + }) end # Amazon specific portion of Push Notification Object def amazon(alert: nil, consolidation_key: nil, expires_after: nil, extra: nil, title: nil, summary: nil, interactive: nil) - { + compact_helper({ alert: alert, consolidation_key: consolidation_key, expires_after: expires_after, @@ -49,20 +50,20 @@ def amazon(alert: nil, consolidation_key: nil, expires_after: nil, title: title, summary: summary, interactive: interactive - }.compact + }) end # Android specific portion of Push Notification Object def android(alert: nil, collapse_key: nil, time_to_live: nil, extra: nil, delay_while_idle: nil, interactive: nil) - { + compact_helper({ alert: alert, collapse_key: collapse_key, time_to_live: time_to_live, extra: extra, delay_while_idle: delay_while_idle, interactive: interactive - }.compact + }) end # BlackBerry specific portion of Push Notification Object @@ -72,23 +73,23 @@ def blackberry(alert: nil, body: nil, content_type: 'text/plain') # WNS specific portion of Push Notification Object def wns_payload(alert: nil, toast: nil, tile: nil, badge: nil) - payload = { + payload = compact_helper({ alert: alert, toast: toast, tile: tile, badge: badge - }.compact + }) fail ArgumentError, 'Must specify one message type' if payload.size != 1 payload end # MPNS specific portion of Push Notification Object def mpns_payload(alert: nil, toast: nil, tile: nil) - payload = { + payload = compact_helper({ alert: alert, toast: toast, tile: tile - }.compact + }) fail ArgumentError, 'Must specify one message type' if payload.size != 1 payload end @@ -96,7 +97,7 @@ def mpns_payload(alert: nil, toast: nil, tile: nil) # Rich Message specific portion of Push Notification Object def message(title: required('title'), body: required('body'), content_type: nil, content_encoding: nil, extra: nil, expiry: nil, icons: nil, options: nil) - { + compact_helper({ title: title, body: body, content_type: content_type, @@ -105,13 +106,13 @@ def message(title: required('title'), body: required('body'), content_type: nil, expiry: expiry, icons: icons, options: options - }.compact + }) end # Interactive Notification portion of Push Notification Object def interactive(type: required('type'), button_actions: nil) fail ArgumentError, 'type must not be nil' if type.nil? - { type: type, button_actions: button_actions }.compact + compact_helper({ type: type, button_actions: button_actions }) end def all @@ -131,13 +132,13 @@ def options(expiry: required('expiry')) # Actions for a Push Notification Object def actions(add_tag: nil, remove_tag: nil, open_: nil, share: nil, app_defined: nil) - { + compact_helper({ add_tag: add_tag, remove_tag: remove_tag, open: open_, share: share, app_defined: app_defined - }.compact + }) end end end diff --git a/lib/urbanairship/push/push.rb b/lib/urbanairship/push/push.rb index abeb346d..e6a4fdba 100644 --- a/lib/urbanairship/push/push.rb +++ b/lib/urbanairship/push/push.rb @@ -1,6 +1,5 @@ require 'json' -require 'ext/object' require 'urbanairship/common' require 'urbanairship/loggable' @@ -23,13 +22,13 @@ def initialize(client) end def payload - { + compact_helper({ audience: @audience, notification: @notification, options: @options, device_types: @device_types, message: @message - }.compact + }) end # Send the Push Object @@ -67,11 +66,11 @@ def initialize(client) end def payload - { + compact_helper({ name: @name, schedule: @schedule, push: @push.payload - }.compact + }) end # Schedule the Push Notification @@ -165,12 +164,13 @@ def update # Response to a successful push notification send or schedule. class PushResponse attr_reader :ok, :push_ids, :schedule_url, :operation_id, :payload, :status_code + include Urbanairship::Common def initialize(http_response_body: nil, http_response_code: nil) @payload = http_response_body || "No Content" @ok = @payload['ok'] || "None" @push_ids = @payload['push_ids'] || "None" - @schedule_url = @payload['schedule_urls'].try(:first) || "None" + @schedule_url = try_helper(:first, @payload['schedule_urls']) || "None" @operation_id = @payload['operation_id'] || "None" @status_code = http_response_code end diff --git a/lib/urbanairship/version.rb b/lib/urbanairship/version.rb index 6cafcb56..896de2dd 100644 --- a/lib/urbanairship/version.rb +++ b/lib/urbanairship/version.rb @@ -1,3 +1,3 @@ module Urbanairship - VERSION = '3.0.1' + VERSION = '3.0.2' end From c9c53b151f7c7548aca09d9830f51f02217e60b7 Mon Sep 17 00:00:00 2001 From: jkvoorhis Date: Wed, 29 Jul 2015 13:51:29 -0700 Subject: [PATCH 2/2] update changelog, add code comments --- CHANGELOG | 7 +++++++ lib/urbanairship/common.rb | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index ceb0b122..2a402998 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +-------------------- +3.0.2 +-------------------- +- Resolve compatibility issues with Rails ActiveSupport + + + -------------------- 3.0.1 -------------------- diff --git a/lib/urbanairship/common.rb b/lib/urbanairship/common.rb index 1eebd0ca..14ece47f 100644 --- a/lib/urbanairship/common.rb +++ b/lib/urbanairship/common.rb @@ -16,7 +16,7 @@ module Common TAGS_URL = BASE_URL + '/tags/' SEGMENTS_URL = BASE_URL + '/segments/' - # Helper method for required keyword args in 2.0 that is compatible with 2.1+ + # Helper method for required keyword args in Ruby 2.0 that is compatible with 2.1+ # @example # def say(greeting: required('greeting')) # puts greeting @@ -32,12 +32,22 @@ def required(arg=nil) raise ArgumentError.new("required parameter #{arg.to_sym.inspect + ' ' if arg}not passed to method #{method}") end + # Helper method that sends the indicated method to the indicated object, if the object responds to the method + # @example + # try_helper(:first, [1,2,3]) + # + # >> 1 def try_helper(method, obj) if obj.respond_to?(method) obj.send(method) end end + # Helper method that deletes every key-value pair from a hash for which the value is nil + # @example + # compact_helper({"a" => 1, "b" => nil}) + # + # >> {"a" => 1} def compact_helper(a_hash) a_hash.keep_if {|_, value| !value.nil?} end