Gem::Commands::WebhookCommand

Public Class Methods

new() click to toggle source
# File lib/commands/webhook.rb, line 19
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, options|
    options[:send] = 'add'
    options[:url] = value
  end

  add_option('-r', '--remove URL', "#{option_text} remove") do |value, options|
    options[:send] = 'remove'
    options[:url] = value
  end

  add_option('-f', '--fire URL', "#{option_text} testfire") do |value, options|
    options[:send] = 'fire'
    options[:url] = value
  end

  add_option('-g', '--global', "Apply hook globally") do |value, options|
    options[:global] = true
  end

  add_proxy_option
end

Public Instance Methods

add_webhook(name, url) click to toggle source
# File lib/commands/webhook.rb, line 56
def add_webhook(name, url)
  say "Adding webhook..."
  make_webhook_request(:post, name, url)
end
arguments() click to toggle source
# File lib/commands/webhook.rb, line 11
def arguments
  "GEM_NAME       name of gem to register webhook for, or omit to list hooks."
end
description() click to toggle source
# File lib/commands/webhook.rb, line 3
def description
  Webhooks can be created for either specific gems or all gems. In both casesyou'll get a POST request of the gem in JSON format at the URL you specify inthe command. You can also use this command to test fire a webhook for any gem.
end
execute() click to toggle source
# File lib/commands/webhook.rb, line 45
def execute
  setup

  if options[:url]
    name = options[:global] ? '*' : get_one_gem_name
    send("#{options[:send]}_webhook", name, options[:url])
  else
    list_webhooks
  end
end
fire_webhook(name, url) click to toggle source
# File lib/commands/webhook.rb, line 66
def fire_webhook(name, url)
  say "Test firing webhook..."
  make_webhook_request(:post, name, url, "web_hooks/fire")
end
list_webhooks() click to toggle source
# File lib/commands/webhook.rb, line 71
def list_webhooks
  require 'json/pure' unless defined?(JSON::JSON_LOADED)

  response = make_request(:get, "web_hooks") do |request|
    request.add_field("Authorization", api_key)
  end

  case response
  when Net::HTTPSuccess
    begin
      groups = JSON.parse(response.body)

      if groups.size.zero?
        say "You haven't added any webhooks yet."
      else
        groups.each do |group, hooks|
          if options[:global]
            next if group != "all gems"
          elsif options[:args] && options[:args].first
            next if group != options[:args].first
          end

          say "#{group}:"
          hooks.each do |hook|
            say "- #{hook['url']}"
          end
        end
      end
    rescue JSON::ParserError => json_error
      say "There was a problem parsing the data:"
      say json_error.to_s
      terminate_interaction
    end
  else
    say response.body
    terminate_interaction
  end
end
make_webhook_request(method, name, url, api = "web_hooks") click to toggle source
# File lib/commands/webhook.rb, line 110
def make_webhook_request(method, name, url, api = "web_hooks")
  response = make_request(method, api) do |request|
    request.set_form_data("gem_name" => name, "url" => url)
    request.add_field("Authorization", api_key)
  end

  case response
  when Net::HTTPSuccess
    say response.body
  else
    say response.body
    terminate_interaction
  end
end
remove_webhook(name, url) click to toggle source
# File lib/commands/webhook.rb, line 61
def remove_webhook(name, url)
  say "Removing webhook..."
  make_webhook_request(:delete, name, url, "web_hooks/remove")
end
usage() click to toggle source
# File lib/commands/webhook.rb, line 15
def usage
  "#{program_name} [GEM_NAME]"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.