# Prowler is a plugin for integrating apps with the Prowl iPhone application. # # === Installation # # From your project's RAILS_ROOT, run: # # script/plugin install git://github.com/pixeltrix/prowler.git # # === Configuration # # You should have something like this in config/initializers/prowler.rb. # # Prowler.configure do |config| # config.username = 'username' # config.password = 'password' # config.application = 'www.example.com' # end # # You can test that Prowler is working in your production environment by using # this rake task (from RAILS_ROOT): # # rake prowler:test # # If everything is configured properly the task will send a request to # prowl.weks.net which will be appear on your iPhone after a short delay. # # === Usage # # To use Prowler within your application just call the notify method, e.g. # # Prowler.notify "Event", "Description" # # === About # # Prowler relies upon the Prowl iPhone application which is advertised as # a Growl notification forwarder from your Mac. However they provide an API # which can be called by a generic script which allows you to use the # application as a general push notification application for your iPhone. # # For more about the Prowl application see: http://prowl.weks.net/ require 'logger' require 'net/http' require 'net/https' module Prowler class << self attr_accessor :host, :port, :secure attr_accessor :username, :password attr_accessor :application, :send_notifications # The host to connect to. def host @host ||= 'prowl.weks.net' end # The port on which the service runs. def port @port || (secure ? 443 : 80) end # Whether the service is running over SSL. def secure @secure.nil? ? true : !!@secure end # Call this method to configure your account details in an initializer. def configure yield self end # Whether to send notifications def send_notifications @send_notifications.nil? ? true : !!@send_notifications end alias :send_notifications? :send_notifications # Reset configuration def reset_configuration @host = @port = @secure = @application = @username = @password = nil end # Whether the library has been configured def configured? !(@application.nil? || @username.nil? || @password.nil?) end def path(*params) #:nodoc: sprintf("/api/add_notification.php?application=%s&event=%s&description=%s", *params) end # Returns the default logger or a logger that prints to STDOUT. def logger ActiveRecord::Base.logger rescue @logger ||= Logger.new(STDERR) end # Send a notification to your iPhone: # * event: The title of notification you want to send. # * message: The text of the notification message you want to send. def notify(event, message) raise RuntimeError, "Prowler needs to be configured first before using it" unless configured? http = Net::HTTP.new(host, port) http.use_ssl = secure http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.start do headers = { 'User-Agent' => 'ProwlScript/1.0' } http.read_timeout = 5 # seconds http.open_timeout = 2 # seconds request = Net::HTTP::Get.new(path(URI.escape(application), URI.escape(event), URI.escape(message)), headers) request.basic_auth(username, password) response = begin http.request(request) if send_notifications? rescue TimeoutError => e logger.error "Timeout while contacting the Prowl server." nil end case response when Net::HTTPSuccess then logger.info "Prowl Success: #{response.class}" when NilClass then # Do nothing else logger.error "Prowl Failure: #{response.class}\n#{response.body if response.respond_to? :body}" end end end end end