From fdcc77ae80f4e8f0d99621b980d0a9e7c0b45578 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Sat, 31 Jan 2015 08:41:56 +0000 Subject: [PATCH] VRFS-2697 url format and site validation --- web/app/assets/javascripts/jam_rest.js | 9 +++++ .../javascripts/website_validator.js.coffee | 34 ++++++++++++++++--- .../client/website_validator.css.scss | 10 ++++-- web/app/controllers/api_users_controller.rb | 19 ++++++++++- .../clients/_website_validator.html.slim | 6 ++-- web/app/views/spikes/site_validate.html.slim | 4 +-- web/config/routes.rb | 3 ++ 7 files changed, 73 insertions(+), 12 deletions(-) diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 29b5fe284..7755e467b 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -1478,6 +1478,14 @@ }); } + function validateUrl(url) { + return $.ajax({ + type: "GET", + url: '/api/data_validation?data=' + encodeURIComponent(url), + contentType: 'application/json' + }); + } + function initialize() { return self; } @@ -1608,6 +1616,7 @@ this.resendBandInvitation = resendBandInvitation; this.getMount = getMount; this.createSourceChange = createSourceChange; + this.validateUrl = validateUrl; return this; }; diff --git a/web/app/assets/javascripts/website_validator.js.coffee b/web/app/assets/javascripts/website_validator.js.coffee index 5187e78ad..f0882f699 100644 --- a/web/app/assets/javascripts/website_validator.js.coffee +++ b/web/app/assets/javascripts/website_validator.js.coffee @@ -3,18 +3,21 @@ context = window context.JK ||= {}; context.JK.WebsiteValidator = class WebsiteValidator - constructor: (@app, input_obj) -> + constructor: (@app, input_div) -> @EVENTS = context.JK.EVENTS @rest = context.JK.Rest() - @url_input = input_obj + @input_div = input_div + @url_input = @input_div.find('input') + this.show_validation_status() + @logger = context.JK.logger init: () => validator = this; @url_input.bind 'blur', -> - yn = validator.validate_url() - alert 'invalid' unless yn + alert 'invalid' unless validator.show_validation_status() + validator.validate_url_site() - validate_url: () => + validate_url_format: () => url = @url_input.val() if 0 < url.length regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ @@ -22,3 +25,24 @@ context.JK.WebsiteValidator = class WebsiteValidator else true + show_validation_status: () => + yn = this.validate_url_format() + if yn + @input_div.find('.valid_checkmark').show() + else + @input_div.find('.valid_checkmark').hide() + yn + + validate_url_site: () => + @rest.validateUrl(@url_input.val(), context.JK.currentUserId) + .done(this.processDidValidate) + .fail(this.processDidNotValidate) + + processDidValidate: (response) => + if 'valid' == response.message + @logger.debug("is valid") + else if 'invalid' == response.message + @logger.debug("is not valid") + + processDidNotValidate: () => + @logger.error("is not valid ... error") diff --git a/web/app/assets/stylesheets/client/website_validator.css.scss b/web/app/assets/stylesheets/client/website_validator.css.scss index 6173c7c68..15c8be03a 100644 --- a/web/app/assets/stylesheets/client/website_validator.css.scss +++ b/web/app/assets/stylesheets/client/website_validator.css.scss @@ -1,6 +1,12 @@ @import "client/common"; .website_validator { - width: 100%; - padding: 10px; + input { + width: 100%; + padding: 5px; + float: left; + } + .valid_checkmark { + float: left; + } } diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 7d8d40f94..ae7ede30b 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -1,7 +1,7 @@ require 'sanitize' class ApiUsersController < ApiController - before_filter :api_signed_in_user, :except => [:create, :show, :signup_confirm, :auth_session_create, :complete, :finalize_update_email, :isp_scoring, :add_play, :crash_dump] + before_filter :api_signed_in_user, :except => [:create, :show, :signup_confirm, :auth_session_create, :complete, :finalize_update_email, :isp_scoring, :add_play, :crash_dump, :validate_data] before_filter :auth_user, :only => [:session_settings_show, :session_history_index, :session_user_history_index, :update, :delete, :liking_create, :liking_destroy, # likes :following_create, :following_show, :following_destroy, # followings @@ -699,6 +699,23 @@ class ApiUsersController < ApiController end end + def validate_data + data = params[:data] + vtype = data =~ /^http/ ? 'url' : 'username' + if 'url' == vtype + if data.present? + `curl --output /dev/null --silent --head --fail --show-error '#{data}'` + render json: { message: $?.success? ? 'valid' : 'invalid' }, status: 200 + else + render json: { message: "blank data #{data}" }, status: :unprocessable_entity + end + return + elsif 'username' == vtype + end + + render json: { message: "unknown validation type #{params[:validation_type]}" }, status: :unprocessable_entity + end + ###################### RECORDINGS ####################### # def recording_index # @recordings = User.recording_index(current_user, params[:id]) diff --git a/web/app/views/clients/_website_validator.html.slim b/web/app/views/clients/_website_validator.html.slim index c3996f441..7573b166e 100644 --- a/web/app/views/clients/_website_validator.html.slim +++ b/web/app/views/clients/_website_validator.html.slim @@ -1,2 +1,4 @@ -input type='text' id="website_#{siteid}" class="website_validator" - +div class="website_validator" id="#{siteid}_url" + div class="valid_checkmark" ✓ + input type='text' id="url_input_#{siteid}" + diff --git a/web/app/views/spikes/site_validate.html.slim b/web/app/views/spikes/site_validate.html.slim index 329e8edf2..294f14f6d 100644 --- a/web/app/views/spikes/site_validate.html.slim +++ b/web/app/views/spikes/site_validate.html.slim @@ -7,8 +7,8 @@ javascript: var initialized = false; $(document).on('JAMKAZAM_READY', function(e, data) { setTimeout(function() { - window.website_validator = new JK.WebsiteValidator(data.app, $("#website_foobar")) + window.website_validator = new JK.WebsiteValidator(data.app, $(".website_validator#foobar_url")) website_validator.init() - $('#website_foobar').val('http://www.jamkazam.com') + $('#url_input_foobar').val('http://www.jamkazam.com') }, 1) }) diff --git a/web/config/routes.rb b/web/config/routes.rb index 50d0ae687..653ecfe1b 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -231,6 +231,9 @@ SampleApp::Application.routes.draw do # users match '/users/isp_scoring' => 'api_users#isp_scoring', :via => :post , :as => 'isp_scoring' + # validation + match '/data_validation' => 'api_users#validate_data', :via => :get + match '/users' => 'api_users#index', :via => :get match '/users/:id' => 'api_users#show', :via => :get, :as => 'api_user_detail' #match '/users' => 'api_users#create', :via => :post