Class: Slkecho::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/slkecho/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_parser:, blocks_builder:) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
# File 'lib/slkecho/cli.rb', line 5

def initialize(option_parser:, blocks_builder:)
  @option_parser = option_parser
  @blocks_builder = blocks_builder
end

Class Method Details

.run(argv) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/slkecho/cli.rb', line 47

def self.run(argv)
  cli = new(
    option_parser: Slkecho::OptionParser.new,
    blocks_builder: Slkecho::BlocksBuilder.new
  )
  cli.run(argv)
end

Instance Method Details

#blocks_from(message, user_id, message_as_blocks) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/slkecho/cli.rb', line 39

def blocks_from(message, user_id, message_as_blocks)
  if message_as_blocks
    @blocks_builder.build_from_json(message, user_id)
  else
    @blocks_builder.build_from_message(message, user_id)
  end
end

#email_to_user_id(slack_client, email) ⇒ Object



24
25
26
27
# File 'lib/slkecho/cli.rb', line 24

def email_to_user_id(slack_client, email)
  user = slack_client.lookup_user_by_email(email: email)
  user[:id]
end

#post_message_params_from(options, user_id) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/slkecho/cli.rb', line 29

def post_message_params_from(options, user_id)
  Slkecho::SlackClient::PostMessageParams.new(
    channel: options.channel,
    blocks: blocks_from(options.message, user_id, options.message_as_blocks),
    username: options.username,
    icon_url: options.icon_url,
    icon_emoji: options.icon_emoji
  )
end

#run(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/slkecho/cli.rb', line 10

def run(argv)
  options = @option_parser.parse(argv)

  if options.configure
    puts "Slkecho configuration"
  else
    slack_client = Slkecho::SlackClient.new(slack_api_token: options.token)
    user_id = options.mention_by_email.nil? ? nil : email_to_user_id(slack_client, options.mention_by_email)
    slack_client.post_message(post_message_params_from(options, user_id))

    puts "Message sent successfully."
  end
end