post_commit 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/README.rdoc +27 -9
- data/lib/post_commit/hooks/base.rb +36 -1
- data/lib/post_commit/hooks/url.rb +31 -3
- data/lib/post_commit/version.rb +1 -1
- data/spec/post_commit/base_spec.rb +27 -0
- data/spec/post_commit/url_spec.rb +43 -0
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Introduction
|
2
2
|
|
3
|
-
Post commit allows you to notify several services with simple and elegant DSL. It currently supports 5 services:
|
3
|
+
Post commit allows you to notify several services with a simple and elegant DSL. It currently supports 5 services:
|
4
4
|
|
5
5
|
* Basecamp
|
6
6
|
* Campfire
|
@@ -80,16 +80,34 @@ The Twitter hook supports public and direct messages.
|
|
80
80
|
|
81
81
|
To send a message to an arbitrary URL, use the <tt>:url</tt>.
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
post_commit :url do
|
84
|
+
post "http://example.com", :status => "pending", :id => 1234, :service => "yourapp"
|
85
|
+
end
|
86
86
|
|
87
87
|
If you need a basic authorization, just inform the <tt>username</tt> and <tt>password</tt>.
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
post_commit :url do
|
90
|
+
authorize :username => "johndoe", :password => "mypass"
|
91
|
+
post "http://example.com", :status => "running", :id => 1234, :service => "yourapp"
|
92
|
+
end
|
93
|
+
|
94
|
+
You can post data encoded as JSON or XML. Just set the <tt>:data_type</tt> option.
|
95
|
+
|
96
|
+
post_commit :url do
|
97
|
+
set :data_type, :json
|
98
|
+
set :data_type, :xml
|
99
|
+
end
|
100
|
+
|
101
|
+
To post a XML, your params hash need to be a multi-level hash. For instance, the hash
|
102
|
+
<tt>{:message => { :title => "Some title", :body => "Some message" }}</tt> will be converted to
|
103
|
+
|
104
|
+
<message>
|
105
|
+
<title>Some title</title>
|
106
|
+
<body>Some message</body>
|
107
|
+
</message>
|
108
|
+
|
109
|
+
<b>Gotcha:</b> to send a normal POST, you have to specify a flat hash.
|
110
|
+
So <tt>{:a => 1}</tt> is ok, but <tt>{:a => {:b => 1}}</tt> will fail.
|
93
111
|
|
94
112
|
= Creating new hooks
|
95
113
|
|
@@ -121,10 +139,10 @@ You can use the <tt>credentials</tt> attribute to retrieve these settings.
|
|
121
139
|
= To-Do
|
122
140
|
|
123
141
|
* Receive post commits
|
124
|
-
* URL hook must post JSON and XML data
|
125
142
|
* Create IRC hook
|
126
143
|
* Create Jabber hook
|
127
144
|
* Create email hook
|
145
|
+
* All hooks should use the <tt>:url</tt> hook
|
128
146
|
|
129
147
|
= Maintainer
|
130
148
|
|
@@ -2,7 +2,7 @@ module PostCommit
|
|
2
2
|
module Hooks
|
3
3
|
class Base
|
4
4
|
# Hold the authorization options.
|
5
|
-
|
5
|
+
attr_reader :credentials
|
6
6
|
|
7
7
|
# Hold the latest response
|
8
8
|
attr_reader :response
|
@@ -13,8 +13,12 @@ module PostCommit
|
|
13
13
|
# Hold the latest uri object
|
14
14
|
attr_reader :uri
|
15
15
|
|
16
|
+
# Hold options defined throught the PostCommit::Hooks::Base#set method
|
17
|
+
attr_reader :options
|
18
|
+
|
16
19
|
def initialize
|
17
20
|
@credentials = {}
|
21
|
+
@options = {}
|
18
22
|
end
|
19
23
|
|
20
24
|
def self.inherited(base)
|
@@ -27,9 +31,40 @@ module PostCommit
|
|
27
31
|
@credentials = options
|
28
32
|
end
|
29
33
|
|
34
|
+
# Set an option for the current hook.
|
35
|
+
# The available options may vary from hook to hook and may not be available at all.
|
36
|
+
#
|
37
|
+
# set :data_type, :json
|
38
|
+
def set(name, value)
|
39
|
+
@options[name.to_sym] = value
|
40
|
+
end
|
41
|
+
|
30
42
|
def post(options = {}) # :nodoc:
|
31
43
|
raise PostCommit::AbstractMethodError
|
32
44
|
end
|
45
|
+
|
46
|
+
def convert_to_xml(hash) # :nodoc:
|
47
|
+
xml_node = Proc.new do |name, value, buffer|
|
48
|
+
buffer ||= ""
|
49
|
+
|
50
|
+
if value.kind_of?(Hash)
|
51
|
+
buffer << "<#{name}>"
|
52
|
+
value.each {|n, v| buffer << xml_node[n, v] }
|
53
|
+
buffer << "</#{name}>"
|
54
|
+
else
|
55
|
+
buffer << "<#{name}><![CDATA[#{value}]]></#{name}>"
|
56
|
+
end
|
57
|
+
|
58
|
+
buffer
|
59
|
+
end
|
60
|
+
|
61
|
+
root = hash.keys.first
|
62
|
+
xml_node[root, hash[root]]
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_to_params(hash) # :nodoc:
|
66
|
+
hash
|
67
|
+
end
|
33
68
|
end
|
34
69
|
end
|
35
70
|
end
|
@@ -12,8 +12,26 @@ module PostCommit
|
|
12
12
|
# authorize :username => "johndoe", :password => "mypass"
|
13
13
|
# post "http://example.com", :message => "Post commit"
|
14
14
|
# end
|
15
|
+
#
|
16
|
+
# You can post data encoded as JSON or XML. Just set the <tt>:data_type</tt> option.
|
17
|
+
#
|
18
|
+
# post_commit :url do
|
19
|
+
# set :data_type, :json
|
20
|
+
# set :data_type, :xml
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# To post a XML, your params hash need to be a multi-level hash. For instance, the hash
|
24
|
+
# <tt>{:message => { :title => "Some title", :body => "Some message" }}</tt> will be converted to
|
25
|
+
#
|
26
|
+
# <message>
|
27
|
+
# <title>Some title</title>
|
28
|
+
# <body>Some message</body>
|
29
|
+
# </message>
|
30
|
+
#
|
31
|
+
# <b>Gotcha:</b> to send a normal POST, you have to specify a flat hash. So <tt>{:a => 1}</tt> is ok,
|
32
|
+
# but <tt>{:a => {:b => 1}}</tt> will fail.
|
15
33
|
class URL < Base
|
16
|
-
# Post data to an arbitrary URL.
|
34
|
+
# Post data to an arbitrary URL.
|
17
35
|
#
|
18
36
|
# post "http://example.com", :message => "Post commit", :service => "yourapp"
|
19
37
|
def post(url, params = {})
|
@@ -21,9 +39,19 @@ module PostCommit
|
|
21
39
|
http = Net::HTTP.new(uri.host, uri.port)
|
22
40
|
|
23
41
|
@request = Net::HTTP::Post.new(uri.path)
|
24
|
-
@request.basic_auth credentials[:username], credentials[:password] if credentials[:username]
|
25
|
-
@request.form_data = params
|
26
42
|
|
43
|
+
case options[:data_type]
|
44
|
+
when :json then
|
45
|
+
@request.content_type = "application/json"
|
46
|
+
@request.body = params.to_json
|
47
|
+
when :xml then
|
48
|
+
@request.content_type = "application/xml"
|
49
|
+
@request.body = convert_to_xml(params)
|
50
|
+
else
|
51
|
+
@request.form_data = convert_to_params(params)
|
52
|
+
end
|
53
|
+
|
54
|
+
@request.basic_auth credentials[:username], credentials[:password] if credentials[:username]
|
27
55
|
@response = http.request(@request)
|
28
56
|
|
29
57
|
if response.code =~ /^2\d+/
|
data/lib/post_commit/version.rb
CHANGED
@@ -4,4 +4,31 @@ describe PostCommit::Hooks::Base do
|
|
4
4
|
it "should raise when trying to call abstract method" do
|
5
5
|
doing { subject.post }.should raise_error(PostCommit::AbstractMethodError)
|
6
6
|
end
|
7
|
+
|
8
|
+
context "convert to params" do
|
9
|
+
it "should convert flat hash"
|
10
|
+
it "should convert multi-level hash"
|
11
|
+
end
|
12
|
+
|
13
|
+
context "convert to xml" do
|
14
|
+
it "should convert flat hash" do
|
15
|
+
data = {:chars => {:a => 1, :b => 2}}
|
16
|
+
xml = Nokogiri::XML(subject.convert_to_xml(data))
|
17
|
+
|
18
|
+
xml.at("chars > a").inner_text.should == "1"
|
19
|
+
xml.at("chars > b").inner_text.should == "2"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should convert multi-level hash" do
|
23
|
+
data = {
|
24
|
+
:chars => {
|
25
|
+
:a => {:lowercase => "a", :uppercase => "A"}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
xml = Nokogiri::XML(subject.convert_to_xml(data))
|
29
|
+
|
30
|
+
xml.at("chars > a > lowercase").inner_text.should == "a"
|
31
|
+
xml.at("chars > a > uppercase").inner_text.should == "A"
|
32
|
+
end
|
33
|
+
end
|
7
34
|
end
|
@@ -41,6 +41,12 @@ describe PostCommit::Hooks::URL do
|
|
41
41
|
subject.post(@url, :message => "Some message").should be_true
|
42
42
|
end
|
43
43
|
|
44
|
+
it "should return false with any status other than 2xx" do
|
45
|
+
FakeWeb.register_uri(:post, @url, :status => ["401", "Unauthorized"])
|
46
|
+
|
47
|
+
subject.post(@url, :message => "Some message").should be_false
|
48
|
+
end
|
49
|
+
|
44
50
|
it "should set post data" do
|
45
51
|
subject.post(@url, :message => "Some message", :id => 1234)
|
46
52
|
body = CGI.parse(subject.request.body)
|
@@ -48,4 +54,41 @@ describe PostCommit::Hooks::URL do
|
|
48
54
|
body["message"].to_s.should == "Some message"
|
49
55
|
body["id"].to_s.should == "1234"
|
50
56
|
end
|
57
|
+
|
58
|
+
context "data type" do
|
59
|
+
context "JSON string" do
|
60
|
+
before do
|
61
|
+
subject.set :data_type, :json
|
62
|
+
subject.post(@url, :message => {:title => "Some title", :body => "Some message"})
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should encode body" do
|
66
|
+
body = JSON.parse(subject.request.body)
|
67
|
+
|
68
|
+
body["message"]["title"].should == "Some title"
|
69
|
+
body["message"]["body"].should == "Some message"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should set headers" do
|
73
|
+
subject.request.content_type.should == "application/json"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "XML string" do
|
78
|
+
before do
|
79
|
+
subject.set :data_type, :xml
|
80
|
+
subject.post(@url, :message => {:title => "Some title", :body => "Some message"})
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should encode body" do
|
84
|
+
xml = Nokogiri::XML(subject.request.body)
|
85
|
+
xml.at("message > title").inner_text.should == "Some title"
|
86
|
+
xml.at("message > body").inner_text.should == "Some message"
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should set headers" do
|
90
|
+
subject.request.content_type.should == "application/xml"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
51
94
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: post_commit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-02 00:00:00 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|