lib/gloo/objs/web/slack.rb in gloo-0.3.0 vs lib/gloo/objs/web/slack.rb in gloo-0.4.0

- old
+ new

@@ -8,119 +8,150 @@ require 'json' module Gloo module Objs class Slack < Gloo::Core::Obj - - KEYWORD = 'slack' - KEYWORD_SHORT = 'slack' - URL = 'uri' - MSG = 'message' - USER = 'username' - CHANNEL = 'channel' - ICON = 'icon_emoji' - ATTACHMENT = 'attachment' - TITLE = 'title' - TEXT = 'text' + KEYWORD = 'slack'.freeze + KEYWORD_SHORT = 'slack'.freeze + URL = 'uri'.freeze + MSG = 'message'.freeze + USER = 'username'.freeze + CHANNEL = 'channel'.freeze + ICON = 'icon_emoji'.freeze - # + ATTACHMENT = 'attachment'.freeze + TITLE = 'title'.freeze + TEXT = 'text'.freeze + + # # The name of the object type. - # + # def self.typename return KEYWORD end - # + # # The short name of the object type. - # + # def self.short_typename return KEYWORD_SHORT end - - # + + # # Get the URI from the child object. # Returns nil if there is none. - # - def get_uri + # + def uri_value uri = find_child URL return nil unless uri + return uri.value end - # + # # Get the URI from the child object. # Returns nil if there is none. - # - def get_attachment + # + def attachment_value o = find_child ATTACHMENT return nil unless o - - title = o.find_child TITLE - text = o.find_child TEXT - return [{ 'title' => title.value, 'text' => text.value }] + + title = o.find_child TITLE + text = o.find_child TEXT + return [ { 'title' => title.value, + 'text' => text.value } ] end - - # - # Get all the children of the body container and + + # + # Get all the children of the body container and # convert to JSON that will be sent in the HTTP body. - # - def get_body_as_json + # + def body_as_json h = { 'text' => find_child( MSG ).value, - 'username' => find_child( USER ).value, - 'channel' => find_child( CHANNEL ).value, - 'icon_emoji' => find_child( ICON ).value, - } - - o = get_attachment - h[ 'attachments' ] = o if o + 'username' => find_child( USER ).value, + 'channel' => find_child( CHANNEL ).value, + 'icon_emoji' => find_child( ICON ).value } + + o = attachment_value + h[ 'attachments' ] = o if o return h.to_json end - # --------------------------------------------------------------------- # Children # --------------------------------------------------------------------- # Does this object have children to add when an object # is created in interactive mode? # This does not apply during obj load, etc. def add_children_on_create? return true end - + # Add children to this object. - # This is used by containers to add children needed + # This is used by containers to add children needed # for default configurations. def add_default_children fac = $engine.factory - fac.create URL, "string", "https://hooks.slack.com/services/...", self - fac.create MSG, "string", "textual message", self - fac.create USER, "string", "Slack Bot", self - fac.create CHANNEL, "string", "general", self - fac.create ICON, "string", ":ghost:", self + fac.create_string URL, 'https://hooks.slack.com/services/...', self + fac.create_string MSG, 'textual message', self + fac.create_string USER, 'Slack Bot', self + fac.create_string CHANNEL, 'general', self + fac.create_string ICON, ':ghost:', self end - # --------------------------------------------------------------------- # Messages # --------------------------------------------------------------------- - # + # # Get a list of message names that this object receives. - # + # def self.messages - return super + [ "run" ] + return super + [ 'run' ] end - + # Post the content to the endpoint. def msg_run - uri = get_uri + uri = uri_value return unless uri - - body = get_body_as_json - Gloo::Objs::HttpPost.post_json uri, body, true + + Gloo::Objs::HttpPost.post_json uri, body_as_json, true end - + + # --------------------------------------------------------------------- + # Help + # --------------------------------------------------------------------- + + # + # Get help for this object type. + # + def self.help + return <<~TEXT + SLACK OBJECT TYPE + NAME: slack + SHORTCUT: slack + + DESCRIPTION + Send message to channel in Slack. + + CHILDREN + uri - string - 'https://hooks.slack.com/services/...' + The URI with access to the Slack channel. + message - string - 'textual message' + Message to send to Slack. + username - string - 'Slack Bot' + Attribute the message to this user. + channel - string - 'general' + The name of the channel for the post. + icon_emoji - string - ':ghost:' + The emoji to use for the attribution. + + MESSAGES + run - Post the message to Slack. + TEXT + end + end end end