lib/ridley/connection.rb in ridley-0.0.2 vs lib/ridley/connection.rb in ridley-0.0.3

- old
+ new

@@ -3,10 +3,22 @@ class Connection class << self def sync(options, &block) new(options).sync(&block) end + + # @raise [ArgumentError] + # + # @return [Boolean] + def validate_options(options) + missing = (REQUIRED_OPTIONS - options.keys) + + unless missing.empty? + missing.collect! { |opt| "'#{opt}'" } + raise ArgumentError, "Missing required option(s): #{missing.join(', ')}" + end + end end extend Forwardable include Ridley::DSL @@ -37,12 +49,19 @@ ] DEFAULT_THREAD_COUNT = 8 # @option options [String] :server_url + # URL to the Chef API # @option options [String] :client_name + # name of the client used to authenticate with the Chef API # @option options [String] :client_key + # filepath to the client's private key used to authenticate with + # the Chef API + # @option options [String] :organization + # the Organization to connect to. This is only used if you are connecting to + # private Chef or hosted Chef # @option options [Integer] :thread_count # @option options [Hash] :params # URI query unencoded key/value pairs # @option options [Hash] :headers # unencoded HTTP header key/value pairs @@ -51,27 +70,25 @@ # @option options [Hash] :ssl # SSL options # @option options [URI, String, Hash] :proxy # URI, String, or Hash of HTTP proxy options def initialize(options = {}) - options[:thread_count] ||= DEFAULT_THREAD_COUNT + self.class.validate_options(options) - validate_options(options) + @client_name = options.fetch(:client_name) + @client_key = options.fetch(:client_key) + @organization = options.fetch(:organization, nil) + @thread_count = options.fetch(:thread_count, DEFAULT_THREAD_COUNT) - @client_name = options[:client_name] - @client_key = options[:client_key] - @organization = options[:organization] - @thread_count = options[:thread_count] - faraday_options = options.slice(:params, :headers, :request, :ssl, :proxy) uri_hash = Addressable::URI.parse(options[:server_url]).to_hash.slice(:scheme, :host, :port) unless uri_hash[:port] uri_hash[:port] = (uri_hash[:scheme] == "https" ? 443 : 80) end - if organization + unless organization.nil? uri_hash[:path] = "/organizations/#{organization}" end server_uri = Addressable::URI.new(uri_hash) @@ -116,16 +133,8 @@ instance_eval(&block) end def method_missing(method, *args, &block) @self_before_instance_eval.send(method, *args, &block) - end - - def validate_options(options) - missing = REQUIRED_OPTIONS - options.keys - unless missing.empty? - missing.collect! { |opt| "'#{opt}'" } - raise ArgumentError, "missing required option(s): #{missing.join(', ')}" - end end end end