Class: Kronk::Cmd::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/kronk/cmd/oauth.rb

Constant Summary collapse

TWITTER_HOST =
'api.twitter.com'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ OAuth

Returns a new instance of OAuth.



114
115
116
117
# File 'lib/kronk/cmd/oauth.rb', line 114

def initialize file
  @file = file
  @config = File.file?(@file) ? Kronk::OAuthConfig.load_file( @file ) : Kronk::OAuthConfig.new
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



112
113
114
# File 'lib/kronk/cmd/oauth.rb', line 112

def file
  @file
end

Class Method Details

.parse_args(argv) ⇒ Object

Parse ARGV into options and Kronk config.



11
12
13
14
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
# File 'lib/kronk/cmd/oauth.rb', line 11

def self.parse_args argv
  options = {}

  opts = OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = Kronk::VERSION
    opt.release = nil

    opt.banner = <<-STR

#{opt.program_name} #{opt.version}

Manage OAuth configs for kronk.

  Usage:
    #{opt.program_name} --help
    #{opt.program_name} --version
    #{opt.program_name} [options]

  Examples:
    #{opt.program_name} --list
    #{opt.program_name} --list api.twitter.com
    #{opt.program_name} --add api.twitter.com
    #{opt.program_name} --remove [email protected]

    Option targets may be a in the form of a host or user@host.

  Options:
        STR

    opt.on('-l', '--list [TARGET]', 'List OAuth configs for an optional target') do |target|
      return :list_config, *parse_target(target)
    end

    opt.on('-a', '--add TARGET', 'Add a new OAuth config for a given target') do |target|
      return :add_config, *parse_target(target)
    end

    opt.on('-r', '--remove TARGET', 'Remove an OAuth config for a given target') do |target|
      return :remove_config, *parse_target(target)
    end

    opt.on('-s', '--select TARGET', 'Set default OAuth config for a given target') do |target|
      return :select_config, *parse_target(target)
    end

    opt.on('-d', '--disable HOST', 'Stop using OAuth config for a given host') do |host|
      return :disable_config, host
    end

    opt.on('-n', '--rename TARGET', 'Rename an OAuth config for a given host') do |target|
      return :rename_config, *parse_target(target)
    end

    opt.on('--twurl [FILE]', 'Import twurl configs') do |file|
      return :import_twurl, file
    end

    opt.on('-h', '--help', 'Print this help screen') do
      puts opt
      exit
    end

    opt.on('-v', '--version', 'Output Kronk version and exit') do
      puts Kronk::VERSION
      exit
    end

    opt.separator nil
  end

  opts.parse! argv

  puts opts.to_s
  exit 0

rescue OptionParser::ParseError => e
  $stderr.puts("\nError: #{e.message}")
  $stderr.puts("See 'kronk-oauth --help' for usage\n\n")
  exit 1
end

.parse_target(target) ⇒ Object



94
95
96
97
# File 'lib/kronk/cmd/oauth.rb', line 94

def self.parse_target target
  return unless target
  target.split("@", 2).reverse
end

.run(argv = ARGV) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/kronk/cmd/oauth.rb', line 100

def self.run argv=ARGV
   trap 'INT' do
     puts "\n"
     exit 2
   end

   Kronk::Cmd.load_config_file

   new(Kronk::DEFAULT_OAUTH_FILE).send(*parse_args(argv))
end

Instance Method Details

#add_config(host, name = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/kronk/cmd/oauth.rb', line 191

def add_config host, name=nil
  name ||= query_name(host)
  name = validate_name(name, host)

  config = {
    'consumer_key'    => query("Consumer Key: "),
    'consumer_secret' => query("Consumer Secret: "),
    'token'           => query("Token: "),
    'token_secret'    => query("Token Secret: ")
  }

  @config.set(name, host, config)

  save_file!

  $stderr.puts("Added config for #{name}@#{host}")
end

#add_twurl_config(name, config) ⇒ Object



333
334
335
336
337
338
# File 'lib/kronk/cmd/oauth.rb', line 333

def add_twurl_config name, config
  config.delete('username')
  config['token_secret'] = config.delete('secret')

  @config.set(name, TWITTER_HOST, config)
end

#assert_has_host!(host) ⇒ Object



142
143
144
145
146
147
# File 'lib/kronk/cmd/oauth.rb', line 142

def assert_has_host! host
  if !@config.has_host?(host)
    $stderr.puts("No config for host #{host}")
    exit 1
  end
end

#assert_has_name_for_host!(name, host) ⇒ Object



134
135
136
137
138
139
# File 'lib/kronk/cmd/oauth.rb', line 134

def assert_has_name_for_host! name, host
  if !@config.has_name_for_host?(name, host)
    $stderr.puts("No config for #{name}@#{host}")
    exit 1
  end
end

#disable_config(host) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/kronk/cmd/oauth.rb', line 264

def disable_config host
  assert_has_host!(host)
  name = @config.active_name_for_host(host)

  if !name
    $stderr.puts("No active account for #{host}")
    exit 0
  end

  @config.set_active_for_host(nil, host)
  save_file!

  $stderr.puts("Disabled config #{name}@#{host}")
end

#import_twurl(file = nil) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/kronk/cmd/oauth.rb', line 294

def import_twurl file=nil
  file ||= File.expand_path('~/.twurlrc')

  if !File.file?( file )
    $stderr.puts("Could not find file: #{file}")
    exit 1
  end

  config = YAML.load_file(file) rescue nil

  is_valid = Hash === config &&
              config['profiles'] &&
              Hash === config['profiles']

  unless is_valid
    $stderr.puts("Invalid file format: #{file}")
    exit 1
  end

  host = 'api.twitter.com'
  profiles = config['profiles']

  profiles.each do |name, consumers|
    name = "#{name}-twurl" if @config.has_name_for_host?(name, TWITTER_HOST)
    if consumers.length == 1
      add_twurl_config(name, consumers[consumers.keys.first])
    else
      consumers.each do |key, config|
        add_twurl_config("#{name}-#{key}", config)
      end
    end
  end

  save_file!

  $stderr.puts("Successfully imported twurl config")
end

#list_config(host = nil, name = nil) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/kronk/cmd/oauth.rb', line 222

def list_config host=nil, name=nil
  if host && name
    assert_has_name_for_host!(name, host)
    $stdout.puts(@config.get(name, host).to_yaml)

  elsif host
    assert_has_host!(host)
    $stdout.puts(host)
    @config.names_for_host(host).sort.each do |config_name|
      mark = config_name == @config.active_name_for_host(host) ? "* " : "  "
      $stdout.puts "  #{mark}#{config_name}\n"
    end
    $stdout.puts("\n")

  elsif @config.empty?
    $stderr.puts("No config to display")

  else
    @config.hosts.sort.each do |h|
      $stdout.puts(h)
      @config.names_for_host(h).sort.each do |config_name|
        mark = config_name == @config.active_name_for_host(h) ? "* " : "  "
        $stdout.puts "  #{mark}#{config_name}\n"
      end
      $stdout.puts("\n")
    end
  end
end

#query(prompt, allow_blank = false) ⇒ Object



166
167
168
169
170
# File 'lib/kronk/cmd/oauth.rb', line 166

def query prompt, allow_blank=false
  $stderr << prompt
  value = $stdin.gets.chomp
  return value.empty? && !allow_blank ? query(prompt) : value
end

#query_name(host) ⇒ Object



161
162
163
# File 'lib/kronk/cmd/oauth.rb', line 161

def query_name host
  return query("Config name for #{host}: ")
end

#remove_config(host, name = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/kronk/cmd/oauth.rb', line 210

def remove_config host, name=nil
  assert_has_host!(host)
  name ||= select_name(host, true)

  @config.remove(host, name)

  save_file!

  $stderr.puts("Removed config #{name && "#{name}@"}#{host}")
end

#rename_config(host, name = nil) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/kronk/cmd/oauth.rb', line 280

def rename_config host, name=nil
  assert_has_host!(host)
  name ||= select_name(host)

  new_name = query("Enter new name: ")

  @config.rename(host, name, new_name)

  save_file!

  $stderr.puts("Renamed #{name}@#{host} to #{new_name}@#{host}")
end

#save_file!Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kronk/cmd/oauth.rb', line 120

def save_file!
  @config.save_file(@file)

  autocomplete = []
  @config.each do |host, name, config|
    autocomplete << "#{name}@#{host}"
  end

  autocomplete.concat(@config.hosts)

  File.open(Kronk::DEFAULT_OAUTH_LIST_FILE, "w+") {|f| f.write( autocomplete.join("\n") << "\n" ) }
end

#select_config(host, name = nil) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/kronk/cmd/oauth.rb', line 252

def select_config host, name=nil
  assert_has_host!(host)
  assert_has_name_for_host!(name, host) if name
  name ||= select_name(host)

  @config.set_active_for_host(name, host)
  save_file!

  $stderr.puts("Set active config #{name}@#{host}")
end

#select_name(host, allow_all = false) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/kronk/cmd/oauth.rb', line 173

def select_name host, allow_all=false
  names = @config.names_for_host(host).sort
  names.each_with_index do |name, i|
    mark = name == @config.active_name_for_host(host) ? "* " : "  "
    $stderr.puts("#{i+1}) #{mark}#{name}")
  end
  $stderr.puts("#{names.length+1})   All") if allow_all

  num = 0
  len = names.length + (allow_all ? 1 : 0)
  until num > 0 && num <= len
    num = query("Enter number: ").to_i
  end

  return names[num-1]
end

#validate_name(name, host) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/kronk/cmd/oauth.rb', line 150

def validate_name name, host
  while @config.has_name_for_host?(name, host)
    confirm = query("Override existing #{name}@#{host}? (y/n) ", true) == 'y'
    break if confirm
    name = query_name(host)
  end

  return name
end