Class: Gem::Commands::WebhookCommand
- Inherits:
-
Gem::Command
- Object
- Gem::Command
- Gem::Commands::WebhookCommand
- Includes:
- GemcutterUtilities, LocalRemoteOptions
- Defined in:
- lib/rubygems/commands/webhook_command.rb
Instance Method Summary collapse
- #add_webhook(name, url) ⇒ Object
- #arguments ⇒ Object
- #description ⇒ Object
- #execute ⇒ Object
- #fire_webhook(name, url) ⇒ Object
-
#initialize ⇒ WebhookCommand
constructor
A new instance of WebhookCommand.
- #list_webhooks ⇒ Object
- #make_webhook_request(method, name, url, api = "api/v1/web_hooks") ⇒ Object
- #remove_webhook(name, url) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize ⇒ WebhookCommand
Returns a new instance of WebhookCommand.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rubygems/commands/webhook_command.rb', line 24 def initialize super 'webhook', "Register a webhook that will be called any time a gem is updated on Gemcutter." option_text = "The URL of the webhook to" add_option('-a', '--add URL', "#{option_text} add") do |value, | [:send] = 'add' [:url] = value end add_option('-r', '--remove URL', "#{option_text} remove") do |value, | [:send] = 'remove' [:url] = value end add_option('-f', '--fire URL', "#{option_text} testfire") do |value, | [:send] = 'fire' [:url] = value end add_option('-g', '--global', "Apply hook globally") do |value, | [:global] = true end add_proxy_option end |
Instance Method Details
#add_webhook(name, url) ⇒ Object
61 62 63 64 |
# File 'lib/rubygems/commands/webhook_command.rb', line 61 def add_webhook(name, url) say "Adding webhook..." make_webhook_request(:post, name, url) end |
#arguments ⇒ Object
16 17 18 |
# File 'lib/rubygems/commands/webhook_command.rb', line 16 def arguments "GEM_NAME name of gem to register webhook for, or omit to list hooks." end |
#description ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/rubygems/commands/webhook_command.rb', line 8 def description "Webhooks can be created for either specific gems or all gems. In both cases\nyou'll get a POST request of the gem in JSON format at the URL you specify in\nthe command. You can also use this command to test fire a webhook for any gem.\n" end |
#execute ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rubygems/commands/webhook_command.rb', line 50 def execute sign_in if [:url] name = [:global] ? '*' : get_one_gem_name send("#{options[:send]}_webhook", name, [:url]) else list_webhooks end end |
#fire_webhook(name, url) ⇒ Object
71 72 73 74 |
# File 'lib/rubygems/commands/webhook_command.rb', line 71 def fire_webhook(name, url) say "Test firing webhook..." make_webhook_request(:post, name, url, "api/v1/web_hooks/fire") end |
#list_webhooks ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rubygems/commands/webhook_command.rb', line 76 def list_webhooks resp = rubygems_api_request(:get, "api/v1/web_hooks.yaml") do |request| request.add_field("Authorization", Gem.configuration.rubygems_api_key) end with_response(resp) do |response| begin groups = YAML.load(response.body) if groups.size.zero? say "You haven't added any webhooks yet." else groups.each do |group, hooks| if [:global] next if group != "all gems" elsif [:args] && [:args].first next if group != [:args].first end say "#{group}:" hooks.each do |hook| say "- #{hook['url']}" end end end rescue Exception => error say "There was a problem parsing the data:" say error.to_s terminate_interaction end end end |
#make_webhook_request(method, name, url, api = "api/v1/web_hooks") ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/rubygems/commands/webhook_command.rb', line 109 def make_webhook_request(method, name, url, api = "api/v1/web_hooks") response = rubygems_api_request(method, api) do |request| request.set_form_data("gem_name" => name, "url" => url) request.add_field("Authorization", Gem.configuration.rubygems_api_key) end with_response(response) end |
#remove_webhook(name, url) ⇒ Object
66 67 68 69 |
# File 'lib/rubygems/commands/webhook_command.rb', line 66 def remove_webhook(name, url) say "Removing webhook..." make_webhook_request(:delete, name, url, "api/v1/web_hooks/remove") end |
#usage ⇒ Object
20 21 22 |
# File 'lib/rubygems/commands/webhook_command.rb', line 20 def usage "#{program_name} [GEM_NAME]" end |