lib/curb-fu/request/base.rb in curb-fu-0.6.0 vs lib/curb-fu/request/base.rb in curb-fu-0.6.1

- old
+ new

@@ -1,11 +1,11 @@ module CurbFu class Request module Base include Common - def build(url_params, query_params = {}, &block) + def build(url_params, query_params = {}, cookies = nil, &block) curb = Curl::Easy.new(build_url(url_params, query_params)) headers = global_headers unless url_params.is_a?(String) @@ -13,15 +13,18 @@ if url_params[:authtype] curb.http_auth_types = url_params[:authtype] elsif url_params[:username] curb.http_auth_types = CurbFu::Authentication::BASIC end - + + cookies ||= url_params[:cookies] + headers = headers.merge(url_params[:headers]) unless url_params[:headers].nil? headers["Expect"] = '' unless url_params[:headers] && url_params[:headers]["Expect"] end + curb.cookies = cookies if cookies curb.headers = headers curb.timeout = @timeout yield curb if block_given? @@ -38,27 +41,30 @@ # Returns: hash of header names and values def global_headers @global_headers ||= {} end - def get(url, params = {}, &block) - curb = self.build(url, params, &block) + def get(url, params = {}, cookies = nil, &block) + curb = self.build(url, params, cookies, &block) curb.http_get CurbFu::Response::Base.from_curb_response(curb) end - def put(url, params = {}, &block) - curb = self.build(url, params, &block) - curb.http_put("") + def put(url, params = {}, cookies = nil, &block) + fields = create_post_fields(params) + fields = [fields] if fields.is_a?(String) + + curb = self.build(url, {}, cookies, &block) + curb.http_put(*fields) CurbFu::Response::Base.from_curb_response(curb) end - def post(url, params = {}, &block) + def post(url, params = {}, cookies = nil, &block) fields = create_post_fields(params) fields = [fields] if fields.is_a?(String) - curb = self.build(url, &block) + curb = self.build(url, {}, cookies, &block) curb.http_post(*fields) response = CurbFu::Response::Base.from_curb_response(curb) if CurbFu.debug? puts "Response from server was" puts "Status: #{response.status}" @@ -66,15 +72,15 @@ puts "Body: #{response.body.inspect}" end response end - def post_file(url, params = {}, filez = {}, &block) + def post_file(url, params = {}, filez = {}, cookies = nil, &block) fields = create_post_fields(params) fields += create_file_fields(filez) - curb = self.build(url, &block) + curb = self.build(url, {}, cookies, &block) curb.multipart_form_post = true begin curb.http_post(*fields) rescue Curl::Err::InvalidPostFieldError => e @@ -82,11 +88,11 @@ raise e, "There was an attempt to post invalid fields. The fields were:\n#{field_list.join("\n")}" end CurbFu::Response::Base.from_curb_response(curb) end - def delete(url, &block) - curb = self.build(url, &block) + def delete(url, cookies = nil, &block) + curb = self.build(url, {}, cookies, &block) curb.http_delete CurbFu::Response::Base.from_curb_response(curb) end private