lib/runcible/base.rb in runcible-1.2.0 vs lib/runcible/base.rb in runcible-1.3.0

- old
+ new

@@ -24,15 +24,13 @@ require 'rest_client' require 'oauth' require 'json' require 'thread' - module Runcible class Base - - def initialize(config={}) + def initialize(config = {}) @mutex = Mutex.new @config = config end def lazy_config=(a_block) @@ -40,41 +38,38 @@ end def config @mutex.synchronize do @config = @lazy_config.call if defined?(@lazy_config) - raise Runcible::ConfigurationUndefinedError, Runcible::ConfigurationUndefinedError.message unless @config + fail Runcible::ConfigurationUndefinedError, Runcible::ConfigurationUndefinedError.message unless @config @config end end def path(*args) self.class.path(*args) end - def call(method, path, options={}) + def call(method, path, options = {}) clone_config = self.config.clone #on occation path will already have prefix (sync cancel) - path = clone_config[:api_path] + path if !path.start_with?(clone_config[:api_path]) + path = clone_config[:api_path] + path unless path.start_with?(clone_config[:api_path]) RestClient.log = [] - logger = clone_config[:logging][:logger] - debug_logging = clone_config[:logging][:debug] - exception_logging = clone_config[:logging][:exception] - headers = clone_config[:headers].clone get_params = options[:params] if options[:params] path = combine_get_params(path, get_params) if get_params client_options = {} client_options[:timeout] = clone_config[:timeout] if clone_config[:timeout] client_options[:open_timeout] = clone_config[:open_timeout] if clone_config[:open_timeout] + client_options[:verify_ssl] = clone_config[:verify_ssl] unless clone_config[:verify_ssl].nil? if clone_config[:oauth] headers = add_oauth_header(method, path, headers) if clone_config[:oauth] - headers["pulp-user"] = clone_config[:user] + headers['pulp-user'] = clone_config[:user] client = RestClient::Resource.new(clone_config[:url], client_options) else client_options[:user] = clone_config[:user] client_options[:password] = config[:http_auth][:password] client = RestClient::Resource.new(clone_config[:url], client_options) @@ -91,25 +86,26 @@ log_exception raise e end def get_response(client, path, *args) - client[path].send(*args) do |response, request, result, &block| + client[path].send(*args) do |response, request, result, &_block| resp = response.return!(request, result) log_debug return resp end end def combine_get_params(path, params) - query_string = params.collect do |k, v| + query_string = params.map do |k, v| if v.is_a? Array - v.collect{|y| "#{k.to_s}=#{y.to_s}" }.join('&') + v.map { |y| "#{k}=#{y}" }.join('&') else - "#{k.to_s}=#{v.to_s}" + "#{k}=#{v}" end - end.flatten().join('&') + end + query_string = query_string.flatten.join('&') path + "?#{query_string}" end def generate_payload(options) if options[:payload].is_a?(String) @@ -143,33 +139,34 @@ begin body = JSON.parse(response.body) if body.respond_to? :with_indifferent_access body = body.with_indifferent_access elsif body.is_a? Array - body = body.collect do |i| + body = body.map do |i| i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i end end response = RestClient::Response.create(body, response.net_http_res, response.args) rescue JSON::ParserError + log_exception end return response end - def required_params(local_names, binding, keys_to_remove=[]) - local_names = local_names.reduce({}) do |acc, v| + def required_params(local_names, binding, keys_to_remove = []) + local_names = local_names.each_with_object({}) do |v, acc| value = binding.eval(v.to_s) unless v == :_ acc[v] = value unless value.nil? acc end #The double delete is to support 1.8.7 and 1.9.3 local_names.delete(:payload) local_names.delete(:optional) - local_names.delete("payload") - local_names.delete("optional") + local_names.delete('payload') + local_names.delete('optional') keys_to_remove.each do |key| local_names.delete(key) local_names.delete(key.to_sym) end @@ -181,13 +178,13 @@ end def add_oauth_header(method, path, headers) default_options = { :site => config[:url], :http_method => method, - :request_token_path => "", - :authorize_path => "", - :access_token_path => "" } + :request_token_path => '', + :authorize_path => '', + :access_token_path => '' } default_options[:ca_file] = config[:ca_cert_file] unless config[:ca_cert_file].nil? consumer = OAuth::Consumer.new(config[:oauth][:oauth_key], config[:oauth][:oauth_secret], default_options) method_to_http_request = { :get => Net::HTTP::Get, @@ -202,11 +199,11 @@ return headers end def log_debug if self.config[:logging][:debug] - log_message = generate_log_message + log_message = generate_log_message self.config[:logging][:logger].debug(log_message) end end def log_exception @@ -221,16 +218,14 @@ end def logger self.config[:logging][:logger] end + end - end - class ConfigurationUndefinedError < StandardError - def self.message # override me to change the error message - "Configuration not set. Runcible::Base.config= must be called before Runcible::Base.config." + 'Configuration not set. Runcible::Base.config= must be called before Runcible::Base.config.' end end end