Skip to content

Commit

Permalink
Merge pull request #8 from urbanairship/Rails_Fix
Browse files Browse the repository at this point in the history
Resolve rails compatibility issue, switch to helper fns.
  • Loading branch information
jkvoorhis committed Jul 29, 2015
2 parents ea35121 + c9c53b1 commit a6d2b1c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 36 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
--------------------
3.0.2
--------------------
- Resolve compatibility issues with Rails ActiveSupport



--------------------
3.0.1
--------------------
Expand Down
5 changes: 0 additions & 5 deletions lib/ext/hash.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/ext/object.rb

This file was deleted.

22 changes: 21 additions & 1 deletion lib/urbanairship/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,6 +32,26 @@ 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

class Unauthorized < StandardError
# raised when we get a 401 from server
end
Expand Down
37 changes: 19 additions & 18 deletions lib/urbanairship/push/payload.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,15 +19,15 @@ 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

# 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,
Expand All @@ -35,34 +36,34 @@ 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,
extra: extra,
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
Expand All @@ -72,31 +73,31 @@ 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

# 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,
Expand All @@ -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
Expand 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
Expand Down
12 changes: 6 additions & 6 deletions lib/urbanairship/push/push.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'json'

require 'ext/object'
require 'urbanairship/common'
require 'urbanairship/loggable'

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/urbanairship/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Urbanairship
VERSION = '3.0.1'
VERSION = '3.0.2'
end

0 comments on commit a6d2b1c

Please sign in to comment.