Skip to content

Instantly share code, notes, and snippets.

@jakecraige
Created August 16, 2013 05:26
Show Gist options
  • Save jakecraige/6247525 to your computer and use it in GitHub Desktop.
Save jakecraige/6247525 to your computer and use it in GitHub Desktop.
class Airship
# name and alert key
SupportedDevices = { ios: "aps", android: "android" }
def initialize(application_key: 'application-key',
application_secret: 'application-secret',
mester_secret: 'master-secret',
logger: Rails.logger,
request_timeout: 5)
@devices = base_devices_hash
end
# Pass in a hand made hash, one returned from the create_alert method or just
# pass in the alert and tags and it'll send out the push notification
def push(alert, tags = [])
tags = tags.to_a
if alert.instance_of?(Hash)
if alert.has_key?(:tags)
push_hash_with_tags(alert)
else
push_hash_to_all(alert)
end
else
add_to_devices(:alert, alert)
if tags.present?
# TODO: Validation tags is array?
push_with_tags(tags)
else
push_to_all
end
end
end
def create_alert(alert, tags = [])
devices = base_devices_hash
devices = add_to_devices(:alert, alert, devices)
devices = add_to_devices(:tags, tags, devices) if tags.present?
devices
end
def push_with_tags tags
add_to_devices(:tags, tags)
puts "pushing tags for @devices"
@devices
# TODO: broadcast @devices hash to @airship
end
def push_hash_with_tags alert_hash
puts "sending off hash that has tags"
alert_hash
# TODO: broadcast alert hash to @airship
end
def push_to_all
puts "pushing @devices to all"
@devices
# TODO: push @devices hash to @airship
end
def push_hash_to_all alert_hash
puts "pushing hash to all"
return alert_hash
# TODO: push alert hash to @airship
end
private
# Pass in a key value pair to be added to the instance device or the hash you
# pass in and have it returned to you.
def add_to_devices(key, value, devices = @devices)
if key == :alert
devices.each do |device, alert_name|
devices[device].fetch(alert_name.keys.first).merge!({ :"#{key}" => value } )
end
else
devices.each { |device, _| devices[device].merge!({ :"#{key}" => value } ) }
end
end
# creates basic hash from supported devices constant
def base_devices_hash
SupportedDevices.inject({}) do |devices, (device, attribute)|
devices.merge!({ :"#{device}" => { :"#{attribute}" => {} } })
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment