Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
Noah
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Noah
Commits
fec88421
Commit
fec88421
authored
Mar 11, 2011
by
John E. Vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished watcher API and tests
parent
c0f73265
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
3 deletions
+132
-3
ephemeral_routes.rb
lib/noah/ephemeral_routes.rb
+0
-1
watchers.rb
lib/noah/models/watchers.rb
+9
-1
watcher_routes.rb
lib/noah/watcher_routes.rb
+34
-1
noahapp_watcher_spec.rb
spec/noahapp_watcher_spec.rb
+89
-0
No files found.
lib/noah/ephemeral_routes.rb
View file @
fec88421
class
Noah
::
App
# Stubbing Ephemeral endpoints
get
'/e/?'
do
halt
404
end
...
...
lib/noah/models/watchers.rb
View file @
fec88421
...
...
@@ -7,6 +7,7 @@ module Noah
attribute
:pattern
attribute
:endpoint
attribute
:name
index
:pattern
index
:endpoint
...
...
@@ -29,12 +30,19 @@ module Noah
super
.
merge
(
h
)
end
def
self
.
watch_list
class
<<
self
def
find_by_name
(
name
)
pattern
,
endpoint
=
Base64
.
decode64
(
name
).
split
(
'|'
)
find
(
:pattern
=>
pattern
,
:endpoint
=>
endpoint
).
first
end
def
watch_list
arr
=
[]
watches
=
self
.
all
.
sort_by
(
:pattern
)
watches
.
each
{
|
w
|
arr
<<
w
.
name
}
arr
end
end
private
# Not sure about these next two.
...
...
lib/noah/watcher_routes.rb
View file @
fec88421
class
Noah
::
App
get
'/w/:name'
do
|
name
|
w
=
Noah
::
Watcher
.
find_by_name
(
name
)
w
.
nil?
?
(
halt
404
)
:
w
.
to_json
end
get
'/w/?'
do
w
=
Noah
::
Watcher
.
all
.
sort_by
(
:pattern
)
if
w
.
size
==
0
...
...
@@ -9,4 +14,32 @@ class Noah::App
end
end
put
'/w/?'
do
required_params
=
%w[endpoint pattern]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
pattern
,
endpoint
=
data
[
'pattern'
],
data
[
'endpoint'
])
:
(
raise
"Missing Parameters"
)
w
=
Noah
::
Watcher
.
create
(
:pattern
=>
pattern
,
:endpoint
=>
endpoint
)
if
w
.
valid?
w
.
save
r
=
{
"action"
=>
"create"
,
"result"
=>
"success"
}.
merge
(
w
.
to_hash
)
r
.
to_json
else
raise
"
#{
format_errors
(
w
)
}
"
end
end
delete
'/w/?'
do
required_params
=
%w[endpoint pattern]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
pattern
,
endpoint
=
data
[
'pattern'
],
data
[
'endpoint'
])
:
(
raise
"Missing Parameters"
)
w
=
Noah
::
Watcher
.
find
(
:pattern
=>
pattern
,
:endpoint
=>
endpoint
).
first
if
w
.
nil?
halt
404
else
w
.
delete
r
=
{
"result"
=>
"success"
,
"action"
=>
"delete"
}.
merge
(
w
.
to_hash
)
r
.
to_json
end
end
end
spec/noahapp_watcher_spec.rb
View file @
fec88421
...
...
@@ -28,7 +28,96 @@ describe "Using the Watcher API", :reset_redis => true do
response
.
is_a?
(
Array
).
should
==
true
response
.
size
.
should
==
4
end
it
"named watch should work"
do
w
=
Noah
::
Watcher
.
create
(
:pattern
=>
'//noah/application/myapp'
,
:endpoint
=>
'http://localhost/webhook'
)
get
"/w/
#{
w
.
name
}
"
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
'pattern'
].
should
==
w
.
pattern
response
[
'endpoint'
].
should
==
w
.
endpoint
end
it
"invalid watch should not work"
do
get
'/w/asdfasdfasdfasdfasdfsdf'
last_response
.
should
be_missing
end
end
describe
"PUT"
do
it
"new watch should work"
do
data
=
{
:pattern
=>
"//noah/application"
,
:endpoint
=>
"http://myendpoint/webhook"
}
put
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
'pattern'
].
should
==
data
[
:pattern
]
response
[
'endpoint'
].
should
==
data
[
:endpoint
]
w
=
Noah
::
Watcher
.
find
(
data
).
first
w
.
pattern
.
should
==
data
[
:pattern
]
w
.
endpoint
.
should
==
data
[
:endpoint
]
end
it
"new watch without pattern should not work"
do
data
=
{
:endpoint
=>
"http://myendpoint/webhook"
}
put
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_invalid
end
it
"new watch without endpoint should not work"
do
data
=
{
:pattern
=>
"//noah/application"
}
put
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_invalid
end
it
"new watch that supercedes existing should not work"
do
Noah
::
Watcher
.
create
(
:endpoint
=>
'http://myendpoint/webhook'
,
:pattern
=>
'//noah/application/foo'
)
data
=
{
:endpoint
=>
"http://myendpoint/webhook"
,
:pattern
=>
'//noah/application'
}
put
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should_not
be_ok
response
=
last_response
.
should
return_json
response
[
'error_message'
].
should
==
'Pattern would overwrite existing'
end
it
"new watch that subsets an existing should not work"
do
Noah
::
Watcher
.
create
(
:endpoint
=>
'http://myendpoint/webhook'
,
:pattern
=>
'//noah/application'
)
data
=
{
:endpoint
=>
"http://myendpoint/webhook"
,
:pattern
=>
'//noah/application/foo'
}
put
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should_not
be_ok
response
=
last_response
.
should
return_json
response
[
'error_message'
].
should
==
'Pattern is already provided'
end
end
describe
"DELETE"
do
it
"delete an existing watch should work"
do
data
=
{
:endpoint
=>
"http://myendpoint/webhookd"
,
:pattern
=>
'//noah/application/d'
}
w
=
Noah
::
Watcher
.
create
(
data
)
delete
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
'pattern'
].
should
==
data
[
:pattern
]
response
[
'endpoint'
].
should
==
data
[
:endpoint
]
response
[
'name'
].
should_not
==
"null"
Noah
::
Watcher
.
find
(
data
).
size
.
should
==
0
end
it
"delete an invalid watch should not work"
do
data
=
{
:endpoint
=>
'missing'
,
:pattern
=>
'//noah/application/dag'
}
delete
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_missing
end
it
"delete without pattern should not work"
do
data
=
{
:endpoint
=>
"invalid"
}
delete
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_invalid
end
it
"delete without endpoint should not work"
do
data
=
{
:pattern
=>
"//noah/invalid"
}
delete
'/w'
,
data
.
to_json
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_invalid
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment