gwooks 0.0.5 → 0.0.6

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6781d02a796d53e92d4bce412fe1bd0908c4eb25
4
+ data.tar.gz: 3a563a41d93ae70f2c4e415a941f400dcb97dc8a
5
+ SHA512:
6
+ metadata.gz: 40e37a11a10d6e24d3d28e8c50d4f1590c60d03f68cafbb12cb9c01b7eaa543abbbc7f3227e495f8d348d62dd0b9d01df4e3e34c23fe8b56d430f08a6a6c8906
7
+ data.tar.gz: 1f9724f655fb69e51080b8aaf28d3df73db8a9a08861ef6141c13a83abebcae9502f6c541fde03bd9c4686195634a4830d94dcf6eb0b892b627e12f663572cff
data/README.md CHANGED
@@ -26,26 +26,34 @@ performed in response to a push:
26
26
 
27
27
  ```ruby
28
28
  class MyActions < Gwooks::Base
29
-
29
+
30
+ # Do something when GitHub receives a push to a repository
31
+ # named "my_cool_project":
30
32
  repository_name "my_cool_project" do
31
- # this block gets executed when GitHub receives
32
- # a push to a repo named "my_cool_project"
33
33
 
34
- # Here you can also access the payload sent by GitHub, parsed to a hash:
35
- contributors = payload[:commits].map {|c| c[:author][:name] }.uniq
36
- send_email "someone@email.com", "my_cool_project received changes by: #{ contributors.join(', ') }"
37
- end
34
+ # You can nest matchers. The following block for example is executed
35
+ # when GitHub receives a push to the 'master' branch:
36
+ branch "master" do
37
+ # You have also access to the payload sent by GitHub, parsed to a hash:
38
+ contributors = payload[:commits].map {|c| c[:author][:email] }
39
+ contributors.uniq.each do |email|
40
+ send_email email, "Thanks for your contribution :)"
41
+ end
42
+ end
38
43
 
39
- commit_message /Bump new version v(\d+\.\d+\.\d+)/ do |matches|
40
- # this block gets called when GitHub receives a push
41
- # with at least one commit message matching the Regexp.
42
- matches.each do |match|
43
- send_email("someone@email.com", "New version released: #{match[1]}")
44
+ # You can match with regular exceptions too. E.g. the next block gets
45
+ # called when GitHub receives a push with at least one commit message
46
+ # matching the given Regexp:
47
+ commit_message /Bump new version v(\d+\.\d+\.\d+)/ do |matches|
48
+ matches.each do |match|
49
+ send_email("someone@email.com", "New version released: #{match[1]}")
50
+ end
44
51
  end
52
+
45
53
  end
46
-
54
+
47
55
  private
48
-
56
+
49
57
  def send_email(to, msg)
50
58
  # assume we define here a method to send an email
51
59
  end
@@ -112,10 +120,11 @@ match is a MatchData object when regexp matching is used, or the matched
112
120
  pattern otherwise. The block is evaluated in the instance scope, and thus can
113
121
  access the `payload` object.
114
122
 
115
- Here is the full list of DSL methods:
123
+ Here is the full list of DSL methods:
116
124
  ```
117
125
  after
118
126
  before
127
+ branch
119
128
  commits_added (alias: commit_added)
120
129
  commits_author_email (alias: commit_author_email)
121
130
  commits_author_name (alias: commit_author_name)
@@ -138,6 +147,10 @@ repository_url (alias: repo_url)
138
147
  repository_watchers (alias: repo_watchers)
139
148
  ```
140
149
 
150
+ Each of them matches a corresponding field in the payload sent by GitHub,
151
+ except `branch`, which is a shortcut to match the branch using info in the
152
+ `ref` (e.g. `branch "master"` is a shortcut for `ref "refs/heads/master"`)
153
+
141
154
 
142
155
  ## Payload
143
156
 
@@ -172,6 +185,8 @@ may change frequently.
172
185
 
173
186
  ## Changelog
174
187
 
188
+ v0.0.6 - Add possibility to nest matcher methods
189
+
175
190
  v0.0.5 - Add `branch` payload property and corresponding DSL method
176
191
 
177
192
  v0.0.4 - Payload with indifferent access
data/gwooks.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
  gem.add_dependency("json")
20
20
  gem.add_dependency("sinatra")
21
+ gem.add_development_dependency("rake")
21
22
  gem.add_development_dependency("rspec")
22
23
  gem.add_development_dependency("rack-test")
23
24
  end
data/lib/gwooks/base.rb CHANGED
@@ -3,6 +3,32 @@ require File.expand_path("payload.rb", File.dirname(__FILE__))
3
3
  module Gwooks
4
4
  class Base
5
5
 
6
+ DSL_METHODS = %w(
7
+ after
8
+ before
9
+ branch
10
+ commits_added
11
+ commits_author_email
12
+ commits_author_name
13
+ commits_id
14
+ commits_message
15
+ commits_modified
16
+ commits_removed
17
+ commits_timestamp
18
+ commits_url
19
+ ref
20
+ repository_description
21
+ repository_forks
22
+ repository_homepage
23
+ repository_name
24
+ repository_owner_email
25
+ repository_owner_name
26
+ repository_pledgie
27
+ repository_private
28
+ repository_url
29
+ repository_watchers
30
+ )
31
+
6
32
  class << self
7
33
  def call(payload)
8
34
  new(payload).call
@@ -17,37 +43,7 @@ module Gwooks
17
43
  @_hooks << [key, pattern, block]
18
44
  end
19
45
 
20
- def ref(pattern, &block)
21
- payload_matches("ref", pattern, &block)
22
- end
23
-
24
- method_names = %w(
25
- after
26
- before
27
- branch
28
- commits_added
29
- commits_author_email
30
- commits_author_name
31
- commits_id
32
- commits_message
33
- commits_modified
34
- commits_removed
35
- commits_timestamp
36
- commits_url
37
- ref
38
- repository_description
39
- repository_forks
40
- repository_homepage
41
- repository_name
42
- repository_owner_email
43
- repository_owner_name
44
- repository_pledgie
45
- repository_private
46
- repository_url
47
- repository_watchers
48
- )
49
-
50
- method_names.each do |method_name|
46
+ DSL_METHODS.each do |method_name|
51
47
  key = method_name.gsub("_", ".")
52
48
 
53
49
  define_method(method_name.to_sym) do |pattern, &block|
@@ -55,7 +51,7 @@ module Gwooks
55
51
  end
56
52
  end
57
53
 
58
- to_be_aliased = method_names.select do |n|
54
+ to_be_aliased = DSL_METHODS.select do |n|
59
55
  n.start_with? "commits_", "repository_"
60
56
  end
61
57
 
@@ -75,28 +71,54 @@ module Gwooks
75
71
  attr_reader :payload
76
72
 
77
73
  def initialize(payload)
78
- @payload = Gwooks::Payload.new(payload)
74
+ @payload = Gwooks::Payload.new(payload)
79
75
  end
80
76
 
81
77
  def call
82
78
  self.class.hooks.each do |hook|
83
79
  key, pattern, block = *hook
84
- target = payload.resolve(key)
85
- if target.is_a? Array
86
- match = target.map do |t|
87
- match_pattern(t, pattern)
88
- end.compact
89
- matching = match.compact.size > 0
80
+ exec_if_matching( key, pattern, &block )
81
+ end
82
+ nil
83
+ end
84
+
85
+ DSL_METHODS.each do |method_name|
86
+ key = method_name.gsub("_", ".")
87
+
88
+ define_method(method_name.to_sym) do |pattern, &block|
89
+ exec_if_matching(key, pattern, &block)
90
+ end
91
+ end
92
+
93
+ to_be_aliased = DSL_METHODS.select do |n|
94
+ n.start_with? "commits_", "repository_"
95
+ end
96
+
97
+ to_be_aliased.each do |method_name|
98
+ alias_name = method_name.gsub /^(commits|repository)_/ do
99
+ if $1 == "commits"
100
+ "commit_"
90
101
  else
91
- match = match_pattern(target, pattern)
92
- matching = !match.nil?
102
+ "repo_"
93
103
  end
94
- instance_exec(match, &block) if matching
95
104
  end
96
- nil
105
+ alias_method alias_name, method_name
97
106
  end
98
107
 
99
- private
108
+ private
109
+
110
+ def exec_if_matching(key, pattern, &block)
111
+ target = payload.resolve(key)
112
+ if target.is_a? Array
113
+ match = target.map do |t|
114
+ match_pattern(t, pattern)
115
+ end.compact
116
+ match = nil if match.size == 0
117
+ else
118
+ match = match_pattern(target, pattern)
119
+ end
120
+ instance_exec(match, &block) if match
121
+ end
100
122
 
101
123
  def match_pattern(target, pattern)
102
124
  if pattern.is_a? Regexp
@@ -1,3 +1,3 @@
1
1
  module Gwooks
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -3,6 +3,32 @@ require File.expand_path("../../lib/gwooks/base.rb", File.dirname(__FILE__))
3
3
 
4
4
  describe "subclass of Gwooks::Base" do
5
5
 
6
+ dsl_methods = %w(
7
+ after
8
+ before
9
+ branch
10
+ commits_added
11
+ commits_author_email
12
+ commits_author_name
13
+ commits_id
14
+ commits_message
15
+ commits_modified
16
+ commits_removed
17
+ commits_timestamp
18
+ commits_url
19
+ ref
20
+ repository_description
21
+ repository_forks
22
+ repository_homepage
23
+ repository_name
24
+ repository_owner_email
25
+ repository_owner_name
26
+ repository_pledgie
27
+ repository_private
28
+ repository_url
29
+ repository_watchers
30
+ )
31
+
6
32
  before(:each) do
7
33
  Object.send(:remove_const, "GwooksBaseSub") if Object.const_defined?("GwooksBaseSub")
8
34
  GwooksBaseSub = Class.new(Gwooks::Base)
@@ -32,33 +58,7 @@ describe "subclass of Gwooks::Base" do
32
58
  end
33
59
  end
34
60
 
35
- method_names = %w(
36
- after
37
- before
38
- branch
39
- commits_added
40
- commits_author_email
41
- commits_author_name
42
- commits_id
43
- commits_message
44
- commits_modified
45
- commits_removed
46
- commits_timestamp
47
- commits_url
48
- ref
49
- repository_description
50
- repository_forks
51
- repository_homepage
52
- repository_name
53
- repository_owner_email
54
- repository_owner_name
55
- repository_pledgie
56
- repository_private
57
- repository_url
58
- repository_watchers
59
- )
60
-
61
- method_names.each do |method_name|
61
+ dsl_methods.each do |method_name|
62
62
  key = method_name.gsub("_", ".")
63
63
 
64
64
  describe method_name do
@@ -70,7 +70,7 @@ describe "subclass of Gwooks::Base" do
70
70
  end
71
71
  end
72
72
 
73
- to_be_aliased = method_names.select do |n|
73
+ to_be_aliased = dsl_methods.select do |n|
74
74
  n.start_with? "commits_", "repository_"
75
75
  end
76
76
 
@@ -208,6 +208,36 @@ describe "subclass of Gwooks::Base" do
208
208
  end
209
209
  end
210
210
 
211
+ dsl_methods.each do |method_name|
212
+ key = method_name.gsub("_", ".")
213
+
214
+ describe method_name do
215
+ it "executes block if '#{key}' matches value" do
216
+ called = false
217
+ gwooks = GwooksBaseSub.new(
218
+ "repository" => {
219
+ "url" => "foo/bar"
220
+ }
221
+ )
222
+ gwooks.payload.should_receive(:resolve).with(key).and_return "foo"
223
+ gwooks.send method_name.to_sym, "foo", &(Proc.new { called = true })
224
+ called.should be_true
225
+ end
226
+
227
+ it "does not executes block if '#{key}' does not match value" do
228
+ called = false
229
+ gwooks = GwooksBaseSub.new(
230
+ "repository" => {
231
+ "url" => "foo/bar"
232
+ }
233
+ )
234
+ gwooks.payload.should_receive(:resolve).with(key).and_return "bar"
235
+ gwooks.send method_name.to_sym, "foo", &(Proc.new { called = true })
236
+ called.should be_false
237
+ end
238
+ end
239
+ end
240
+
211
241
  end
212
242
 
213
243
  end
metadata CHANGED
@@ -1,88 +1,92 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gwooks
3
- version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 5
10
- version: 0.0.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Luca Ongaro
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2013-01-19 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- requirement: &id001 !ruby/object:Gem::Requirement
22
- none: false
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- hash: 3
27
- segments:
28
- - 0
29
- version: "0"
30
- prerelease: false
31
- type: :runtime
11
+ date: 2013-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
32
14
  name: json
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- none: false
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- hash: 3
41
- segments:
42
- - 0
43
- version: "0"
44
- prerelease: false
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
45
20
  type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
46
28
  name: sinatra
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- requirement: &id003 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- hash: 3
55
- segments:
56
- - 0
57
- version: "0"
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
58
35
  prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
59
48
  type: :development
60
- name: rspec
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- requirement: &id004 !ruby/object:Gem::Requirement
64
- none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
72
49
  prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
73
62
  type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
74
70
  name: rack-test
75
- version_requirements: *id004
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
76
83
  description: A DSL for quickly creating endpoints for GitHub post-receive webhooks.
77
- email:
84
+ email:
78
85
  - lukeongaro@gmail.com
79
86
  executables: []
80
-
81
87
  extensions: []
82
-
83
88
  extra_rdoc_files: []
84
-
85
- files:
89
+ files:
86
90
  - .gitignore
87
91
  - Gemfile
88
92
  - LICENSE.txt
@@ -101,38 +105,30 @@ files:
101
105
  - spec/spec_helper.rb
102
106
  homepage: https://github.com/lucaong/gwooks
103
107
  licenses: []
104
-
108
+ metadata: {}
105
109
  post_install_message:
106
110
  rdoc_options: []
107
-
108
- require_paths:
111
+ require_paths:
109
112
  - lib
110
- required_ruby_version: !ruby/object:Gem::Requirement
111
- none: false
112
- requirements:
113
- - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- version: "0"
119
- required_rubygems_version: !ruby/object:Gem::Requirement
120
- none: false
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- hash: 3
125
- segments:
126
- - 0
127
- version: "0"
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
128
123
  requirements: []
129
-
130
124
  rubyforge_project:
131
- rubygems_version: 1.8.24
125
+ rubygems_version: 2.0.2
132
126
  signing_key:
133
- specification_version: 3
134
- summary: A DSL for quickly creating endpoints for GitHub post-receive webhooks. It provides methods for executing blocks of code when GitHub posts a payload matching some conditions in response to a code push.
135
- test_files:
127
+ specification_version: 4
128
+ summary: A DSL for quickly creating endpoints for GitHub post-receive webhooks. It
129
+ provides methods for executing blocks of code when GitHub posts a payload matching
130
+ some conditions in response to a code push.
131
+ test_files:
136
132
  - spec/gwooks/app_spec.rb
137
133
  - spec/gwooks/base_spec.rb
138
134
  - spec/gwooks/payload_spec.rb