Module: SlackMessage

Defined in:
lib/slack_message.rb

Defined Under Namespace

Modules: Api, Configuration, RSpec Classes: ApiError, Dsl, ErrorHandling, Response

Constant Summary collapse

EMAIL_TAG_PATTERN =
/<[^@ \t\r\n\<]+@[^@ \t\r\n]+\.[^@ \t\r\n]+>/
EMAIL_PATTERN =
/^\S{1,}@\S{2,}\.\S{2,}$/

Class Method Summary collapse

Class Method Details

.build(profile_name = :default, &block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/slack_message.rb', line 84

def self.build(profile_name = :default, &block)
  profile = Configuration.profile(profile_name)

  Dsl.new(block, profile).tap do |instance|
    instance.instance_eval(&block)
  end.send(:render)
end

.configurationObject



13
14
15
# File 'lib/slack_message.rb', line 13

def self.configuration
  Configuration
end

.configure(&block) ⇒ Object



17
18
19
# File 'lib/slack_message.rb', line 17

def self.configure(&block)
  configuration.configure(&block)
end

.delete(message) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/slack_message.rb', line 71

def self.delete(message)
  unless message.is_a?(SlackMessage::Response)
    raise ArgumentError, "You must pass in a SlackMessage::Response to delete a message"
  end

  if message.sent_to_user?
    raise ArgumentError, "It's not possible to delete messages sent directly to users."
  end

  profile = Configuration.profile(message.profile_handle)
  Api.delete(message, profile)
end

.post_as(profile_name, at: nil, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/slack_message.rb', line 38

def self.post_as(profile_name, at: nil, &block)
  profile = Configuration.profile(profile_name)
  if profile[:default_channel].nil?
    raise ArgumentError, "Sorry, you need to specify a default_channel for profile #{profile_name} to use post_as"
  end

  target  = profile[:default_channel]
  payload = Dsl.new(block, profile).tap do |instance|
    instance.instance_eval(&block)
  end

  target  = Api::user_id_for(target, profile) if target =~ EMAIL_PATTERN

  Api.post(payload, target, profile, at)
end

.post_to(target, as: :default, at: nil, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/slack_message.rb', line 26

def self.post_to(target, as: :default, at: nil, &block)
  profile = Configuration.profile(as)

  payload = Dsl.new(block, profile).tap do |instance|
    instance.instance_eval(&block)
  end

  target  = Api::user_id_for(target, profile) if target =~ EMAIL_PATTERN

  Api.post(payload, target, profile, at)
end

.update(message, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slack_message.rb', line 54

def self.update(message, &block)
  unless message.is_a?(SlackMessage::Response)
    raise ArgumentError, "You must pass in a SlackMessage::Response to update a message"
  end

  if message.scheduled?
    raise ArgumentError, "Sorry, scheduled messages cannot be updated. You will need to delete the message and schedule a new one."
  end

  profile = Configuration.profile(message.profile_handle)
  payload = Dsl.new(block, profile).tap do |instance|
    instance.instance_eval(&block)
  end

  Api.update(payload, message, profile)
end

.user_id(email, profile_name = :default) ⇒ Object



21
22
23
24
# File 'lib/slack_message.rb', line 21

def self.user_id(email, profile_name = :default)
  profile = Configuration.profile(profile_name)
  Api.user_id_for(email, profile)
end