From 8077731abb31926aa2c0c1ca0375e8199f6554f8 Mon Sep 17 00:00:00 2001 From: Alexander Olofsson <alexander.olofsson@liu.se> Date: Wed, 23 Oct 2019 14:33:43 +0200 Subject: [PATCH] Fix rubocop complaints --- lib/passwordstate/client.rb | 2 +- lib/passwordstate/resource.rb | 5 ++--- lib/passwordstate/resource_list.rb | 9 +++++++++ lib/passwordstate/resources/folder.rb | 1 + lib/passwordstate/resources/password.rb | 3 +++ lib/passwordstate/resources/report.rb | 18 +++++++++--------- lib/passwordstate/util.rb | 3 ++- 7 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/passwordstate/client.rb b/lib/passwordstate/client.rb index c4ed1f7..0262991 100644 --- a/lib/passwordstate/client.rb +++ b/lib/passwordstate/client.rb @@ -80,7 +80,7 @@ module Passwordstate def request(method, api_path, options = {}) uri = URI(server_url + "/#{api_type}/" + api_path) uri.query = URI.encode_www_form(options.fetch(:query)) if options.key? :query - uri.query = nil if uri.query&.empty? + uri.query = nil unless uri.query&.any? req_obj = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new uri if options.key? :body diff --git a/lib/passwordstate/resource.rb b/lib/passwordstate/resource.rb index 7ad6a4b..0ba7d8e 100644 --- a/lib/passwordstate/resource.rb +++ b/lib/passwordstate/resource.rb @@ -46,9 +46,7 @@ module Passwordstate def self.get(client, object, query = {}) object = object.send(object.class.send(index_field)) if object.is_a? Resource - if query[:_bare] - return new _client: client, index_field => object - end + return new _client: client, index_field => object if query[:_bare] path = query.fetch(:_api_path, api_path) query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' } @@ -57,6 +55,7 @@ module Passwordstate new data.merge(_client: client) end return resp.first if resp.one? || resp.empty? + resp end diff --git a/lib/passwordstate/resource_list.rb b/lib/passwordstate/resource_list.rb index 5265cea..beb262d 100644 --- a/lib/passwordstate/resource_list.rb +++ b/lib/passwordstate/resource_list.rb @@ -2,6 +2,7 @@ module Passwordstate class ResourceList < Array Array.public_instance_methods(false).each do |method| next if %i[reject select slice clear inspect].include?(method.to_sym) + class_eval <<-EVAL, __FILE__, __LINE__ + 1 def #{method}(*args) lazy_load unless @loaded @@ -57,6 +58,7 @@ module Passwordstate return nil unless %i[search all get post put delete].include?(operation) return false if options.key?(:only) && !options[:only].include?(operation) return false if options.key?(:except) && options[:except].include?(operation) + !options.fetch("#{operation}_path".to_sym, '').nil? end @@ -66,6 +68,7 @@ module Passwordstate def create(data) raise 'Operation not supported' unless operation_supported?(:post) + obj = resource.new options.fetch(:object_data, {}).merge(data).merge(_client: client) obj.post obj @@ -73,6 +76,7 @@ module Passwordstate def search(query = {}) raise 'Operation not supported' unless operation_supported?(:search) + api_path = options.fetch(:search_path, resource.api_path) query = options.fetch(:search_query, {}).merge(query) @@ -81,6 +85,7 @@ module Passwordstate def all(query = {}) raise 'Operation not supported' unless operation_supported?(:all) + api_path = options.fetch(:all_path, resource.api_path) query = options.fetch(:all_query, {}).merge(query) @@ -89,6 +94,7 @@ module Passwordstate def get(id, query = {}) raise 'Operation not supported' unless operation_supported?(:get) + api_path = options.fetch(:get_path, resource.api_path) query = options.fetch(:get_query, {}).merge(query) @@ -97,6 +103,7 @@ module Passwordstate def post(data, query = {}) raise 'Operation not supported' unless operation_supported?(:post) + api_path = options.fetch(:post_path, resource.api_path) query = options.fetch(:post_query, {}).merge(query) @@ -105,6 +112,7 @@ module Passwordstate def put(data, query = {}) raise 'Operation not supported' unless operation_supported?(:put) + api_path = options.fetch(:put_path, resource.api_path) query = options.fetch(:put_query, {}).merge(query) @@ -113,6 +121,7 @@ module Passwordstate def delete(id, query = {}) raise 'Operation not supported' unless operation_supported?(:delete) + api_path = options.fetch(:delete_path, resource.api_path) query = options.fetch(:delete_query, {}).merge(query) diff --git a/lib/passwordstate/resources/folder.rb b/lib/passwordstate/resources/folder.rb index 6290d8c..07b5189 100644 --- a/lib/passwordstate/resources/folder.rb +++ b/lib/passwordstate/resources/folder.rb @@ -30,6 +30,7 @@ module Passwordstate def full_path(unix = false) return tree_path.tr('\\', '/') if unix + tree_path end end diff --git a/lib/passwordstate/resources/password.rb b/lib/passwordstate/resources/password.rb index 7a1ef64..3984a2f 100644 --- a/lib/passwordstate/resources/password.rb +++ b/lib/passwordstate/resources/password.rb @@ -57,6 +57,7 @@ module Passwordstate def history raise 'Password history only available on stored passwords' unless stored? + Passwordstate::ResourceList.new client, PasswordHistory, all_path: "passwordhistory/#{password_id}", only: :all @@ -73,6 +74,7 @@ module Passwordstate def add_dependency(data = {}) raise 'Password dependency creation only available for stored passwords' unless stored? + client.request :post, 'dependencies', body: self.class.passwordstatify_hash(data.merge(password_id: password_id)) end @@ -87,6 +89,7 @@ module Passwordstate def self.generate(client, options = {}) results = client.request(:get, 'generatepassword', query: options).map { |r| r['Password'] } return results.first if results.count == 1 + results end end diff --git a/lib/passwordstate/resources/report.rb b/lib/passwordstate/resources/report.rb index dae0752..1da57f7 100644 --- a/lib/passwordstate/resources/report.rb +++ b/lib/passwordstate/resources/report.rb @@ -2,15 +2,15 @@ module Passwordstate module Resources class Report < Passwordstate::Resource REPORT_PARAMETERS = { - 1 => %i[user_id], - 2 => %i[user_id site_id], - 3 => %i[user_id duration], - 4 => %i[user_id site_id duration], - 5 => %i[duration], - 6 => %i[], - 7 => %i[user_id site_id duration], - 8 => %i[], - 9 => %i[], + 1 => %i[user_id], + 2 => %i[user_id site_id], + 3 => %i[user_id duration], + 4 => %i[user_id site_id duration], + 5 => %i[duration], + 6 => %i[], + 7 => %i[user_id site_id duration], + 8 => %i[], + 9 => %i[], 10 => %i[duration], 11 => %i[duration], 12 => %i[site_id], diff --git a/lib/passwordstate/util.rb b/lib/passwordstate/util.rb index 46d8c3f..6060136 100644 --- a/lib/passwordstate/util.rb +++ b/lib/passwordstate/util.rb @@ -33,6 +33,7 @@ class String def find_line(&_block) raise ArgumentError, 'No block given' unless block_given? + each_line do |line| return line if yield line end @@ -76,4 +77,4 @@ module Passwordstate end end -Net::HTTP.send :prepend, Passwordstate::NetHTTPExtensions unless Net::HTTP.ancestors.include? Passwordstate::NetHTTPExtensions +Net::HTTP.prepend Passwordstate::NetHTTPExtensions unless Net::HTTP.ancestors.include? Passwordstate::NetHTTPExtensions -- GitLab