Skip to content

Commit

Permalink
Add methods to add, remove, and fire web hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 1, 2011
1 parent 23706f3 commit 36c8c48
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ Usage Examples
puts Gems.web_hooks

# Add a webhook
Gems.add_web_hook 'rails', 'http://example.com' [TODO]
Gems.add_web_hook 'rails', 'http://example.com'

# Remove a webhook
Gems.remove_web_hook 'rails', 'http://example.com' [TODO]
Gems.remove_web_hook 'rails', 'http://example.com'

# Test fire a webhook.
Gems.fire_web_hook 'rails', 'http://example.com' [TODO]
Gems.fire_web_hook 'rails', 'http://example.com'

# Submit a gem to RubyGems.org
Gems.push File.new 'gemcutter-0.2.1.gem' [TODO]
Expand Down
66 changes: 54 additions & 12 deletions lib/gems/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,6 @@ def owners(gem)
get("/api/v1/gems/#{gem}/owners", {}, :json)
end

# List the webhooks registered under your account
#
# @return [Hashie::Mash]
# @example
# Gems.configure do |config|
# config.key = '701243f217cdf23b1370c7b66b65ca97'
# end
# Gems.web_hooks
def web_hooks
get("/api/v1/web_hooks", {}, :json)
end

# Add an owner to a RubyGem you own, giving that user permission to manage it
#
# @param gem [String] The name of a gem.
Expand Down Expand Up @@ -153,5 +141,59 @@ def add_owner(gem, owner)
def remove_owner(gem, owner)
delete("/api/v1/gems/#{gem}/owners", {:email => owner}, :raw)
end

# List the webhooks registered under your account
#
# @return [Hashie::Mash]
# @example
# Gems.configure do |config|
# config.key = '701243f217cdf23b1370c7b66b65ca97'
# end
# Gems.web_hooks
def web_hooks
get("/api/v1/web_hooks", {}, :json)
end

# Create a webhook
#
# @param gem [String] The name of a gem. Specify "*" to add the hook to all your gems.
# @param url [String] The URL of the web hook.
# @return [String]
# @example
# Gems.configure do |config|
# config.key = '701243f217cdf23b1370c7b66b65ca97'
# end
# Gems.add_web_hook("rails", "http://example.com")
def add_web_hook(gem, url)
post("/api/v1/web_hooks", {:gem_name => gem, :url => url}, :raw)
end

# Remove a webhook
#
# @param gem [String] The name of a gem. Specify "*" to remove the hook from all your gems.
# @param url [String] The URL of the web hook.
# @return [String]
# @example
# Gems.configure do |config|
# config.key = '701243f217cdf23b1370c7b66b65ca97'
# end
# Gems.remove_web_hook("rails", "http://example.com")
def remove_web_hook(gem, url)
delete("/api/v1/web_hooks/remove", {:gem_name => gem, :url => url}, :raw)
end

# Test fire a webhook
#
# @param gem [String] The name of a gem. Specify "*" to fire the hook for all your gems.
# @param url [String] The URL of the web hook.
# @return [String]
# @example
# Gems.configure do |config|
# config.key = '701243f217cdf23b1370c7b66b65ca97'
# end
# Gems.fire_web_hook("rails", "http://example.com")
def fire_web_hook(gem, url)
post("/api/v1/web_hooks/fire", {:gem_name => gem, :url => url}, :raw)
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/add_web_hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Successfully created webhook for all gems to http://example.com
1 change: 1 addition & 0 deletions spec/fixtures/fire_web_hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Successfully deployed webhook for gemcutter to http://example.com
1 change: 1 addition & 0 deletions spec/fixtures/remove_web_hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Successfully removed webhook for all gems to http://example.com
76 changes: 62 additions & 14 deletions spec/gems/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,6 @@
end
end

describe ".web_hooks" do
before do
stub_get("/api/v1/web_hooks.json").
to_return(:body => fixture("web_hooks.json"))
end

it "should list the webhooks registered under your account" do
web_hooks = Gems.web_hooks
a_get("/api/v1/web_hooks.json").
should have_been_made
web_hooks.rails.first.url.should == "http://example.com"
end
end

describe ".add_owner" do
before do
stub_post("/api/v1/gems/gems/owners").
Expand Down Expand Up @@ -211,6 +197,68 @@
owner.should == "Owner removed successfully."
end
end

describe ".web_hooks" do
before do
stub_get("/api/v1/web_hooks.json").
to_return(:body => fixture("web_hooks.json"))
end

it "should list the webhooks registered under your account" do
web_hooks = Gems.web_hooks
a_get("/api/v1/web_hooks.json").
should have_been_made
web_hooks.rails.first.url.should == "http://example.com"
end
end

describe ".add_web_hook" do
before do
stub_post("/api/v1/web_hooks").
with(:body => {:gem_name => "*", :url => "http://example.com"}).
to_return(:body => fixture("add_web_hook"))
end

it "should add a web hook" do
add_web_hook = Gems.add_web_hook("*", "http://example.com")
a_post("/api/v1/web_hooks").
with(:body => {:gem_name => "*", :url => "http://example.com"}).
should have_been_made
add_web_hook.should == "Successfully created webhook for all gems to http://example.com"
end
end

describe ".remove_web_hook" do
before do
stub_delete("/api/v1/web_hooks/remove").
with(:query => {:gem_name => "*", :url => "http://example.com"}).
to_return(:body => fixture("remove_web_hook"))
end

it "should remove a web hook" do
remove_web_hook = Gems.remove_web_hook("*", "http://example.com")
a_delete("/api/v1/web_hooks/remove").
with(:query => {:gem_name => "*", :url => "http://example.com"}).
should have_been_made
remove_web_hook.should == "Successfully removed webhook for all gems to http://example.com"
end
end

describe ".fire_web_hook" do
before do
stub_post("/api/v1/web_hooks/fire").
with(:body => {:gem_name => "*", :url => "http://example.com"}).
to_return(:body => fixture("fire_web_hook"))
end

it "should fire a web hook" do
fire_web_hook = Gems.fire_web_hook("*", "http://example.com")
a_post("/api/v1/web_hooks/fire").
with(:body => {:gem_name => "*", :url => "http://example.com"}).
should have_been_made
fire_web_hook.should == "Successfully deployed webhook for gemcutter to http://example.com"
end
end
end
end
end

0 comments on commit 36c8c48

Please sign in to comment.