Module: HipTail::Util

Defined in:
lib/hiptail/util.rb

Constant Summary collapse

DEFAULT_VENDOR_NAME =
"HipTail"
DEFAULT_VENDOR_URL =
"https://github.com/dayflower/hiptail"

Class Method Summary collapse

Class Method Details

.create_capability(params) ⇒ Object

Build capability object.

Parameters:

  • params (Hash)

Options Hash (params):

  • :key (String)

    Identical key for integration

  • :name (String)

    integration name

  • :base_url (String)

    Base URL for integration

  • :capability_url (String)

    URL for capability

  • :webhook_url (String)

    URL for event webhook

  • :installed_url (String)

    URL for installed / uninstalled event webhook

  • :description (String) — default: same as :name

    integration description (optional)

  • :vendor_name (String) — default: same as :name

    Vendor name (optional)

  • :vendor_url (String) — default: same as :base_url

    Vendor URL (optional)

  • :homepage_url (String) — default: same as :base_url

    Homepage (optional)

  • :sender_name (String) — default: same as :name

    Name of notification sender (optional)

  • :avatar (String)

    Url of avatar image (optional)

  • :allow_global (String) — default: true

    Allow global installation (optional)

  • :allow_room (String) — default: true

    Allow room installation (optional)

  • :message_filter (String)

    Room message filter regexp (optional)



23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hiptail/util.rb', line 23

def create_capability(params)
  requireds = %w( key name
                  base_url capability_url webhook_url installed_url )
  missings = requireds.select { |k| ! params.include?(k.to_sym) }
  if missings.length > 0
    raise "missing parameters: " + missings.join(%q{, })
  end

  capability = {
    key: params[:key],
    name: params[:name],
    description: params[:description] || params[:name],
    vendor: {
      name: params[:vendor_name] || DEFAULT_VENDOR_NAME,
      url: (params[:vendor_url] || DEFAULT_VENDOR_URL).to_s,
    },
    links: {
      self: params[:capability_url].to_s,
      homepage: (params[:homepage_url] || params[:base_url]).to_s,
    },
    capabilities: {
      webhook: [],
      hipchatApiConsumer: {
        scopes: %w( send_notification send_message admin_room view_group ),
        fromName: params[:sender_name] || params[:name],
      },
      installable: {
        allowGlobal: params.include?(:allow_global) ? params[:allow_global] : true,
        allowRoom: params.include?(:allow_room) ? params[:allow_room] : true,
        callbackUrl: params[:installed_url].to_s,
      }
    },
  }

  event_names = %w( room_notification room_topic_change
                    room_enter room_exit )
  webhook_url = params[:webhook_url].to_s

  capability[:capabilities][:webhook] = event_names.map { |key|
      {
        name: key,
        event: key,
        url: webhook_url,
      }
  }
  message_webhook = {
    name: 'room_message',
    event: 'room_message',
    url: webhook_url,
  }
  if params[:message_filter]
    message_webhook[:pattern] = params[:message_filter].to_s
  end
  capability[:capabilities][:webhook] << message_webhook

  if params[:avatar]
    capability[:capabilities][:hipchatApiConsumer][:avatar] = params[:avatar]
  end

  capability
end