lib/gooddata/connection.rb in gooddata-0.5.2 vs lib/gooddata/connection.rb in gooddata-0.5.3

- old
+ new

@@ -66,11 +66,10 @@ # # === Examples # # Connection.new(username, password).get '/gdc/projects' def get(path, options = {}) - pp options GoodData.logger.debug "GET #{path}" ensure_connection b = Proc.new { @server[path].get cookies } response = (options[:process] == false) ? (b.call) : process_response(options, &b) end @@ -145,23 +144,41 @@ # We should have followed a link. If it was correct. stage_url = DEFAULT_URL.sub(/\./, '-di.') # Make a directory, if needed if dir then - method = :mkcol url = stage_url + STAGE_PATH + dir + '/' + method = :get GoodData.logger.debug "#{method}: #{url}" - RestClient::Request.execute( - :method => method, - :url => url, - :user => @username, - :password => @password, - :timeout => @options[:timeout], - :headers => { - :user_agent => GoodData.gem_version_string - } - ) + begin + # first check if it does exits + RestClient::Request.execute( + :method => method, + :url => url, + :user => @username, + :password => @password, + :timeout => @options[:timeout], + :headers => { + :user_agent => GoodData.gem_version_string + } + ) + rescue RestClient::Exception => e + if e.http_code == 404 then + method = :mkcol + GoodData.logger.debug "#{method}: #{url}" + RestClient::Request.execute( + :method => method, + :url => url, + :user => @username, + :password => @password, + :timeout => @options[:timeout], + :headers => { + :user_agent => GoodData.gem_version_string + } + ) + end + end end # Upload the file RestClient::Request.execute( :method => :put, @@ -172,9 +189,25 @@ :headers => { :user_agent => GoodData.gem_version_string, }, :payload => File.read(file) ) + end + + def download(what, where) + stage_url = DEFAULT_URL.sub(/\./, '-di.') + url = stage_url + STAGE_PATH + what + File.open(where, 'w') do |f| + resp = RestClient::Request.execute({ + :method => 'GET', + :url => url, + :user => @username, + :password => @password, + :timeout => 0 + }) do |chunk, x, y| + f.write chunk + end + end end private def ensure_connection