module Stastic::Command class Base def initialize(args); end private def request_credentials print "Enter your Stastic login credentials.\n\n" attempts = 0 begin attempts += 1 print " Email: " email = ask print " Password: " password = ask_for_password token = Stastic::Client.authenticate(email, password) rescue Stastic::InvalidCredentials if attempts < 3 print "\nInvalid credentials - try again.\n\n" retry else print "\nInvalid credentials -- too many tries!\n" exit end end Stastic::Config.update({:user => email, :token => token}, :global) print "\nYour credentials have been saved.\n" end def echo_off system "stty -echo" end def echo_on system "stty echo" end def ask gets.strip end def ask_for_password echo_off password = ask puts echo_on return password end def with_valid_user request_credentials unless Stastic::Config.credentials? begin yield rescue RestClient::Forbidden request_credentials retry rescue RestClient::UnprocessableEntity => e puts "There was an error with the request:" puts JSON.parse(e.response)['errors'].each do |msg| printf(" * %s\n", msg) end puts exit -1 end end def with_valid_site request_credentials unless Stastic::Config.credentials? missing_site_id unless Stastic::Config.site_defined? begin yield rescue RestClient::Forbidden request_credentials retry rescue RestClient::UnprocessableEntity => e puts "Error:" puts e.response.to_s puts exit end end def missing_site_id puts "No Stastic Site found. To create a site, run:" puts puts " stastic create" puts exit end end end