Class: Gloo::Objs::Slack

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/web/slack.rb

Constant Summary collapse

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

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



114
115
116
# File 'lib/gloo/objs/web/slack.rb', line 114

def self.messages
  return super + [ 'run' ]
end

.short_typenameObject

The short name of the object type.



36
37
38
# File 'lib/gloo/objs/web/slack.rb', line 36

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



29
30
31
# File 'lib/gloo/objs/web/slack.rb', line 29

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



89
90
91
# File 'lib/gloo/objs/web/slack.rb', line 89

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



98
99
100
101
102
103
104
105
# File 'lib/gloo/objs/web/slack.rb', line 98

def add_default_children
  fac = @engine.factory
  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

#attachment_valueObject

Get the URI from the child object. Returns nil if there is none.



55
56
57
58
59
60
61
62
63
# File 'lib/gloo/objs/web/slack.rb', line 55

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 } ]
end

#body_as_jsonObject

Get all the children of the body container and convert to JSON that will be sent in the HTTP body.



69
70
71
72
73
74
75
76
77
78
# File 'lib/gloo/objs/web/slack.rb', line 69

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 = attachment_value
  h[ 'attachments' ] = o if o
  return h.to_json
end

#msg_runObject

Post the content to the Slack channel.



121
122
123
124
125
126
# File 'lib/gloo/objs/web/slack.rb', line 121

def msg_run
  uri = uri_value
  return unless uri

  Gloo::Objs::HttpPost.post_json uri, body_as_json
end

#uri_valueObject

Get the URI from the child object. Returns nil if there is none.



44
45
46
47
48
49
# File 'lib/gloo/objs/web/slack.rb', line 44

def uri_value
  uri = find_child URL
  return nil unless uri

  return uri.value
end