Skip to content

Commit

Permalink
Rubocop autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
lleirborras committed Apr 22, 2024
1 parent a4a75b5 commit 03b4b98
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 151 deletions.
53 changes: 27 additions & 26 deletions lib/sharepoint/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Client

attr_accessor :token

def authenticating(&block)
def authenticating
get_token
yield
end
Expand All @@ -24,7 +24,7 @@ def get_token
end

def bearer_auth
"Bearer #{token.to_s}"
"Bearer #{token}"
end

# @return [OpenStruct] The current configuration.
Expand Down Expand Up @@ -376,7 +376,7 @@ def upload(filename, content, path, site_path = nil)
url = uri_escape "#{url}GetFolderByServerRelativeUrl('#{path}')/Files/Add(url='#{sanitized_filename}',overwrite=true)"
easy = ethon_easy_json_requester
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path) })
'X-RequestDigest' => xrequest_digest(site_path) })
easy.http_request(url, :post, { body: content })
easy.perform
check_and_raise_failure(easy)
Expand Down Expand Up @@ -404,11 +404,11 @@ def update_metadata(filename, metadata, path, site_path = nil)
prepared_metadata = prepare_metadata(metadata, __metadata['type'])

easy = ethon_easy_json_requester
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'content-type' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path),
'X-Http-Method' => 'PATCH',
'If-Match' => "*" })
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'content-type' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path),
'X-Http-Method' => 'PATCH',
'If-Match' => '*' })
easy.http_request(update_metadata_url,
:post,
{ body: prepared_metadata })
Expand Down Expand Up @@ -486,10 +486,10 @@ def process_url(url, fields)
parsed_response_body = JSON.parse(easy.response_body)

page_content = if fields
parsed_response_body['d']['results'].map{|v|v.fetch_values(*fields)}
else
parsed_response_body['d']['results']
end
parsed_response_body['d']['results'].map { |v| v.fetch_values(*fields) }
else
parsed_response_body['d']['results']
end

if next_url = parsed_response_body['d']['__next']
page_content + process_url(next_url, fields)
Expand All @@ -513,7 +513,7 @@ def with_bearer_authentication_header(h)
end

def bearer_auth_header
{"Authorization" => bearer_auth }
{ 'Authorization' => bearer_auth }
end

def base_url
Expand Down Expand Up @@ -542,7 +542,7 @@ def computed_web_api_url(site)

def ethon_easy_json_requester
easy = ethon_easy_requester
easy.headers = with_bearer_authentication_header({ 'accept'=> 'application/json;odata=verbose'})
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose' })
easy
end

Expand Down Expand Up @@ -629,33 +629,34 @@ def extract_paths(url)
end

def validate_token_config
valid_config_options( %i(client_id client_secret tenant_id cert_name auth_scope) )
valid_config_options(%i[client_id client_secret tenant_id cert_name auth_scope])
end

def validate_ntlm_config
valid_config_options( %i(username password) )
valid_config_options(%i[username password])
end

def valid_config_options(options = [])
options.map do |opt|
c = config.send(opt)

next if c.present? && string_not_blank?(c)

opt
end.compact
end

def validate_config!
raise Errors::InvalidAuthenticationError.new unless valid_authentication?(config.authentication)
raise Errors::InvalidAuthenticationError.new unless valid_authentication?(config.authentication)

if config.authentication == "token"
if config.authentication == 'token'
invalid_token_opts = validate_token_config

raise Errors::InvalidTokenConfigError.new(invalid_token_opts) unless invalid_token_opts.empty?
raise Errors::UriConfigurationError.new unless valid_uri?(config.auth_scope)
end

if config.authentication == "ntlm"
if config.authentication == 'ntlm'
invalid_ntlm_opts = validate_ntlm_config

raise Errors::InvalidNTLMConfigError.new(invalid_ntlm_opts) unless invalid_ntlm_opts.empty?
Expand All @@ -672,14 +673,14 @@ def string_not_blank?(object)
def valid_uri?(which)
if which and which.is_a? String
uri = URI.parse(which)
uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
else
false
end
end

def valid_authentication?(which)
%w(ntlm token).include?(which)
%w[ntlm token].include?(which)
end

# Waiting for RFC 3986 to be implemented, we need to escape square brackets
Expand Down Expand Up @@ -834,11 +835,11 @@ def update_object_metadata(metadata, new_metadata, site_path = '')
prepared_metadata = prepare_metadata(new_metadata, metadata['type'])

easy = ethon_easy_json_requester
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'content-type' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path),
'X-Http-Method' => 'PATCH',
'If-Match' => "*" })
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'content-type' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path),
'X-Http-Method' => 'PATCH',
'If-Match' => '*' })

easy.http_request(update_metadata_url,
:post,
Expand Down
21 changes: 10 additions & 11 deletions lib/sharepoint/client/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ module Sharepoint
class Client
class Token
class InvalidTokenError < StandardError
end

attr_accessor :expires_in
attr_accessor :access_token
attr_accessor :fetched_at
end

attr_accessor :expires_in, :access_token, :fetched_at
attr_reader :config

def initialize(config)
Expand All @@ -15,6 +13,7 @@ def initialize(config)

def get_or_fetch
return access_token unless access_token.nil? || expired?

fetch
end

Expand All @@ -25,10 +24,10 @@ def to_s
def fetch
response = request_new_token

details = response["Token"]
details = response['Token']
self.fetched_at = Time.now.utc.to_i
self.expires_in = details["expires_in"]
self.access_token = details["access_token"]
self.expires_in = details['expires_in']
self.access_token = details['access_token']
end

private
Expand All @@ -39,8 +38,8 @@ def expired?
(fetched_at + expires_in) < Time.now.utc.to_i
end

def
def request_new_token
def
def(_request_new_token)
auth_request = {
client_id: config.client_id,
client_secret: config.client_secret,
Expand All @@ -49,7 +48,7 @@ def request_new_token
auth_scope: config.auth_scope
}.to_json

headers = {'Content-Type' => 'application/json'}
headers = { 'Content-Type' => 'application/json' }

ethon = Ethon::Easy.new(followlocation: true)
ethon.http_request(config.token_url, :post, body: auth_request, headers: headers)
Expand Down
6 changes: 3 additions & 3 deletions lib/sharepoint/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize

class InvalidAuthenticationError < StandardError
def initialize
super "Invalid authentication mechanism"
super('Invalid authentication mechanism')
end
end

Expand All @@ -24,7 +24,7 @@ def initialize(invalid_entries)
"Invalid #{e} in Token configuration"
end

super error_messages.join(',')
super(error_messages.join(','))
end
end

Expand All @@ -34,7 +34,7 @@ def initialize(invalid_entries)
"Invalid #{e} in NTLM configuration"
end

super error_messages.join(',')
super(error_messages.join(','))
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/sharepoint/spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def value_to_string(value)

def sp_config
{
uri: ENV['SP_URL'],
authentication: ENV['SP_AUTHENTICATION'],
client_id: ENV['SP_CLIENT_ID'],
client_secret: ENV['SP_CLIENT_SECRET'],
tenant_id: ENV['SP_TENANT_ID'],
cert_name: ENV['SP_CERT_NAME'],
auth_scope: ENV['SP_AUTH_SCOPE'],
username: ENV['SP_USERNAME'],
password: ENV['SP_PASSWORD']
uri: ENV.fetch('SP_URL', nil),
authentication: ENV.fetch('SP_AUTHENTICATION', nil),
client_id: ENV.fetch('SP_CLIENT_ID', nil),
client_secret: ENV.fetch('SP_CLIENT_SECRET', nil),
tenant_id: ENV.fetch('SP_TENANT_ID', nil),
cert_name: ENV.fetch('SP_CERT_NAME', nil),
auth_scope: ENV.fetch('SP_AUTH_SCOPE', nil),
username: ENV.fetch('SP_USERNAME', nil),
password: ENV.fetch('SP_PASSWORD', nil)
}
end

Expand All @@ -34,7 +34,7 @@ def mock_requests
def mock_token_responses
allow_any_instance_of(Sharepoint::Client::Token)
.to receive(:request_new_token)
.and_return({"Token" => { "expires_in" => 3600, "access_token" => "access_token" }})
.and_return({ 'Token' => { 'expires_in' => 3600, 'access_token' => 'access_token' } })
end

def mock_responses(fixture_file)
Expand Down
Loading

0 comments on commit 03b4b98

Please sign in to comment.