diff --git a/lib/deploygate.rb b/lib/deploygate.rb index e6046e6..f4b4a54 100644 --- a/lib/deploygate.rb +++ b/lib/deploygate.rb @@ -25,6 +25,7 @@ module DeployGate require "deploygate/api/v1/base" require "deploygate/api/v1/session" require "deploygate/api/v1/push" +require "deploygate/api/v1/user" require "deploygate/command_builder" require "deploygate/commands/init" require "deploygate/commands/logout" @@ -37,6 +38,7 @@ module DeployGate require "deploygate/session" require "deploygate/deploy" require "deploygate/build" +require "deploygate/user" require "deploygate/builds/ios" require "deploygate/builds/ios/export" require "deploygate/builds/ios/analyze" diff --git a/lib/deploygate/api/v1/user.rb b/lib/deploygate/api/v1/user.rb new file mode 100644 index 0000000..dd206bc --- /dev/null +++ b/lib/deploygate/api/v1/user.rb @@ -0,0 +1,43 @@ +module DeployGate + module API + module V1 + class User + + ENDPOINT = '/users' + + class << self + # @param [String] name + # @param [String] email + # @param [String] password + # @return [Hash] + def create(name, email, password) + res = Base.new().post(ENDPOINT, {:name => name, :email => email, :password => password}) + + user_create_results = { + :error => res['error'], + :message => res['because'] + } + + results = res['results'] + unless results.nil? + user_create_results.merge!({ + :name => results['user']['name'], + :token => results['api_token'] + }) + end + + user_create_results + end + + # @param [String] name + # @param [String] email + # @return [Boolean] + def registered?(name, email) + res = Base.new().get("#{ENDPOINT}/registered", {:name => name, :email => email}) + res['results']['registered'] + end + end + end + end + end +end diff --git a/lib/deploygate/commands/deploy.rb b/lib/deploygate/commands/deploy.rb index ba2f798..002d2f2 100644 --- a/lib/deploygate/commands/deploy.rb +++ b/lib/deploygate/commands/deploy.rb @@ -6,7 +6,7 @@ class << self # @param [Array] args # @param [Commander::Command::Options] options def run(args, options) - Init.login unless DeployGate::Session.new.login? + Init.start_login_or_create_account() unless DeployGate::Session.new.login? # push or build(android/ios) args.push(Dir.pwd) if args.empty? diff --git a/lib/deploygate/commands/init.rb b/lib/deploygate/commands/init.rb index 9e00c8d..0ec69e1 100644 --- a/lib/deploygate/commands/init.rb +++ b/lib/deploygate/commands/init.rb @@ -5,21 +5,33 @@ class << self # @return [void] def run - login unless Session.new().login? + start_login_or_create_account() unless Session.new().login? finish end # @return [void] - def login + def start_login_or_create_account puts 'Welcome to DeployGate!' puts '' - print 'Email: ' - email= STDIN.gets.chop - print 'Password: ' - password = STDIN.noecho(&:gets).chop + email = ask("Email: ") + puts '' + puts 'Checking for your account...' + if DeployGate::User.registered?('', email) + puts '' + password = input_password('Password: ') + puts '' + login(email, password) + else + create_account(email) + end + end + # @param [String] email + # @param [String] password + # @return [void] + def login(email, password) begin Session.login(email, password) rescue Session::LoginError => e @@ -34,6 +46,61 @@ def login Message::Success.print("Hello #{session.name}!") end + # @param [String] email + # @return [void] + def create_account(email) + puts "Looks new to DeployGate. Let's set up your account, just choose your username and password." + puts '' + + name = input_new_account_name() + puts '' + + password = input_new_account_password() + puts '' + + print 'Creating your account... ' + if DeployGate::User.create(name, email, password).nil? + Message::Error.print('User create error') + Message::Error.print('Please try again') + raise 'User create error' + else + Message::Success.print('done! Your account has been set up successfully.') + login(email, password) + end + end + + # @return [String] + def input_new_account_name + user_name = ask("Username: " ) + print 'Checking for availability... ' + + if DeployGate::User.registered?(user_name, '') + Message::Error.print("Bad, #{user_name} is already used. Please try again.") + return input_new_account_name() + else + Message::Success.print("Good, #{user_name} is available.") + return user_name + end + end + + # @return [String] + def input_new_account_password + password = input_password('Password: ') + secound_password = input_password('Type the same password: ') + + if password == secound_password + return password + else + Message::Error.print("Password Please enter the same thing.") + return input_new_account_password() + end + end + + # @return [String] + def input_password(message) + ask(message) { |q| q.echo = "*" } + end + # @return [void] def finish Message::Success.print('Enjoy development!') diff --git a/lib/deploygate/user.rb b/lib/deploygate/user.rb new file mode 100644 index 0000000..2cf31a0 --- /dev/null +++ b/lib/deploygate/user.rb @@ -0,0 +1,28 @@ +module DeployGate + class User + attr_reader :name + + # @param [String] name + # @return [DeployGate::User] + def initialize(name) + @name = name + end + + # @param [String] name + # @param [String] email + # @param [String] password + # @return [DeployGate::User] + def self.create(name, email, password) + results = DeployGate::API::V1::User.create(name, email, password) + return if results[:error] + DeployGate::User.new(results[:name]) + end + + # @param [String] name + # @param [String] email + # @return [Boolean] + def self.registered?(name, email) + DeployGate::API::V1::User.registered?(name, email) + end + end +end diff --git a/spec/deploygate/api/v1/user_spec.rb b/spec/deploygate/api/v1/user_spec.rb new file mode 100644 index 0000000..f03ec46 --- /dev/null +++ b/spec/deploygate/api/v1/user_spec.rb @@ -0,0 +1,58 @@ +describe DeployGate::API::V1::User do + describe "#create" do + it "success" do + name = 'test' + email = 'email' + password = 'password' + token = 'token' + response = { + :error => false, + :because => '', + :results => { + :user => {:name => name}, + :api_token => token + } + } + stub_request(:post, "#{API_ENDPOINT}/users"). + to_return(:body => response.to_json) + + results = DeployGate::API::V1::User.create(name, email, password) + expect(results).to eq({ + :error => response[:error], + :message => response[:because], + :name => name, + :token => token + }) + end + end + + describe "#registered?" do + it "registered" do + name = 'test' + response = { + :error => false, + :because => '', + :results => {:registered => true} + } + stub_request(:get, "#{API_ENDPOINT}/users/registered?email=&name=#{name}"). + to_return(:body => response.to_json) + + result = DeployGate::API::V1::User.registered?(name, '') + expect(result).to be_truthy + end + + it "not registered" do + name = 'test' + response = { + :error => false, + :because => '', + :results => {:registered => false} + } + stub_request(:get, "#{API_ENDPOINT}/users/registered?email=&name=#{name}"). + to_return(:body => response.to_json) + + result = DeployGate::API::V1::User.registered?(name, '') + expect(result).to be_falsey + end + end +end