lib/you_track/client/real.rb in you_track-0.1.0 vs lib/you_track/client/real.rb in you_track-0.2.0

- old
+ new

@@ -1,9 +1,13 @@ class YouTrack::Client::Real attr_reader :url, :connection, :adapter, :username, :authenticated def initialize(options={}) + youtrack_file = YAML.load_file(File.expand_path("~/.youtrack")) + + options.merge!(youtrack_file) + requires(options, :url, :username, :password) @url = URI.parse(options[:url]) adapter = options[:adapter] || Faraday.default_adapter logger = options[:logger] || Logger.new(nil) custom_builder = options[:builder] || lambda { |*| } @@ -35,10 +39,17 @@ builder.adapter(*adapter) end end + def requires(options, *required) + missing = required.map do |required_param| + required_param if options[required_param].nil? + end.compact + raise RuntimeError, "Missing required options: #{missing.inspect}" unless missing.empty? + end + def request(options={}) # @note first request gets the cookie if !@authenticated && !@authenticating @authenticate_mutex.synchronize { @@ -53,15 +64,17 @@ @authenticated = true } end - method = options[:method] || :get - url = URI.parse(options[:url] || File.join(self.url.to_s, "/rest", options.fetch(:path))) - params = options[:params] || {} - body = options[:body] - headers = options[:headers] || {} - parser = options[:parser] + method = options[:method] || :get + query = options[:query] + url = URI.parse(options[:url] || File.join(self.url.to_s, "/rest", options.fetch(:path))) + url.query = query.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&') if query + params = options[:params] || {} + body = options[:body] + headers = options[:headers] || {} + parser = options[:parser] headers["Content-Type"] ||= if body.nil? if !params.empty? "application/x-www-form-urlencoded" else # Rails infers a Content-Type and we must set it here for the canonical string to match