./lib/helios/backend/push-notification.rb in helios-0.2.1 vs ./lib/helios/backend/push-notification.rb in helios-0.2.2

- old
+ new

@@ -1,15 +1,27 @@ require 'rack/push-notification' + +require 'sinatra/base' require 'sinatra/param' +require 'houston' + class Helios::Backend::PushNotification < Sinatra::Base - helpers Sinatra::Param + helpers Sinatra::Param + attr_reader :apn_certificate, :apn_environment - def initialize(app, options = {}) + def initialize(app, options = {}, &block) super(Rack::PushNotification.new) + + @apn_certificate = options[:apn_certificate] || ENV['APN_CERTIFICATE'] + @apn_environment = options[:apn_environment] || ENV['APN_ENVIRONMENT'] end + before do + content_type :json + end + get '/devices/?' do param :q, String devices = ::Rack::PushNotification::Device.dataset devices = devices.filter("tsv @@ to_tsquery('english', ?)", "#{params[:q]}:*") if params[:q] and not params[:q].empty? @@ -77,19 +89,20 @@ private def client begin - return nil unless settings.apn_certificate and ::File.exist?(settings.apn_certificate) - - client = case settings.apn_environment.to_sym - when :development - Houston::Client.development - when :production - Houston::Client.production - end - client.certificate = ::File.read(settings.apn_certificate) - + return nil unless apn_certificate and ::File.exist?(apn_certificate) + + client = case apn_environment.to_sym + when :development + Houston::Client.development + when :production + Houston::Client.production + end + + client.certificate = ::File.read(apn_certificate) + return client rescue return nil end end