pax_global_header00006660000000000000000000000064134401613670014517gustar00rootroot0000000000000052 comment=c6469d2ed2522d0e3f0621cc11224ed4d7026e2f ruby-rushover-0.3.0/000077500000000000000000000000001344016136700143535ustar00rootroot00000000000000ruby-rushover-0.3.0/.gitignore000066400000000000000000000002411344016136700163400ustar00rootroot00000000000000*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp .rvmrc ruby-rushover-0.3.0/CHANGELOG.md000066400000000000000000000001501344016136700161600ustar00rootroot000000000000000.3.0 (February 19, 2013) * Add receipt endpoint handling for checking alerts requiring acknowledgement ruby-rushover-0.3.0/Gemfile000066400000000000000000000001351344016136700156450ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in rushover.gemspec gemspec ruby-rushover-0.3.0/LICENSE000066400000000000000000000020561344016136700153630ustar00rootroot00000000000000Copyright (c) 2012 Brendon Murphy MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.ruby-rushover-0.3.0/README.md000066400000000000000000000032351344016136700156350ustar00rootroot00000000000000# Rushover A simple ruby [Pushover](https://pushover.net/) client. Pushover allows sending simple push notifications to clients on iOS and Android devices. ## Installation Add this line to your application's Gemfile: gem 'rushover' And then execute: $ bundle Or install it yourself as: $ gem install rushover ## Usage ```ruby require "rushover" client = Rushover::Client.new(your_app_token) resp = client.notify(user_key, "some message", :priority => 1, :title => "a title!") resp.ok? # => true # You can also send emergency priority 2 messages # Note you must provide expire and retry options for this to succeed client.notify(user_key, "some message", :priority => 2, :expire => 180, :retry => 60) # Check a receipt client.reciept("S2sXbSL2IKfl6caouD8hJXVn4SoD36") # Validate that a user exists client.validate!(existing_user_key) # => true client.validate!(existing_user_key, existing_device) # => true # Also provides a User class for convenience. Just keeps the user key # around if you want to deal with a User object user = Rushover::User.new(user_key, rushover_client) user.notify("some user message", :title => "another title") ``` Optional params to the Pushover like `priority` or `title` are passed through. Calls to `#notify` will return a `Rushover::Response`. Pushover app uses :status => 1 for success, 0 for failures. The response can be checked for success with `#ok?`, or mined like a plain old hash. ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Added some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ruby-rushover-0.3.0/Rakefile000066400000000000000000000003211344016136700160140ustar00rootroot00000000000000#!/usr/bin/env rake require "bundler/gem_tasks" require "rake/testtask" Rake::TestTask.new(:test) do |t| t.test_files = FileList["test/*_test.rb"] t.ruby_opts << '-Itest -Ilib' end task :default => :test ruby-rushover-0.3.0/lib/000077500000000000000000000000001344016136700151215ustar00rootroot00000000000000ruby-rushover-0.3.0/lib/rushover.rb000066400000000000000000000041171344016136700173260ustar00rootroot00000000000000require "rushover/version" require "rest-client" require "json" module Rushover class Client BASE_URL = "https://api.pushover.net/1".freeze MESSAGES_ENDPOINT = "#{BASE_URL}/messages.json".freeze VALIDATE_ENDPOINT = "#{BASE_URL}/users/validate.json".freeze RECEIPT_ENDPOINT = "#{BASE_URL}/receipts/%s.json".freeze attr_accessor :token def initialize(token) @token = token end def notify(user_key, message, options = {}) data = { :token => token, :user => user_key, :message => message } data.merge!(options) post_json(MESSAGES_ENDPOINT, data) end def validate(user_key, device = nil) data = { :token => token, :user => user_key } data[:device] = device if device post_json(VALIDATE_ENDPOINT, data) end def validate!(user_key, device = nil) validate(user_key, device).ok? end def receipt(id) data = { :token => token } url = RECEIPT_ENDPOINT % id get_json(url, data) end private def parse_response raw_response = begin yield rescue RestClient::Exception => e e.response end Response.new JSON.parse(raw_response) end def get_json(url, data) parse_response do RestClient.get url, { :params => data } end end def post_json(url, data) parse_response do RestClient.post url, data end end end class User attr_accessor :key, :client def initialize(key, client) @key = key @client = client end def notify(message, options = {}) client.notify(key, message, options) end def validate(device = nil) client.validate(key, device) end def validate!(device = nil) validate(device).ok? end end class Response attr_accessor :data def initialize(data) @data = data end def [](key) @data[key.to_s] end def ok? self["status"].to_i == 1 end def inspect @data.inspect end def to_s @data.to_s end def to_h @data.dup end end end ruby-rushover-0.3.0/lib/rushover/000077500000000000000000000000001344016136700167765ustar00rootroot00000000000000ruby-rushover-0.3.0/lib/rushover/version.rb000066400000000000000000000000501344016136700210030ustar00rootroot00000000000000module Rushover VERSION = "0.3.0" end ruby-rushover-0.3.0/rushover.gemspec000066400000000000000000000014611344016136700175770ustar00rootroot00000000000000# -*- encoding: utf-8 -*- require File.expand_path('../lib/rushover/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["Brendon Murphy"] gem.email = ["xternal1+github@gmail.com"] gem.summary = %q{A simple ruby Pushover client} gem.description = gem.summary gem.homepage = "" gem.files = `git ls-files`.split($\) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "rushover" gem.require_paths = ["lib"] gem.version = Rushover::VERSION gem.add_dependency "json" gem.add_dependency "rest-client" gem.add_development_dependency "rake" gem.add_development_dependency "contest" gem.add_development_dependency "fakeweb" end ruby-rushover-0.3.0/test/000077500000000000000000000000001344016136700153325ustar00rootroot00000000000000ruby-rushover-0.3.0/test/rushover_client_test.rb000066400000000000000000000101471344016136700221340ustar00rootroot00000000000000require File.expand_path("../test_helper", __FILE__) class RushoverClientTest < RushoverTest def client Rushover::Client.new("test_api_token") end test "it initializes with a token" do subject = Rushover::Client.new("foobar") assert_equal "foobar", subject.token end context "with mandatory params" do setup do client.notify("test_user", "test message") end test "sending a message hits the messages.json endpoint" do assert_equal "/1/messages.json", FakeWeb.last_request.path end test "sending a message uses the token" do assert_equal "test_api_token", last_request_param("token") end test "sending a message to the intended user" do assert_equal "test_user", last_request_param("user") end test "sending a notification includes the message" do assert_equal "test message", last_request_param("message") end test "successful notify" do resp = client.notify("test_user", "test message") assert_equal 1, resp["status"] end test "failed notify" do FakeWeb.register_uri(:post, "https://api.pushover.net/1/messages.json", :body => { "message" => "cannot be blank", "status" => 0 }.to_json, :content_type => "application/json", :status => ["400", "Bad request"]) resp = client.notify("test_user", nil) assert_equal 0, resp["status"] assert_equal "cannot be blank", resp["message"] end end context "with optional params" do test "basic optional params" do client.notify("test_user", "test message", :device => "test device", :title => "test title", :priority => 1, :timestamp => 123123) assert_equal "test device", last_request_param("device") assert_equal "test title", last_request_param("title") assert_equal "1", last_request_param("priority").to_s assert_equal "123123", last_request_param("timestamp").to_s end end context "looking up receipt data" do test "when the receipt exists" do FakeWeb.register_uri(:get, "https://api.pushover.net/1/receipts/123asdf.json?token=test_api_token", :body => {"status"=>1, "acknowledged"=>1, "acknowledged_at"=>1361314981, "last_delivered_at"=>1361314753, "expired"=>1, "expires_at"=>1361314783, "called_back"=>0, "called_back_at"=>0, "request"=>"9a38ad590235bc07ea7ae2b5fd83f99f"}.to_json, :content_type => "application/json") resp = client.receipt("123asdf") assert_equal 1, resp[:expired] assert_equal 1, resp[:acknowledged] assert_equal 1361314981, resp[:acknowledged_at] assert_equal "9a38ad590235bc07ea7ae2b5fd83f99f", resp[:request] end end context "user validation" do test "determining a user exists" do FakeWeb.register_uri(:post, "https://api.pushover.net/1/users/validate.json", :body => { "status" => 1 }.to_json, :content_type => "application/json") resp = client.validate("user_exists") assert_equal "user_exists", last_request_param("user") assert resp.ok? end test "determining a user device exists" do FakeWeb.register_uri(:post, "https://api.pushover.net/1/users/validate.json", :body => { "status" => 1 }.to_json, :content_type => "application/json") resp = client.validate("user_exists", "htc4g") assert_equal "htc4g", last_request_param("device") end test "determining a user does not exist" do FakeWeb.register_uri(:post, "https://api.pushover.net/1/users/validate.json", :body => { "status" => 0 }.to_json, :content_type => "application/json") resp = client.validate("user_missing") assert_equal "user_missing", last_request_param("user") refute resp.ok? end test "validate! calls ok?" do assert client.validate!("user_exists") end end end ruby-rushover-0.3.0/test/rushover_response_test.rb000066400000000000000000000020571344016136700225150ustar00rootroot00000000000000require File.expand_path("../test_helper", __FILE__) class RushoverResponseTest < RushoverTest def data @data ||= { "status" => 1 } end test "initializing with a response data" do response = Rushover::Response.new(data) assert_equal data, response.data end test "delegates [] lookups to the response hash" do data = { "status" => 1, "foo" => "bar" } response = Rushover::Response.new(data) assert_equal 1, response["status"] assert_equal "bar", response[:foo] end test "checking if ok?" do response = Rushover::Response.new(data) assert response.ok? response.data = { "status" => 0 } refute response.ok? end test "inspect delegates to the data" do response = Rushover::Response.new(data) assert_equal data.inspect, response.inspect end test "to_s delegates to the data" do response = Rushover::Response.new(data) assert_equal data.to_s, response.to_s end test "to_h returns the data" do response = Rushover::Response.new(data) assert_equal data, response.to_h end end ruby-rushover-0.3.0/test/rushover_user_test.rb000066400000000000000000000016401344016136700216320ustar00rootroot00000000000000require File.expand_path("../test_helper", __FILE__) class RushoverUserTest < RushoverTest def client Rushover::Client.new("test_api_token") end setup do @user = Rushover::User.new("user_key", client) end test "it initializes with a user key and client" do assert_equal "user_key", @user.key assert @user.client end test "notifying the user via the client" do @user.notify("test user message", :priority => 1) assert_equal "test user message", last_request_param("message") assert_equal "1", last_request_param("priority").to_s end test "validate if user exists" do resp = @user.validate assert_equal "user_key", last_request_param("user") assert resp.ok? end test "validate if user device exists" do @user.validate("htc4g") assert_equal "htc4g", last_request_param("device") end test "validate! calls ok?" do assert @user.validate! end end ruby-rushover-0.3.0/test/test_helper.rb000066400000000000000000000013521344016136700201760ustar00rootroot00000000000000require "test/unit" require "contest" require "fakeweb" require "rushover" FakeWeb.allow_net_connect = false class RushoverTest < Test::Unit::TestCase setup do FakeWeb.register_uri(:post, "https://api.pushover.net/1/messages.json", :body => { :status => 1 }.to_json, :content_type => "application/json") FakeWeb.register_uri(:post, "https://api.pushover.net/1/users/validate.json", :body => { "status" => 1 }.to_json, :content_type => "application/json") end teardown do FakeWeb.clean_registry end def last_request_param(param) params = CGI.parse(FakeWeb.last_request.body) Array(params[param]).first end end