简体   繁体   中英

How can I create a single login for different ruby methods? #thor

I am making a command line tool for gmail using the gmail gem.

def login
  $username = ask 'Enter your username:'
  $password = ask 'Enter your password:'
  gmail = Gmail.connect!($username, $password)
  say 'User Authorized!'
end
def mail
  gmail = Gmail.connect!($username, $password)
  to_mail = ask 'Enter email-id:'
  subject_mail = ask 'Enter subject:'
  body_mail = ask 'Enter body:'
  email=gmail.compose do
    to to_mail
    subject subject_mail
    body body_mail
  end
  gmail.deliver(email)
  gmail.logout
end

Using thor gem to create different methods but I am not able to create a single login for both the commands. How can I achieve that?

Why cannot I access $username and $password in the mail method.

I want to create a command login and then use the username and password in that in all the different methods.

Every command is a new process, so the data in last command is actually gone. You need to persist them.

As it's command line tool running in local, you may save user's data into local file some where like ~/.yourapp/config . So next time, you can look for the local config file firstly.

Of course, instead of directly asking user's credential, you can use Google OAuth to access user's google data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM