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)


117
118
119
# File 'lib/talknote/cli.rb', line 117

def exit_on_failure?
  true
end

Instance Method Details

#dmObject



112
113
114
# File 'lib/talknote/cli.rb', line 112

def dm
  pp Talknote::Client.new.dm
end

#initObject



15
16
17
18
19
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
# File 'lib/talknote/cli.rb', line 15

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 = %w[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 "Go to URL: #{url}"
  puts ''

  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

    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"
    unless Dir.exists?(config_path)
      Dir.mkdir(config_path)
    end

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

    server.shutdown
  end

  trap('INT') do
    server.shutdown
  end

  server.start
end