From 6b22fa1d84ca5cefe094fc0808d1c6c45c22e4f3 Mon Sep 17 00:00:00 2001 From: henteko Date: Mon, 2 Nov 2015 20:01:40 +0900 Subject: [PATCH 1/6] add api/user --- lib/deploygate.rb | 1 + lib/deploygate/api/v1/user.rb | 35 +++++++++++++++++++++++++++++ spec/deploygate/api/v1/user_spec.rb | 28 +++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/deploygate/api/v1/user.rb create mode 100644 spec/deploygate/api/v1/user_spec.rb diff --git a/lib/deploygate.rb b/lib/deploygate.rb index e6046e6..527643a 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" diff --git a/lib/deploygate/api/v1/user.rb b/lib/deploygate/api/v1/user.rb new file mode 100644 index 0000000..37759e1 --- /dev/null +++ b/lib/deploygate/api/v1/user.rb @@ -0,0 +1,35 @@ +module DeployGate + module API + module V1 + class User + + ENDPOINT = '/users' + + class << self + # @param [String] name + # @param [String] email + # @param [String] passowrd + # @return [Boolean] + 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 + end + end + 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..4e45be7 --- /dev/null +++ b/spec/deploygate/api/v1/user_spec.rb @@ -0,0 +1,28 @@ +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 +end From 5d6a69f5bf24d3386414e73d4fb84572f5205ec0 Mon Sep 17 00:00:00 2001 From: henteko Date: Wed, 4 Nov 2015 16:27:13 +0900 Subject: [PATCH 2/6] add already_registered? api --- lib/deploygate/api/v1/user.rb | 23 ++++++++++++++++-- spec/deploygate/api/v1/user_spec.rb | 36 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/deploygate/api/v1/user.rb b/lib/deploygate/api/v1/user.rb index 37759e1..51061bf 100644 --- a/lib/deploygate/api/v1/user.rb +++ b/lib/deploygate/api/v1/user.rb @@ -8,8 +8,8 @@ class User class << self # @param [String] name # @param [String] email - # @param [String] passowrd - # @return [Boolean] + # @param [String] password + # @return [Hash] def create(name, email, password) res = Base.new().post(ENDPOINT, {:name => name, :email => email, :password => password}) @@ -28,6 +28,25 @@ def create(name, email, password) user_create_results end + + # @param [String] name_or_email + # @return [Hash] + def show(name_or_email) + res = Base.new().get("#{ENDPOINT}/#{name_or_email}", {}) + user_show_results = { + :error => res['error'], + :message => res['because'] + } + + results = res['results'] + unless results.nil? + user_show_results.merge!({ + :name => results['name'], + }) + end + + user_show_results + end end end end diff --git a/spec/deploygate/api/v1/user_spec.rb b/spec/deploygate/api/v1/user_spec.rb index 4e45be7..db4c216 100644 --- a/spec/deploygate/api/v1/user_spec.rb +++ b/spec/deploygate/api/v1/user_spec.rb @@ -25,4 +25,40 @@ }) end end + + describe "#already_registered?" do + it "registerd" do + name = 'test' + response = { + :error => false, + :because => '', + :results => {:name => name} + } + stub_request(:get, "#{API_ENDPOINT}/users/#{name}"). + to_return(:body => response.to_json) + + results = DeployGate::API::V1::User.show(name) + expect(results).to eq({ + :error => response[:error], + :message => response[:because], + :name => name, + }) + end + + it "not registerd" do + name = 'test' + response = { + :error => true, + :because => '' + } + stub_request(:get, "#{API_ENDPOINT}/users/#{name}"). + to_return(:body => response.to_json) + + results = DeployGate::API::V1::User.show(name) + expect(results).to eq({ + :error => response[:error], + :message => response[:because] + }) + end + end end From cf9e2b271f4d90279e066f94f77e8b7e89aae7ce Mon Sep 17 00:00:00 2001 From: henteko Date: Wed, 4 Nov 2015 17:14:13 +0900 Subject: [PATCH 3/6] support create account --- lib/deploygate.rb | 1 + lib/deploygate/commands/deploy.rb | 2 +- lib/deploygate/commands/init.rb | 84 +++++++++++++++++++++++++++++-- lib/deploygate/user.rb | 29 +++++++++++ 4 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 lib/deploygate/user.rb diff --git a/lib/deploygate.rb b/lib/deploygate.rb index 527643a..f4b4a54 100644 --- a/lib/deploygate.rb +++ b/lib/deploygate.rb @@ -38,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/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..bb74b28 100644 --- a/lib/deploygate/commands/init.rb +++ b/lib/deploygate/commands/init.rb @@ -5,21 +5,34 @@ 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 = STDIN.gets.chop + puts '' + puts 'Checking for your account...' + if DeployGate::User.find_user(email).nil? + create_account(email) + else + puts '' + password = input_password() + puts '' + login(email, password) + 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 +47,67 @@ 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 + print 'Username: ' + user_name = STDIN.gets.chop + print 'Checking for availability... ' + + if DeployGate::User.find_user(user_name).nil? + Message::Success.print("Good, #{user_name} is available.") + return user_name + else + Message::Error.print("Bad, #{user_name} is already used. Please try again.") + return input_new_account_name() + end + end + + # @return [String] + def input_new_account_password + print 'Password: ' + password = STDIN.noecho(&:gets).chop + puts '' + print 'Type the same password: ' + secound_password = STDIN.noecho(&:gets).chop + + if password == secound_password + return password + else + puts '' + Message::Error.print("Password Please enter the same thing.") + return input_new_account_password() + end + end + + # @return [String] + def input_password + print 'Password: ' + password = STDIN.noecho(&:gets).chop + 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..3958cb0 --- /dev/null +++ b/lib/deploygate/user.rb @@ -0,0 +1,29 @@ +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] email_or_name + # @return [DeployGate::User] + def self.find_user(email_or_name) + results = DeployGate::API::V1::User.show(email_or_name) + return if results[:error] + DeployGate::User.new(results[:name]) + end + end +end From 37e90b8d6c98a549f1f6d3f9e91165da3e1726d8 Mon Sep 17 00:00:00 2001 From: henteko Date: Thu, 5 Nov 2015 13:43:09 +0900 Subject: [PATCH 4/6] change api check_already_registered --- lib/deploygate/api/v1/user.rb | 23 ++++++----------------- lib/deploygate/commands/init.rb | 14 +++++++------- lib/deploygate/user.rb | 11 +++++------ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lib/deploygate/api/v1/user.rb b/lib/deploygate/api/v1/user.rb index 51061bf..eb8c1e0 100644 --- a/lib/deploygate/api/v1/user.rb +++ b/lib/deploygate/api/v1/user.rb @@ -29,23 +29,12 @@ def create(name, email, password) user_create_results end - # @param [String] name_or_email - # @return [Hash] - def show(name_or_email) - res = Base.new().get("#{ENDPOINT}/#{name_or_email}", {}) - user_show_results = { - :error => res['error'], - :message => res['because'] - } - - results = res['results'] - unless results.nil? - user_show_results.merge!({ - :name => results['name'], - }) - end - - user_show_results + # @param [String] name + # @param [String] email + # @return [Boolean] + def already_registered?(name, email) + res = Base.new().get("#{ENDPOINT}/check_already_registered", {:name => name, :email => email}) + res['results']['already_registered'] end end end diff --git a/lib/deploygate/commands/init.rb b/lib/deploygate/commands/init.rb index bb74b28..46ac99d 100644 --- a/lib/deploygate/commands/init.rb +++ b/lib/deploygate/commands/init.rb @@ -19,13 +19,13 @@ def start_login_or_create_account puts '' puts 'Checking for your account...' - if DeployGate::User.find_user(email).nil? - create_account(email) - else + if DeployGate::User.already_registered?('', email) puts '' password = input_password() puts '' login(email, password) + else + create_account(email) end end @@ -76,12 +76,12 @@ def input_new_account_name user_name = STDIN.gets.chop print 'Checking for availability... ' - if DeployGate::User.find_user(user_name).nil? - Message::Success.print("Good, #{user_name} is available.") - return user_name - else + if DeployGate::User.already_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 diff --git a/lib/deploygate/user.rb b/lib/deploygate/user.rb index 3958cb0..0e9afd4 100644 --- a/lib/deploygate/user.rb +++ b/lib/deploygate/user.rb @@ -18,12 +18,11 @@ def self.create(name, email, password) DeployGate::User.new(results[:name]) end - # @param [String] email_or_name - # @return [DeployGate::User] - def self.find_user(email_or_name) - results = DeployGate::API::V1::User.show(email_or_name) - return if results[:error] - DeployGate::User.new(results[:name]) + # @param [String] name + # @param [String] email + # @return [Boolean] + def self.already_registered?(name, email) + DeployGate::API::V1::User.already_registered?(name, email) end end end From 3835a9bf5a317bf963d31989c8bf8188d0819203 Mon Sep 17 00:00:00 2001 From: henteko Date: Thu, 5 Nov 2015 13:56:25 +0900 Subject: [PATCH 5/6] password input * --- lib/deploygate/commands/init.rb | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/deploygate/commands/init.rb b/lib/deploygate/commands/init.rb index 46ac99d..a948e2a 100644 --- a/lib/deploygate/commands/init.rb +++ b/lib/deploygate/commands/init.rb @@ -14,14 +14,13 @@ def run def start_login_or_create_account puts 'Welcome to DeployGate!' puts '' - print 'Email: ' - email = STDIN.gets.chop + email = ask("Email: ") puts '' puts 'Checking for your account...' if DeployGate::User.already_registered?('', email) puts '' - password = input_password() + password = input_password('Password: ') puts '' login(email, password) else @@ -72,8 +71,7 @@ def create_account(email) # @return [String] def input_new_account_name - print 'Username: ' - user_name = STDIN.gets.chop + user_name = ask("Username: " ) print 'Checking for availability... ' if DeployGate::User.already_registered?(user_name, '') @@ -87,25 +85,20 @@ def input_new_account_name # @return [String] def input_new_account_password - print 'Password: ' - password = STDIN.noecho(&:gets).chop - puts '' - print 'Type the same password: ' - secound_password = STDIN.noecho(&:gets).chop + password = input_password('Password: ') + secound_password = input_password('Type the same password: ') if password == secound_password return password else - puts '' Message::Error.print("Password Please enter the same thing.") return input_new_account_password() end end # @return [String] - def input_password - print 'Password: ' - password = STDIN.noecho(&:gets).chop + def input_password(message) + ask(message) { |q| q.echo = "*" } end # @return [void] From 8aa1f38145cf289b3c07558278a6ec2927e8b24f Mon Sep 17 00:00:00 2001 From: henteko Date: Thu, 5 Nov 2015 14:34:48 +0900 Subject: [PATCH 6/6] fix user registered api --- lib/deploygate/api/v1/user.rb | 6 +++--- lib/deploygate/commands/init.rb | 4 ++-- lib/deploygate/user.rb | 4 ++-- spec/deploygate/api/v1/user_spec.rb | 32 ++++++++++++----------------- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/lib/deploygate/api/v1/user.rb b/lib/deploygate/api/v1/user.rb index eb8c1e0..dd206bc 100644 --- a/lib/deploygate/api/v1/user.rb +++ b/lib/deploygate/api/v1/user.rb @@ -32,9 +32,9 @@ def create(name, email, password) # @param [String] name # @param [String] email # @return [Boolean] - def already_registered?(name, email) - res = Base.new().get("#{ENDPOINT}/check_already_registered", {:name => name, :email => email}) - res['results']['already_registered'] + def registered?(name, email) + res = Base.new().get("#{ENDPOINT}/registered", {:name => name, :email => email}) + res['results']['registered'] end end end diff --git a/lib/deploygate/commands/init.rb b/lib/deploygate/commands/init.rb index a948e2a..0ec69e1 100644 --- a/lib/deploygate/commands/init.rb +++ b/lib/deploygate/commands/init.rb @@ -18,7 +18,7 @@ def start_login_or_create_account puts '' puts 'Checking for your account...' - if DeployGate::User.already_registered?('', email) + if DeployGate::User.registered?('', email) puts '' password = input_password('Password: ') puts '' @@ -74,7 +74,7 @@ def input_new_account_name user_name = ask("Username: " ) print 'Checking for availability... ' - if DeployGate::User.already_registered?(user_name, '') + if DeployGate::User.registered?(user_name, '') Message::Error.print("Bad, #{user_name} is already used. Please try again.") return input_new_account_name() else diff --git a/lib/deploygate/user.rb b/lib/deploygate/user.rb index 0e9afd4..2cf31a0 100644 --- a/lib/deploygate/user.rb +++ b/lib/deploygate/user.rb @@ -21,8 +21,8 @@ def self.create(name, email, password) # @param [String] name # @param [String] email # @return [Boolean] - def self.already_registered?(name, email) - DeployGate::API::V1::User.already_registered?(name, email) + 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 index db4c216..f03ec46 100644 --- a/spec/deploygate/api/v1/user_spec.rb +++ b/spec/deploygate/api/v1/user_spec.rb @@ -26,39 +26,33 @@ end end - describe "#already_registered?" do - it "registerd" do + describe "#registered?" do + it "registered" do name = 'test' response = { :error => false, :because => '', - :results => {:name => name} + :results => {:registered => true} } - stub_request(:get, "#{API_ENDPOINT}/users/#{name}"). + stub_request(:get, "#{API_ENDPOINT}/users/registered?email=&name=#{name}"). to_return(:body => response.to_json) - results = DeployGate::API::V1::User.show(name) - expect(results).to eq({ - :error => response[:error], - :message => response[:because], - :name => name, - }) + result = DeployGate::API::V1::User.registered?(name, '') + expect(result).to be_truthy end - it "not registerd" do + it "not registered" do name = 'test' response = { - :error => true, - :because => '' + :error => false, + :because => '', + :results => {:registered => false} } - stub_request(:get, "#{API_ENDPOINT}/users/#{name}"). + stub_request(:get, "#{API_ENDPOINT}/users/registered?email=&name=#{name}"). to_return(:body => response.to_json) - results = DeployGate::API::V1::User.show(name) - expect(results).to eq({ - :error => response[:error], - :message => response[:because] - }) + result = DeployGate::API::V1::User.registered?(name, '') + expect(result).to be_falsey end end end