heroku_notification 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ # HerokuNotification
2
+
3
+ HerokuNotification provides an easy way of interfacing with the [Heroku
4
+ Notifications API](https://github.com/heroku/notifications).
5
+
6
+ ## Usage
7
+
8
+ The Notifications API requires a subject, text, and details of who or what
9
+ you want to send the message to:
10
+
11
+ HerokuNotification.post(:subject => "Will you be my valentine?",
12
+ :text => "Please help be support rampant consumerism.",
13
+ :to => @user)
14
+
15
+ Alternatively you can explicitly provie the recipient type and id:
16
+
17
+ HerokuNotification.post(:subject => "Will you be my valentine?",
18
+ :text => "Please help be support rampant consumerism.",
19
+ :to => "user",
20
+ :id => 1)
21
+
22
+ Include any additional parameters (e.g., `:html`) and they will be sent
23
+ along also.
24
+
25
+ ## Why would I want to use this?
26
+
27
+ Because you're too lazy to read the API docs for the main notifications
28
+ service, or you don't want to have to keep up with any API changes*. Both
29
+ are perfectly valid, and not mutually exclusive.
30
+
31
+ *I'll keep this client gem in line with the currently deployed API, while
32
+ making every effort to maintain backward compatibility in the gem.
33
+
34
+ ## Compatibility
35
+
36
+ Test suite has currently only been confirmed on the following platforms:
37
+
38
+ * MRI Ruby 1.9.2
39
+
40
+ ## Contributions
41
+
42
+ Patches gladly accepted. Please fork this repo, add a relevant test, and send
43
+ me a pull request.
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+
6
+ spec_data = File.open('heroku_notification.gemspec').read
7
+ spec = nil
8
+ Thread.new do
9
+ spec = eval("#{spec_data}")
10
+ end.join
11
+
12
+ Rake::GemPackageTask.new(spec) do |pkg|
13
+ pkg.need_zip = false
14
+ pkg.need_tar = false
15
+ end
16
+
17
+ require 'rake/testtask'
18
+ Rake::TestTask.new(:test) do |test|
19
+ test.libs << 'lib' << 'test'
20
+ test.pattern = 'test/**/*_test.rb'
21
+ test.verbose = false
22
+ end
@@ -0,0 +1,4 @@
1
+ base_dir = File.join(File.dirname(__FILE__), "heroku_notification")
2
+ ["base", "errors"].each do |lib|
3
+ require File.join(base_dir, lib)
4
+ end
@@ -0,0 +1,17 @@
1
+ require 'rest_client'
2
+ class HerokuNotification
3
+ def self.post(params = {})
4
+ if !params[:to].is_a?(String) && params[:to].respond_to?(:id)
5
+ params[:id] = params[:to].id
6
+ params[:to] = params[:to].class.to_s.downcase
7
+ elsif params[:to].is_a?(Symbol)
8
+ params[:to] = params[:to].to_s
9
+ end
10
+ RestClient.post(url + '/notifications', params)
11
+ end
12
+
13
+ private
14
+ def self.url
15
+ ENV["NOTIFICATIONS_URL"] || "https://notifications.heroku.com"
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ class HerokuNotificationError < StandardError; end
2
+
@@ -0,0 +1,26 @@
1
+ require 'heroku_notification/test_unit'
2
+ class HaveNotifiedWith
3
+
4
+ def initialize(params)
5
+ @params = params
6
+ end
7
+
8
+ def matches?(target)
9
+ assert_notified_with(@params)
10
+ true
11
+ rescue Spec::Expectations::ExpectationNotMetError
12
+ false
13
+ end
14
+
15
+ def failure_message
16
+ "expected Notification to be posted with #{@params.inspect}"
17
+ end
18
+ def negative_failure_message
19
+ "expected Notification to not be posted with #{@params.inspect}"
20
+ end
21
+ end
22
+
23
+ def have_notified_with(params)
24
+ HaveNotifiedWith.new(params)
25
+ end
26
+ include HerokuNotificationAssertions
@@ -0,0 +1,23 @@
1
+ require 'webmock/test_unit'
2
+ module HerokuNotificationAssertions
3
+ include WebMock::API
4
+ def assert_notified_with(params)
5
+ assert_requested(:post, "https://notifications.heroku.com/notifications") do |req|
6
+ params_in_post_body(params, req.body)
7
+ end
8
+ end
9
+
10
+ def stub_notification
11
+ stub_request(:post, "https://notifications.heroku.com/notifications")
12
+ end
13
+
14
+ private
15
+ def params_in_post_body(params, body)
16
+ params.each do |key,val|
17
+ encoded_pair = "#{CGI::escape(key.to_s)}=#{URI::escape(val.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
18
+ return false unless body.index(encoded_pair)
19
+ end
20
+ return true
21
+ end
22
+
23
+ end
@@ -0,0 +1,36 @@
1
+ $:.unshift File.expand_path("..", File.dirname(__FILE__))
2
+ require "test_helper"
3
+ require "heroku_notification/test_unit"
4
+
5
+ class User
6
+ def id
7
+ 2
8
+ end
9
+ end
10
+
11
+ class HerokuNotificationTest < Test::Unit::TestCase
12
+ include HerokuNotificationAssertions
13
+
14
+ context "posting a message to a user" do
15
+ setup do
16
+ stub_request(:post, "https://notifications.heroku.com/notifications")
17
+ end
18
+
19
+ should "pass through the params" do
20
+ params = { :subject => "Will you be my valentine?",
21
+ :text => "Please help be support rampant consumerism.",
22
+ :html => "You make my <blink>eyes</blink>",
23
+ :to => "user",
24
+ :id => 1 }
25
+ HerokuNotification.post(params)
26
+ assert_notified_with params
27
+ end
28
+
29
+ should "detect the target recipient without an explicit ID" do
30
+ params = { :to => User.new }
31
+ expected_params = { :to => "user", :id => 2 }
32
+ HerokuNotification.post(params)
33
+ assert_notified_with expected_params
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require 'webmock/test_unit'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require "heroku_notification"
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku_notification
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Glenn Gillen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-28 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rest-client
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: A client gem for interacting with the REST-based Heroku Notifications API
28
+ email: glenn@rubypond.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - README.mdown
37
+ - Rakefile
38
+ - lib/heroku_notification/base.rb
39
+ - lib/heroku_notification/errors.rb
40
+ - lib/heroku_notification/rspec.rb
41
+ - lib/heroku_notification/test_unit.rb
42
+ - lib/heroku_notification.rb
43
+ - test/heroku_notification/base_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/heroku/notifications_client
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - .
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.6.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: A wrapper for the Heroku Notifications service
74
+ test_files:
75
+ - test/heroku_notification/base_test.rb
76
+ - test/test_helper.rb