Permalink
Browse files

add service hook for pushover

  • Loading branch information...
jcs committed Mar 7, 2012
1 parent 8529ab2 commit 9dadc954629a4f40bb2c641cd9d9ab8538900534
Showing with 93 additions and 0 deletions.
  1. +23 −0 docs/pushover
  2. +40 −0 services/pushover.rb
  3. +30 −0 test/pushover_test.rb
@@ -0,0 +1,23 @@
Pushover
==========

Pushover is a cross-platform push notification service with a [simple API](https://pushover.net/api).

Install Notes
-------------

1. Install the [iOS client](https://pushover.net/clients) from the App Store, or the [Android client](https://pushover.net/clients) from the Android Market.

2. Login to [Pushover](https://pushover.net/) and copy your user key.

3. Add your user key in the field above. Optionally enter a device name to have notifications go to a single device, or leave blank to send to all active devices on your account.

Developer Notes
---------------

data
- user_key
- device_name

payload
- refer to docs/github_payload
@@ -0,0 +1,40 @@
class Service::Pushover < Service
string :user_key
string :device_name

def receive_push
if !payload["commits"].any?
return
end

if !data["user_key"]
raise_config_error "Invalid Pushover user key."
end

url = URI.parse("https://api.pushover.net/1/messages.json")

commits = payload["commits"].length
repo = payload["repository"]["url"].split("/")[-2 .. -1].join("/")
latest_message = payload["commits"].last["message"].split("\n").first
if latest_message.length > 100
latest_message = latest_message[0 ... 96] + "[..]"
end
latest_url = shorten_url(payload["commits"].last["url"])

if commits == 1
title = "#{payload["pusher"]["name"]} pushed to #{repo}"
message = "#{latest_message}\n#{latest_url}"
else
title = "#{payload["pusher"]["name"]} pushed #{commits} " +
"commit#{commits == 1 ? '' : 's'} to #{repo}"
message = "Latest: #{latest_message}\n#{latest_url}"
end

http_post url.to_s,
:token => "E6jpcJjaeASA7CWQ0cYjW6oP9N5xtJ",
:user => data["user_key"],
:device => data["device_name"],
:title => title,
:message => message
end
end
@@ -0,0 +1,30 @@
require File.expand_path('../helper', __FILE__)

class PushoverTest < Service::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end

def test_push
svc = service({"user_key" => "a", "device_name" => "hi"}, payload)

def svc.shorten_url(*args)
"short"
end

@stubs.post "/1/messages.json" do |env|
assert_equal "api.pushover.net", env[:url].host
data = Rack::Utils.parse_query(env[:body])
assert_equal "a", data["user"]
assert_equal "hi", data["device"]
[200, {}, '']
end

svc.receive_push
end

def service(*args)
super Service::Pushover, *args
end
end

0 comments on commit 9dadc95

Please sign in to comment.