class Tomodachi::Auth

Constants

CONFIG_PATH

Public Instance Methods

create() click to toggle source
# File lib/tomodachi/auth.rb, line 13
def create
  load_access_token(access_token)

  accounts = load_config || []
  if accounts.find { |account| account[: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
list() click to toggle source
# 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_config() click to toggle source
# 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

Private Instance Methods

access_token() click to toggle source
# File lib/tomodachi/auth.rb, line 54
def access_token
  return @access_token if @access_token

  request_token = consumer.get_request_token
  system('open', request_token.authorize_url)
  @access_token = request_token.get_access_token(oauth_verifier: ask('PIN:'))
end
consumer() click to toggle source
# File lib/tomodachi/auth.rb, line 75
def consumer
  @consumer ||= OAuth::Consumer.new(
    Tomodachi::CONSUMER_KEY,
    Tomodachi::CONSUMER_SECRET,
    site: 'https://api.twitter.com',
  )
end
load_access_token(access_token) click to toggle source
# File lib/tomodachi/auth.rb, line 62
def load_access_token(access_token)
  Twitter.configure do |config|
    config.consumer_key = Tomodachi::CONSUMER_KEY
    config.consumer_secret = Tomodachi::CONSUMER_SECRET
    config.oauth_token = access_token.token
    config.oauth_token_secret = access_token.secret
  end
end
save_config(conf) click to toggle source
# File lib/tomodachi/auth.rb, line 83
def save_config(conf)
  File.open(CONFIG_PATH, 'w') do |f|
    f << conf.to_yaml
  end
end
user() click to toggle source
# File lib/tomodachi/auth.rb, line 71
def user
  @user ||= Twitter.user
end