From ebc20cf74fbcd402f9fd36d1ed8690a056337039 Mon Sep 17 00:00:00 2001 From: Adam Wead Date: Tue, 21 Mar 2017 15:31:20 -0400 Subject: [PATCH] Use Rails indentation style; include rubocop-rspec --- .rubocop.yml | 3 + .rubocop_todo.yml | 26 +++++- .../browse_everything_controller.rb | 58 ++++++------- lib/browse_everything/driver/base.rb | 16 ++-- lib/browse_everything/driver/box.rb | 82 +++++++++---------- lib/browse_everything/driver/dropbox.rb | 14 ++-- lib/browse_everything/driver/file_system.rb | 42 +++++----- lib/browse_everything/driver/google_drive.rb | 30 +++---- lib/browse_everything/driver/sky_drive.rb | 34 ++++---- 9 files changed, 166 insertions(+), 139 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0b106299..52107609 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -26,3 +26,6 @@ RSpec/LeadingSubject: Style/NumericLiterals: MinDigits: 7 + +Style/IndentationConsistency: + EnforcedStyle: rails diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index eff583bb..be591870 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,8 @@ +require: rubocop-rspec + # This configuration was generated by # `rubocop --auto-gen-config` -# on 2017-03-15 21:03:05 -0400 using RuboCop version 0.42.0. +# on 2017-03-21 15:33:13 -0400 using RuboCop version 0.42.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -29,6 +31,28 @@ Metrics/MethodLength: Metrics/ParameterLists: Max: 7 +# Offense count: 1 +RSpec/DescribeClass: + Exclude: + - 'spec/javascripts/jasmine_spec.rb' + +# Offense count: 2 +# Configuration parameters: Max. +RSpec/ExampleLength: + Exclude: + - 'spec/features/select_files_spec.rb' + - 'spec/javascripts/jasmine_spec.rb' + +# Offense count: 2 +RSpec/MultipleExpectations: + Max: 2 + +# Offense count: 1 +# Configuration parameters: IgnoreSymbolicNames. +RSpec/VerifiedDoubles: + Exclude: + - 'spec/views/browse_everything/_file.html.erb_spec.rb' + # Offense count: 1 Rails/OutputSafety: Exclude: diff --git a/app/controllers/browse_everything_controller.rb b/app/controllers/browse_everything_controller.rb index 193abef2..e75e2bea 100644 --- a/app/controllers/browse_everything_controller.rb +++ b/app/controllers/browse_everything_controller.rb @@ -37,40 +37,40 @@ def resolve private - def auth_link - @auth_link ||= if provider.present? - link, data = provider.auth_link - session["#{provider_name}_data"] = data - link = "#{link}&state=#{provider.key}" unless link.to_s.include?('state') - link - end # else nil, implicitly - end + def auth_link + @auth_link ||= if provider.present? + link, data = provider.auth_link + session["#{provider_name}_data"] = data + link = "#{link}&state=#{provider.key}" unless link.to_s.include?('state') + link + end # else nil, implicitly + end - def browser - if @browser.nil? - @browser = BrowseEverything::Browser.new(url_options) - @browser.providers.values.each do |p| - p.token = session["#{p.key}_token"] + def browser + if @browser.nil? + @browser = BrowseEverything::Browser.new(url_options) + @browser.providers.values.each do |p| + p.token = session["#{p.key}_token"] + end end + @browser end - @browser - end - def browse_path - @path ||= params[:path] || '' - end + def browse_path + @path ||= params[:path] || '' + end - def provider - @provider ||= browser.providers[provider_name] - end + def provider + @provider ||= browser.providers[provider_name] + end - def provider_name - @provider_name ||= params[:provider] || params[:state].to_s.split(/\|/).last - end + def provider_name + @provider_name ||= params[:provider] || params[:state].to_s.split(/\|/).last + end - helper_method :auth_link - helper_method :browser - helper_method :browse_path - helper_method :provider - helper_method :provider_name + helper_method :auth_link + helper_method :browser + helper_method :browse_path + helper_method :provider + helper_method :provider_name end diff --git a/lib/browse_everything/driver/base.rb b/lib/browse_everything/driver/base.rb index 1784aa36..cf5534d9 100644 --- a/lib/browse_everything/driver/base.rb +++ b/lib/browse_everything/driver/base.rb @@ -53,15 +53,15 @@ def connect(_params, _data) private - def callback - connector_response_url(callback_options) - end + def callback + connector_response_url(callback_options) + end - # remove the script_name parameter from the url_options since that is causing issues - # with the route not containing the engine path in rails 4.2.0 - def callback_options - config[:url_options].reject { |k, _v| k == :script_name } - end + # remove the script_name parameter from the url_options since that is causing issues + # with the route not containing the engine path in rails 4.2.0 + def callback_options + config[:url_options].reject { |k, _v| k == :script_name } + end end end end diff --git a/lib/browse_everything/driver/box.rb b/lib/browse_everything/driver/box.rb index 705c3a8a..ab4e3c84 100644 --- a/lib/browse_everything/driver/box.rb +++ b/lib/browse_everything/driver/box.rb @@ -64,50 +64,50 @@ def connect(params, _data) private - def oauth_client - RubyBox::Session.new(client_id: config[:client_id], - client_secret: config[:client_secret]) - # TODO: error checking here - end + def oauth_client + RubyBox::Session.new(client_id: config[:client_id], + client_secret: config[:client_secret]) + # TODO: error checking here + end - def token_expired?(token) - return false unless @token.present? && @token['token'].present? - new_session = RubyBox::Session.new( - client_id: config[:client_id], - client_secret: config[:client_secret], - access_token: token - ) - result = new_session.get("#{RubyBox::API_URL}/users/me") - result['status'] != 200 - rescue RubyBox::AuthError => e - Rails.logger.error("AuthError occured when checking token. Exception #{e.class.name} : #{e.message}. token as expired and need to refresh it") - return true - end + def token_expired?(token) + return false unless @token.present? && @token['token'].present? + new_session = RubyBox::Session.new( + client_id: config[:client_id], + client_secret: config[:client_secret], + access_token: token + ) + result = new_session.get("#{RubyBox::API_URL}/users/me") + result['status'] != 200 + rescue RubyBox::AuthError => e + Rails.logger.error("AuthError occured when checking token. Exception #{e.class.name} : #{e.message}. token as expired and need to refresh it") + return true + end - def refresh_token - refresh_token = @token['refresh_token'] - token = @token['token'] - session = RubyBox::Session.new( - client_id: config[:client_id], - client_secret: config[:client_secret], - access_token: token - ) - access_token = session.refresh_token(refresh_token) - @token = { 'token' => access_token.token, 'refresh_token' => access_token.refresh_token } - end + def refresh_token + refresh_token = @token['refresh_token'] + token = @token['token'] + session = RubyBox::Session.new( + client_id: config[:client_id], + client_secret: config[:client_secret], + access_token: token + ) + access_token = session.refresh_token(refresh_token) + @token = { 'token' => access_token.token, 'refresh_token' => access_token.refresh_token } + end - def box_client - refresh_token if token_expired?(@token['token']) - token = @token['token'] - refresh_token = @token['refresh_token'] - session = RubyBox::Session.new( - client_id: config[:client_id], - client_secret: config[:client_secret], - access_token: token, - refresh_token: refresh_token - ) - RubyBox::Client.new(session) - end + def box_client + refresh_token if token_expired?(@token['token']) + token = @token['token'] + refresh_token = @token['refresh_token'] + session = RubyBox::Session.new( + client_id: config[:client_id], + client_secret: config[:client_secret], + access_token: token, + refresh_token: refresh_token + ) + RubyBox::Client.new(session) + end end end end diff --git a/lib/browse_everything/driver/dropbox.rb b/lib/browse_everything/driver/dropbox.rb index e2dbcd7a..8b370625 100644 --- a/lib/browse_everything/driver/dropbox.rb +++ b/lib/browse_everything/driver/dropbox.rb @@ -66,14 +66,14 @@ def authorized? private - def auth_flow - @csrf ||= {} - DropboxOAuth2Flow.new(config[:app_key], config[:app_secret], callback.to_s, @csrf, 'token') - end + def auth_flow + @csrf ||= {} + DropboxOAuth2Flow.new(config[:app_key], config[:app_secret], callback.to_s, @csrf, 'token') + end - def client - DropboxClient.new(token) - end + def client + DropboxClient.new(token) + end end end end diff --git a/lib/browse_everything/driver/file_system.rb b/lib/browse_everything/driver/file_system.rb index cf4c9ce7..a32f7be5 100644 --- a/lib/browse_everything/driver/file_system.rb +++ b/lib/browse_everything/driver/file_system.rb @@ -47,33 +47,33 @@ def details(path, display = File.basename(path)) private - def make_directory_entry(relative_path, real_path) - entries = [] - if relative_path.present? - entries << details(File.expand_path('..', real_path), '..') + def make_directory_entry(relative_path, real_path) + entries = [] + if relative_path.present? + entries << details(File.expand_path('..', real_path), '..') + end + entries + Dir[File.join(real_path, '*')].collect { |f| details(f) } end - entries + Dir[File.join(real_path, '*')].collect { |f| details(f) } - end - def sort_entries(entries) - entries.sort do |a, b| - if b.container? - a.container? ? a.name.downcase <=> b.name.downcase : 1 - else - a.container? ? -1 : a.name.downcase <=> b.name.downcase + def sort_entries(entries) + entries.sort do |a, b| + if b.container? + a.container? ? a.name.downcase <=> b.name.downcase : 1 + else + a.container? ? -1 : a.name.downcase <=> b.name.downcase + end end end - end - def make_pathname(path) - Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(config[:home])) - end + def make_pathname(path) + Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(config[:home])) + end - def file_size(path) - File.size(path).to_i - rescue - 0 - end + def file_size(path) + File.size(path).to_i + rescue + 0 + end end end end diff --git a/lib/browse_everything/driver/google_drive.rb b/lib/browse_everything/driver/google_drive.rb index e2ecccb3..a68e35b4 100644 --- a/lib/browse_everything/driver/google_drive.rb +++ b/lib/browse_everything/driver/google_drive.rb @@ -85,22 +85,22 @@ def drive private - def authorization - return @auth_client unless @auth_client.nil? - return nil unless token.present? - auth_client.update_token!(token) - self.token = auth_client.fetch_access_token! if auth_client.expired? - auth_client - end + def authorization + return @auth_client unless @auth_client.nil? + return nil unless token.present? + auth_client.update_token!(token) + self.token = auth_client.fetch_access_token! if auth_client.expired? + auth_client + end - def auth_client - @auth_client ||= Signet::OAuth2::Client.new token_credential_uri: 'https://www.googleapis.com/oauth2/v3/token', - authorization_uri: 'https://accounts.google.com/o/oauth2/auth', - scope: 'https://www.googleapis.com/auth/drive', - client_id: config[:client_id], - client_secret: config[:client_secret], - redirect_uri: callback - end + def auth_client + @auth_client ||= Signet::OAuth2::Client.new token_credential_uri: 'https://www.googleapis.com/oauth2/v3/token', + authorization_uri: 'https://accounts.google.com/o/oauth2/auth', + scope: 'https://www.googleapis.com/auth/drive', + client_id: config[:client_id], + client_secret: config[:client_secret], + redirect_uri: callback + end end end end diff --git a/lib/browse_everything/driver/sky_drive.rb b/lib/browse_everything/driver/sky_drive.rb index 81d214de..4a250c3b 100644 --- a/lib/browse_everything/driver/sky_drive.rb +++ b/lib/browse_everything/driver/sky_drive.rb @@ -100,26 +100,26 @@ def connect(params, _data) private - def oauth_client - Skydrive::Oauth::Client.new(config[:client_id], config[:client_secret], callback.to_s, 'wl.skydrive') - # TODO: error checking here - end + def oauth_client + Skydrive::Oauth::Client.new(config[:client_id], config[:client_secret], callback.to_s, 'wl.skydrive') + # TODO: error checking here + end - def rehydrate_token - return @rehydrate_token if @rehydrate_token - token_str = @token[:token] - token_expires = @token[:expires_at] - Rails.logger.warn "\n\n Rehydrating: #{@token} #{token_str} #{token_expires}" - @rehydrate_token = oauth_client.get_access_token_from_hash(token_str, expires_at: token_expires) - end + def rehydrate_token + return @rehydrate_token if @rehydrate_token + token_str = @token[:token] + token_expires = @token[:expires_at] + Rails.logger.warn "\n\n Rehydrating: #{@token} #{token_str} #{token_expires}" + @rehydrate_token = oauth_client.get_access_token_from_hash(token_str, expires_at: token_expires) + end - def safe_id(id) - id.tr('.', '-') - end + def safe_id(id) + id.tr('.', '-') + end - def real_id(id) - id.tr('-', '.') - end + def real_id(id) + id.tr('-', '.') + end end end end