Class: Tomodachi::Auth

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/tomodachi/auth.rb

Constant Summary collapse

CONFIG_PATH =
File.expand_path('~/.tomodachi')

Instance Method Summary collapse

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tomodachi/auth.rb', line 13

def create
  load_access_token(access_token)

  accounts = load_config || []
  if accounts.find { || [:id] == user[:id] }
    puts "#{user[:screen_name]} is already added."
  else
    accounts += [
      id: user[:id],
      screen_name: user[:screen_name],
      access_token: access_token.token,
      access_token_secret: access_token.secret,
    ]
    save_config(accounts)
    puts "Added configuration for #{user[:screen_name]}"
  end
end

#listObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tomodachi/auth.rb', line 31

def list
  accounts = load_config
  if accounts
    puts "Available accounts:"
    accounts.each do |conf|
      puts "  #{conf[:screen_name]}"
    end
  else
    puts "There is no authenticated account."
  end
end

#load_configObject



43
44
45
46
47
48
49
50
# File 'lib/tomodachi/auth.rb', line 43

def load_config
  if File.exists?(CONFIG_PATH)
    yaml = File.read(CONFIG_PATH)
    YAML.load(yaml)
  else
    nil
  end
end