Created
July 6, 2009 21:39
-
-
Save lamdor/141707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # ProwlScript, to communicate with the Prowl server. | |
| # Copyright 2009 Luke Amdor | |
| # original script at http://prowl.weks.net/static/prowl.pl | |
| # | |
| # This requires running Prowl on your device. | |
| # See the Prowl website <http://prowl.weks.net> | |
| # Usage: | |
| # bin/prowl.rb -u username -p ~/.prowl_password -n "from ruby" | |
| require 'optparse' | |
| require 'net/https' | |
| require 'uri' | |
| require 'cgi' | |
| class Prowl | |
| DEFAULT_OPTIONS = { | |
| :application => "Prowl Command Line", | |
| :event => "Command Line" | |
| } | |
| PROWL_URI = "https://prowl.weks.net/api/add_notification.php" | |
| def initialize(args) | |
| @options = parse_options(args) | |
| end | |
| def push | |
| url = URI.parse(PROWL_URI) | |
| http = Net::HTTP.new(url.host, url.port) | |
| http.use_ssl = true | |
| path = url.path + '?' + query_data | |
| req = Net::HTTP::Get.new(path) | |
| req.basic_auth username, password | |
| response = http.request(req) | |
| case response | |
| when Net::HTTPSuccess | |
| #ok | |
| else | |
| resopnse.error! | |
| end | |
| end | |
| def username | |
| @options[:username] | |
| end | |
| def password | |
| if File.exists? @options[:password] | |
| File.readlines(@options[:password]).first.strip! | |
| else | |
| @options[:password] | |
| end | |
| end | |
| def query_data | |
| query_string = "" | |
| form_data.each do |k,v| | |
| query_string += k + "=" + CGI.escape(v) + "&" | |
| end | |
| query_string | |
| end | |
| def form_data | |
| {'application' => @options[:application], | |
| 'event' => @options[:event], | |
| 'description' => @options[:notification]} | |
| end | |
| def parse_options(args) | |
| options = DEFAULT_OPTIONS.dup | |
| opts = OptionParser.new do |opts| | |
| opts.on("-u USERNAME", "--username=USERNAME", "Username") do |username| | |
| options[:username] = username | |
| end | |
| opts.on("-p PASSWORD", "--password=PASSWORD", "Password / Password File") do |password| | |
| options[:password] = password | |
| end | |
| opts.on("-a APPLICATION", "--application=APPLICATION", "Application") do |application| | |
| options[:application] = application | |
| end | |
| opts.on("-e EVENT", "--event=EVENT", "Event") do |event| | |
| options[:event] = event | |
| end | |
| opts.on("-n NOTIFICATION", "--notification=NOTIFICATION", "Notification") do |notification| | |
| options[:notification] = notification | |
| end | |
| opts.on_tail("-h", "--help", "Show this message") do | |
| puts opts | |
| exit | |
| end | |
| end | |
| opts.parse(args) | |
| options | |
| end | |
| end | |
| Prowl.new(ARGV).push | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment