Class: Talknote::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/talknote/cli.rb', line 222

def exit_on_failure?
  true
end

Instance Method Details

#dmObject



150
151
152
153
154
155
# File 'lib/talknote/cli.rb', line 150

def dm
  pp Talknote::Client.new.dm
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#dm_list(id) ⇒ Object



158
159
160
161
162
163
# File 'lib/talknote/cli.rb', line 158

def dm_list(id)
  pp Talknote::Client.new.dm_list(id)
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#dm_post(id, message) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/talknote/cli.rb', line 174

def dm_post(id, message)
  result = Talknote::Client.new.dm_post(id, message)
  puts "Message sent successfully!"
  pp result
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#dm_unread(id) ⇒ Object



166
167
168
169
170
171
# File 'lib/talknote/cli.rb', line 166

def dm_unread(id)
  pp Talknote::Client.new.dm_unread(id)
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#groupObject



186
187
188
189
190
191
# File 'lib/talknote/cli.rb', line 186

def group
  pp Talknote::Client.new.group
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#group_list(id) ⇒ Object



194
195
196
197
198
199
# File 'lib/talknote/cli.rb', line 194

def group_list(id)
  pp Talknote::Client.new.group_list(id)
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#group_post(id, message) ⇒ Object



210
211
212
213
214
215
216
217
# File 'lib/talknote/cli.rb', line 210

def group_post(id, message)
  result = Talknote::Client.new.group_post(id, message)
  puts "Message sent successfully!"
  pp result
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#group_unread(id) ⇒ Object



202
203
204
205
206
207
# File 'lib/talknote/cli.rb', line 202

def group_unread(id)
  pp Talknote::Client.new.group_unread(id)
rescue Talknote::Error => e
  puts "Error: #{e.message}"
  exit 1
end

#initObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
61
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
98
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/talknote/cli.rb', line 20

def init
  state = ('a'..'z').to_a.sample(32).join
  path = '/oauth/callback'

  client = OAuth2::Client.new(
    options['id'],
    options['secret'],
    site: 'https://oauth.talknote.com',
    authorize_url: '/oauth/authorize',
    token_url: 'oauth/token'
  )

  redirect_uri = "http://#{options['host']}:#{options['port']}#{path}"
  scope = [
    'talknote.timeline.read',
    'talknote.timeline.write',
    'talknote.timeline.message.read',
    'talknote.timeline.message.write',
    'talknote.timeline.unread',
    'talknote.group',
    'talknote.group.read',
    'talknote.group.write',
    'talknote.group.unread',
    'talknote.group.message.read',
    'talknote.group.message.write',
    'talknote.direct_message',
    'talknote.direct_message.read',
    'talknote.direct_message.write',
    'talknote.direct_message.unread',
    'talknote.direct_message.message.read',
    'talknote.direct_message.message.write',
    'talknote.user.read',
    'talknote.user.write',
    'talknote.allfeed.read',
    'talknote.allfeed.unread'
  ].join(' ')

  code_args = {
    redirect_uri: redirect_uri,
    scope: scope,
    state: state
  }

  url = client.auth_code.authorize_url(code_args)

  puts ''
  puts "Authorization URL: #{url}"
  puts ''

  # Automatically open the URL in the default browser
  begin
    if RUBY_PLATFORM =~ /darwin/  # macOS
      system("open '#{url}'")
      puts "Opening browser automatically..."
    elsif RUBY_PLATFORM =~ /linux/
      system("xdg-open '#{url}'")
      puts "Opening browser automatically..."
    elsif RUBY_PLATFORM =~ /win32|win64|\.NET|windows|cygwin|mingw32/i
      system("start '#{url}'")
      puts "Opening browser automatically..."
    else
      puts "Please manually open the URL above in your browser."
    end
  rescue => e
    puts "Could not open browser automatically: #{e.message}"
    puts "Please manually open the URL above in your browser."
  end

  puts 'Starting server - use Ctrl+C to stop'
  puts ''

  server_options = {
    Port: options['port']
  }

  server = WEBrick::HTTPServer.new(server_options)

  server.mount_proc('/') do |req, res|
    unless req.path == path
      res.status = 403
      res.body = "Invalid callback path - expecting #{path}"
      next
    end

    unless req.query['state'] == state
      res.status = 400
      res.body = 'Invalid state in callback'
      next
    end

    begin
      token = client.auth_code.get_token(
        req.query['code'],
        grant_type: 'authorization_code',
        redirect_uri: redirect_uri
      )

      pp token.to_hash
      puts ''

      config_path = "#{Dir.home}/.config/talknote"
      FileUtils.mkdir_p(config_path) unless Dir.exist?(config_path)

      File.write("#{config_path}/token.json", token.to_hash.to_json)
      res.status = 200
      res.body = 'You may now close this tab'

      server.shutdown
    rescue OAuth2::Error => e
      puts "OAuth2 Error: #{e.message}"
      puts "Error Code: #{e.code}" if e.respond_to?(:code)
      puts "Error Description: #{e.description}" if e.respond_to?(:description)
      res.status = 400
      res.body = "OAuth Error: #{e.message}"
    rescue => e
      puts "General Error: #{e.message}"
      puts e.backtrace.join("\n")
      res.status = 500
      res.body = "Server Error: #{e.message}"
    end
  end

  trap('INT') do
    server.shutdown
  end

  server.start
end