diff --git a/lib/passwordstate/client.rb b/lib/passwordstate/client.rb index c4ed1f70acea1c5eed78c8654487e098e179f158..02629919af02361aefda573ea8117150d888b39e 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 7ad6a4b10e9c86a3bdd470600343432c035fd8fc..0ba7d8e839a7b23bf28b4c6dd5dc1c9cb8d9833d 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 5265ceac3dc3936359f7abb03cb8769b487c5207..beb262df39f50fc7ee77fec2060b94985393aff0 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 6290d8c752c34c4f773e0a0b9e44ec0ac1c62a48..07b51892e7710c6273a2aa60935b8f92e7c4c40d 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 7a1ef64c63c2029596e0b9ad5e7df20b5e9d28cf..3984a2f64ac11833828a28fd5be37060cf769f99 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 dae0752ace92619b99f6737855bd7bd5ab9e8daa..1da57f7b16ec08af39aba239b7ae03b94aeeec69 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 46d8c3f9e82862b49d0545964ab56cc48deb0a74..6060136070cfdf39c39e137c1ee1f5a6abdc8fea 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