Class: CircuitClient::SendMessageCli

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_client/send_message_cli.rb

Defined Under Namespace

Classes: Config

Instance Method Summary collapse

Constructor Details

#initializeSendMessageCli

Returns a new instance of SendMessageCli.



27
28
29
30
31
32
33
34
# File 'lib/circuit_client/send_message_cli.rb', line 27

def initialize
  @trace = false
  @list = false
  @new = false
  @participants = []
  @config_file = '/etc/send-circuit.yaml'
  @topic = ''
end

Instance Method Details

#clientObject



129
130
131
132
133
134
135
136
137
# File 'lib/circuit_client/send_message_cli.rb', line 129

def client
  CircuitClient::Client.new do |c|
    c.host = Config.host
    c.client_id = Config.client_id
    c.client_secret = Config.client_secret
    c.trace = @trace
    c.timeout = Config.timeout
  end
end

#getoptsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/circuit_client/send_message_cli.rb', line 62

def getopts
  opts = GetoptLong.new(
    [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
    [ '--conversation', '-c', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--subject', '-s', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--topic', '-t', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--trace', GetoptLong::NO_ARGUMENT ],
    [ '--list', '-l', GetoptLong::NO_ARGUMENT ],
    [ '--new', '-n', GetoptLong::NO_ARGUMENT ],
    [ '--participant', '-p', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--config', '-f', GetoptLong::REQUIRED_ARGUMENT ],
  )
  opts.each do |opt, arg|
    case opt
      when '--help'
        usage
        exit 0
      when '--conversation'
        @conversation = arg.to_s
      when '--subject'
        @subject = arg.to_s
      when '--trace'
        @trace = true
      when '--list'
        @list = true
      when '--new'
        @new = true
      when '--participant'
        @participants << arg.to_s
      when '--config'
        @config_file = arg.to_s
      when '--topic'
        @topic = arg.to_s
    end
  end
end

#list_conversationsObject



139
140
141
142
143
# File 'lib/circuit_client/send_message_cli.rb', line 139

def list_conversations
  client.list_conversations.each do |c|
    puts "- #{c['topic']} (#{c['convId']})"
  end
end

#runObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/circuit_client/send_message_cli.rb', line 99

def run
  getopts
  Config.load_config(@config_file)

  if @list == true
    list_conversations
    exit 0
  end

  # read msg from stdin
  body = $stdin.readlines.join

  if @new == true
    puts "creating new group conversation..."
    conv = client.create_group_conversation(@participants, @topic)['convId']
  else
    conv = @conversation
  end

  options = {}
  options[:subject] = @subject unless @subject.nil?
  puts "sending message to #{conv}..."
  begin
    client.create_message( conv, body, **options )
  rescue CircuitClient::ClientError => e
    puts "Could not send message: #{e.message}"
    exit 1
  end
end

#usageObject



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
# File 'lib/circuit_client/send_message_cli.rb', line 36

def usage
  puts <<-END_USAGE
Usage: send-circuit [OPTIONS]
  --help | -h                display this help text
  --config | -c <file>       path to configuration file
                         (default: /etc/send-circuit.yaml)
  --trace                    print http debug information

List conversations:
  --list | -l                list conversations of user

Send message:
  --subject | -s <text>      Set subject for message

  --conversation | -c <id>   Id of the conversation to send a message to
  or
  --new | -n                 creates a new conversation
  --topic | -t <text>        topic of the new conversation
  --participant | -p
<email or id>            adds a participant to the conversation


The command will read the message body from stdin.
END_USAGE
end