gwooks 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,14 +24,14 @@ Or install it yourself as:
24
24
  First extend the `Gwooks::Base` class and use the DSL to create actions to be performed in response to a push:
25
25
 
26
26
  ```ruby
27
- class MyHookHandler < Gwooks::Base
27
+ class MyActions < Gwooks::Base
28
28
 
29
29
  repository_name "my_cool_project" do
30
30
  # this block gets executed when GitHub receives
31
31
  # a push to a repo named "my_cool_project"
32
32
 
33
- # Here you can also access the payload sent by GitHub:
34
- contributors = payload["commits"].map { |c| c["author"]["name"] }
33
+ # Here you can also access the payload sent by GitHub, parsed to a hash:
34
+ contributors = payload[:commits].map {|c| c[:author][:name] }
35
35
  send_email "someone@email.com", "my_cool_project received changes by: #{ contributors.join(', ') }"
36
36
  end
37
37
 
@@ -59,7 +59,7 @@ class you created:
59
59
  require "sinatra"
60
60
 
61
61
  post "/webhook" do
62
- MyHookHandler.call(params[:payload])
62
+ MyActions.call(params[:payload])
63
63
  end
64
64
  ```
65
65
 
@@ -70,7 +70,7 @@ Alternatively, you can use the sinatra application provided by the class `Gwooks
70
70
  require "gwooks"
71
71
 
72
72
  # Tell Gwooks::App to use your class
73
- Gwooks::App.use_webhook MyHookHandler
73
+ Gwooks::App.use_webhook MyActions
74
74
 
75
75
  # Gwooks::App sets up an endpoint on POST /"
76
76
  run Gwooks::App
data/lib/gwooks/base.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "json"
1
+ require File.expand_path("payload.rb", File.dirname(__FILE__))
2
2
 
3
3
  module Gwooks
4
4
  class Base
@@ -72,20 +72,15 @@ module Gwooks
72
72
  end
73
73
 
74
74
  attr_reader :payload
75
- private :payload
76
75
 
77
76
  def initialize(payload)
78
- if payload.is_a? String
79
- @payload = JSON.parse(payload)
80
- else
81
- @payload = payload
82
- end
77
+ @payload = Gwooks::Payload.new(payload)
83
78
  end
84
79
 
85
80
  def call
86
81
  self.class.hooks.each do |hook|
87
82
  key, pattern, block = *hook
88
- target = resolve_key(key)
83
+ target = payload.resolve(key)
89
84
  if target.is_a? Array
90
85
  match = target.map do |t|
91
86
  match_pattern(t, pattern)
@@ -100,20 +95,7 @@ module Gwooks
100
95
  nil
101
96
  end
102
97
 
103
- private
104
-
105
- def resolve_key(key)
106
- key.split(".").inject(payload) do |obj, segment|
107
- break nil if obj.nil?
108
- if obj.is_a? Array
109
- obj.map do |item|
110
- item[segment]
111
- end.flatten
112
- else
113
- obj[segment]
114
- end
115
- end
116
- end
98
+ private
117
99
 
118
100
  def match_pattern(target, pattern)
119
101
  if pattern.is_a? Regexp
@@ -0,0 +1,57 @@
1
+ require "json"
2
+
3
+ module Gwooks
4
+ class Payload < Hash
5
+
6
+ class << self
7
+ alias_method :new_without_indifferent_access, :new
8
+
9
+ def new(payload)
10
+ new_hash = new_without_indifferent_access do |hash, key|
11
+ hash[key.to_s] if key.is_a? Symbol
12
+ end
13
+ payload = JSON.parse(payload) if payload.is_a? String
14
+ new_hash.update(payload)
15
+ end
16
+ end
17
+
18
+ def resolve(key)
19
+ key.split(".").inject(self) do |obj, segment|
20
+ break nil if obj.nil?
21
+ if obj.is_a? Array
22
+ obj.map do |item|
23
+ item[segment]
24
+ end.flatten
25
+ else
26
+ obj[segment]
27
+ end
28
+ end
29
+ end
30
+
31
+ alias_method :update_without_indifferent_access, :update
32
+ def update(hash)
33
+ hash.each do |key, obj|
34
+ self[key] = make_indifferent(obj)
35
+ end
36
+ self
37
+ end
38
+
39
+ private
40
+
41
+ def make_indifferent(obj)
42
+ case obj
43
+ when Hash
44
+ new_hash = Hash.new(&self.default_proc)
45
+ obj.each do |key, val|
46
+ new_hash[key] = make_indifferent(val)
47
+ end
48
+ new_hash
49
+ when Array
50
+ obj.map {|item| make_indifferent(item)}
51
+ else
52
+ obj
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module Gwooks
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -8,6 +8,13 @@ describe "subclass of Gwooks::Base" do
8
8
  GwooksBaseSub = Class.new(Gwooks::Base)
9
9
  end
10
10
 
11
+ describe :new do
12
+ it "creates and store a new instance of Gwooks::Payload" do
13
+ gwooks = GwooksBaseSub.new({"foo" => "bar"})
14
+ gwooks.payload.should == {"foo" => "bar"}
15
+ end
16
+ end
17
+
11
18
  describe :call do
12
19
  it "creates a new instance passing payload and then invokes call()" do
13
20
  gwooks = double GwooksBaseSub.new("{}")
@@ -83,19 +90,7 @@ describe "subclass of Gwooks::Base" do
83
90
  end
84
91
  end
85
92
 
86
- describe "instance" do
87
-
88
- describe :new do
89
- it "initializes payload parsing the argument as JSON if it is a string" do
90
- gwooks = GwooksBaseSub.new("{\"foo\": \"bar\"}")
91
- gwooks.send(:payload).should == {"foo" => "bar"}
92
- end
93
-
94
- it "initializes payload using the argument if it is not a string" do
95
- gwooks = GwooksBaseSub.new({"foo" => "bar"})
96
- gwooks.send(:payload).should == {"foo" => "bar"}
97
- end
98
- end
93
+ describe "instance" do
99
94
 
100
95
  describe :call do
101
96
  it "executes all matching hooks" do
@@ -212,64 +207,6 @@ describe "subclass of Gwooks::Base" do
212
207
  end
213
208
  end
214
209
 
215
- describe :resolve_key do
216
- it "returns nested object according to the key" do
217
- GwooksBaseSub.new(
218
- "foo" => {
219
- "bar" => {
220
- "baz" => "qux"
221
- }
222
- }
223
- ).send(:resolve_key, "foo.bar.baz").should == "qux"
224
- end
225
-
226
- it "returns nil if nested object does not exist" do
227
- GwooksBaseSub.new(
228
- "foo" => {
229
- "bar" => {}
230
- }
231
- ).send(:resolve_key, "foo.bar.baz").should == nil
232
- end
233
-
234
- it "returns nil if parent object does not exist" do
235
- GwooksBaseSub.new(
236
- "foo" => {}
237
- ).send(:resolve_key, "foo.bar.baz").should == nil
238
- end
239
-
240
- it "returns all items in array when item is an array" do
241
- GwooksBaseSub.new(
242
- "foo" => ["a", "b", "c"]
243
- ).send(:resolve_key, "foo").should == ["a", "b", "c"]
244
- end
245
-
246
- it "resolve key in array" do
247
- GwooksBaseSub.new(
248
- "foo" => [
249
- { "bar" => 123 },
250
- { "bar" => 321 }
251
- ]
252
- ).send(:resolve_key, "foo.bar").should == [123, 321]
253
- end
254
-
255
- it "resolve key in nested arrays" do
256
- GwooksBaseSub.new(
257
- "foo" => [
258
- { "bar" => [
259
- {"baz" => 123},
260
- {"baz" => 321}
261
- ]},
262
- { "bar" => [
263
- {"baz" => 312},
264
- {"baz" => 132}
265
- ]}
266
- ]
267
- ).send(:resolve_key, "foo.bar.baz").should == [123, 321, 312, 132]
268
- end
269
-
270
-
271
- end
272
-
273
210
  end
274
211
 
275
212
  end
@@ -0,0 +1,82 @@
1
+ require File.expand_path("../spec_helper.rb", File.dirname(__FILE__))
2
+ require File.expand_path("../../lib/gwooks/payload.rb", File.dirname(__FILE__))
3
+
4
+ describe Gwooks::Payload do
5
+ describe "class methods" do
6
+ describe :new do
7
+ it "initializes parsing the argument as JSON if it is a string" do
8
+ payload = Gwooks::Payload.new("{\"foo\": \"bar\"}")
9
+ payload.should == {"foo" => "bar"}
10
+ end
11
+
12
+ it "initializes using the argument if it is not a string" do
13
+ payload = Gwooks::Payload.new({"foo" => "bar"})
14
+ payload.should == {"foo" => "bar"}
15
+ end
16
+ end
17
+ end
18
+
19
+ describe :[] do
20
+ it "provides indifferent access" do
21
+ payload = Gwooks::Payload.new("foo" => {"bar" => "baz"})
22
+ payload[:foo][:bar].should == "baz"
23
+ payload["foo"]["bar"].should == "baz"
24
+ end
25
+ end
26
+
27
+ describe :resolve do
28
+ it "returns nested object according to the key" do
29
+ Gwooks::Payload.new(
30
+ "foo" => {
31
+ "bar" => {
32
+ "baz" => "qux"
33
+ }
34
+ }
35
+ ).resolve("foo.bar.baz").should == "qux"
36
+ end
37
+
38
+ it "returns nil if nested property does not exist" do
39
+ Gwooks::Payload.new(
40
+ "foo" => {
41
+ "bar" => {}
42
+ }
43
+ ).resolve("foo.bar.baz").should == nil
44
+ end
45
+
46
+ it "returns nil if parent property does not exist" do
47
+ Gwooks::Payload.new(
48
+ "foo" => {}
49
+ ).resolve("foo.bar.baz").should == nil
50
+ end
51
+
52
+ it "returns all items in array when property is an array" do
53
+ Gwooks::Payload.new(
54
+ "foo" => ["a", "b", "c"]
55
+ ).resolve("foo").should == ["a", "b", "c"]
56
+ end
57
+
58
+ it "resolve key nested in an array" do
59
+ Gwooks::Payload.new(
60
+ "foo" => [
61
+ { "bar" => 123 },
62
+ { "bar" => 321 }
63
+ ]
64
+ ).resolve("foo.bar").should == [123, 321]
65
+ end
66
+
67
+ it "resolve key nested in multiple arrays" do
68
+ Gwooks::Payload.new(
69
+ "foo" => [
70
+ { "bar" => [
71
+ {"baz" => 123},
72
+ {"baz" => 321}
73
+ ]},
74
+ { "bar" => [
75
+ {"baz" => 312},
76
+ {"baz" => 132}
77
+ ]}
78
+ ]
79
+ ).resolve("foo.bar.baz").should == [123, 321, 312, 132]
80
+ end
81
+ end
82
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gwooks
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Luca Ongaro
@@ -93,9 +93,11 @@ files:
93
93
  - lib/gwooks.rb
94
94
  - lib/gwooks/app.rb
95
95
  - lib/gwooks/base.rb
96
+ - lib/gwooks/payload.rb
96
97
  - lib/gwooks/version.rb
97
98
  - spec/gwooks/app_spec.rb
98
99
  - spec/gwooks/base_spec.rb
100
+ - spec/gwooks/payload_spec.rb
99
101
  - spec/integration/integration_spec.rb
100
102
  - spec/spec_helper.rb
101
103
  has_rdoc: true
@@ -135,5 +137,6 @@ summary: A DSL for quickly creating endpoints for GitHub post-receive webhooks.
135
137
  test_files:
136
138
  - spec/gwooks/app_spec.rb
137
139
  - spec/gwooks/base_spec.rb
140
+ - spec/gwooks/payload_spec.rb
138
141
  - spec/integration/integration_spec.rb
139
142
  - spec/spec_helper.rb