Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'prowly/interface' | ||
require 'prowly/notification' | ||
|
||
module Prowly | ||
|
||
extend self | ||
|
||
def add(notification = Notification.new) | ||
yield notification if block_given? | ||
api.call Interface::Command::ADD, notification.to_params | ||
end | ||
|
||
def verify(apikey) | ||
api.call Interface::Command::VERIFY, "apikey=#{apikey}" | ||
end | ||
|
||
def valid_key?(key) | ||
result = verify(key) | ||
result.succeeded? | ||
end | ||
|
||
def remaining_calls(key) | ||
result = verify(key) | ||
return result.remaining if result.succeeded? | ||
result.message | ||
end | ||
|
||
private | ||
def api | ||
Interface.instance | ||
end | ||
|
||
#aliases | ||
alias :notify :add | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'net/https' | ||
require 'prowly/response' | ||
require 'prowly/notification' | ||
require 'singleton' | ||
|
||
module Prowly | ||
|
||
class Interface | ||
|
||
include Singleton | ||
|
||
def initialize | ||
@url = "https://prowl.weks.net/publicapi" | ||
end | ||
|
||
## Make the actual call to the prowl api | ||
def call(command, params) | ||
@command = command | ||
request = Net::HTTP::Get.new(uri.request_uri + "?" + params) | ||
response = http.request(request) | ||
Response.new(response.body, response) | ||
end | ||
|
||
private | ||
def http | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
http | ||
end | ||
|
||
def uri | ||
URI.parse("#{@url}/#{@command}") | ||
end | ||
|
||
## Available commands are documented in http://prowl.weks.net/api.php | ||
module Command | ||
ADD = "add" | ||
VERIFY = "verify" | ||
end | ||
|
||
## EXCEPTIONS | ||
class NoAPIKeyGiven < RuntimeError; end | ||
|
||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require 'cgi' | ||
|
||
module Prowly | ||
|
||
class BasicNotification | ||
|
||
## EXCEPTIONS | ||
class NoAPIKeyGiven < RuntimeError; end | ||
class NoDescriptionGiven < RuntimeError; end | ||
class NoApplicationNameGiven < RuntimeError; end | ||
class DuplicatedAssignmentOfApiKey < RuntimeError; end | ||
|
||
attr_accessor :application, :description | ||
attr_writer :apikey | ||
|
||
alias :apikeys= :apikey= | ||
|
||
def apikey | ||
if @apikey.is_a? Array | ||
@apikey.join(',') | ||
else | ||
@apikey | ||
end | ||
end | ||
alias :apikeys :apikey | ||
|
||
def initialize(params = {}) | ||
if params[:apikeys] and params[:apikey] | ||
raise DuplicatedAssignmentOfApiKey, "Use apikey or apikeys, not both" | ||
else | ||
@apikey = params[:apikeys] unless params[:apikeys].nil? | ||
@apikey = params[:apikey] unless params[:apikey].nil? | ||
end | ||
end | ||
|
||
def to_params | ||
raise NoAPIKeyGiven if apikey.nil? | ||
raise NoApplicationNameGiven if @application.nil? | ||
raise NoDescriptionGiven if @description.nil? | ||
params.join('&') | ||
end | ||
|
||
private | ||
def params | ||
attributes = [] | ||
instance_variables.each do |var| | ||
raw_attr = "#{var.sub('@','')}" | ||
value = send("#{raw_attr}") | ||
next if value.nil? | ||
attributes << "#{raw_attr}=" + CGI.escape(value.to_s) | ||
end | ||
attributes.sort | ||
end | ||
|
||
end | ||
|
||
class Notification < BasicNotification | ||
|
||
## EXCEPTIONS | ||
class PriorityNotAvailable < RuntimeError; end | ||
|
||
attr_accessor :providerkey, :priority, :event | ||
|
||
def initialize(params = {}) | ||
@apikey = (params[:apikey] || params[:apikeys]) || "fffffffffffffffffffffffffffffffffffffffff" | ||
@application = params[:application] || "Prowly" | ||
@event = params[:event] || "Prowly is working!!" | ||
@description = params[:description] || "This is the default description" | ||
@priority = params[:priority] || Priority::NORMAL | ||
super | ||
end | ||
|
||
public | ||
## Priorities are documented in http://prowl.weks.net/api.php | ||
module Priority | ||
VERY_LOW = -2 | ||
MODERATE = -1 | ||
NORMAL = 0 | ||
HIGH = 1 | ||
EMERGENCY = 2 | ||
|
||
def self.const_missing(const) | ||
raise PriorityNotAvailable, const | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'rexml/document' | ||
|
||
module Prowly | ||
|
||
class Response | ||
|
||
attr_writer :response | ||
attr_accessor :http_response | ||
|
||
def initialize(xml_response, full_http_response) | ||
@response = xml_response | ||
@http_response = full_http_response | ||
Response.map_xml @response | ||
end | ||
|
||
def self.map_xml(response) | ||
#parse xml | ||
data = REXML::Document.new response | ||
response_info = data.root[1] | ||
|
||
#define dynamic methods based on the prowl api response | ||
#posible methods on success: code, remaining, resetdate | ||
#posible methods on error: code | ||
response_info.attributes.each do |key, value| | ||
add_instance_method(key, value) | ||
end | ||
|
||
#define a method named status and it'll return "success" or "error" | ||
Response.add_instance_method(:status, response_info.name) | ||
|
||
if response_info.name == "success" | ||
boolean_status = true | ||
elsif response_info.name == "error" | ||
boolean_status = false | ||
end | ||
|
||
add_instance_method(:message, response_info.text) unless boolean_status | ||
|
||
add_instance_method(:succeeded?, boolean_status) | ||
true | ||
end | ||
|
||
#define dynamic methods | ||
def self.add_instance_method(name, content) | ||
define_method(name) { content } | ||
end | ||
|
||
## ERRORCODES are documented in http://prowl.weks.net/api.php | ||
module ErrorCode | ||
BAD = 400 #Bad request, the parameters you provided did not validate, see ERRORMESSAGE. | ||
NOT_AUTHORIZED = 401 #Not authorized, the API key given is not valid, and does not correspond to a user. | ||
METHOD_NOT_ALLOWED = 405 #Method not allowed, you attempted to use a non-SSL connection to Prowl. | ||
NOT_ACCEPTABLE = 406 #Not acceptable, your IP address has exceeded the API limit. | ||
INTERNAL_SERVER_ERROR = 500 #Internal server error, something failed to execute properly on the Prowl side. | ||
end | ||
|
||
module SuccessCode | ||
SUCCESS = 200 #Everything went fine | ||
end | ||
|
||
end | ||
|
||
end |