Sha256: 62bcc8953b4799ac2aa0ab4ce38bc5704deec848d9c5f244e67216f567245269

Contents?: true

Size: 836 Bytes

Versions: 1

Compression:

Stored size: 836 Bytes

Contents

require 'json'

module Houston
  class Notification
    attr_accessor :device, :alert, :badge, :sound, :custom_data
    attr_reader :sent_at

    def initialize(options = {})
      @device = options.delete(:device)
      @alert = options.delete(:alert)
      @badge = options.delete(:badge)
      @sound = options.delete(:sound)

      @custom_data = options
    end

    def payload
      json = {}.merge(@custom_data || {})
      json['aps'] = {}
      json['aps']['alert'] = @alert
      json['aps']['badge'] = @badge.to_i rescue 0
      json['aps']['sound'] = @sound

      json
    end

    def message
      json = payload.to_json

      "\0\0 #{[@device.gsub(/[<\s>]/, '')].pack('H*')}\0#{json.length.chr}#{json}"
    end

    def mark_as_sent!
      @sent_at = Time.now
    end

    def sent?
      !!@sent_at
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
houston-0.1.0 ./lib/houston/notification.rb