lib/curb-fu/request/base.rb in curb-fu-0.4.4 vs lib/curb-fu/request/base.rb in curb-fu-0.6.0
- old
+ new
@@ -1,11 +1,11 @@
module CurbFu
class Request
module Base
include Common
- def build(url_params, query_params = {})
+ def build(url_params, query_params = {}, &block)
curb = Curl::Easy.new(build_url(url_params, query_params))
headers = global_headers
unless url_params.is_a?(String)
@@ -21,10 +21,12 @@
end
curb.headers = headers
curb.timeout = @timeout
+ yield curb if block_given?
+
curb
end
# Set headers to be used for every request
# * headers: hash of header names and values
@@ -36,27 +38,27 @@
# Returns: hash of header names and values
def global_headers
@global_headers ||= {}
end
- def get(url, params = {})
- curb = self.build(url, params)
+ def get(url, params = {}, &block)
+ curb = self.build(url, params, &block)
curb.http_get
CurbFu::Response::Base.from_curb_response(curb)
end
- def put(url, params = {})
- curb = self.build(url, params)
+ def put(url, params = {}, &block)
+ curb = self.build(url, params, &block)
curb.http_put("")
CurbFu::Response::Base.from_curb_response(curb)
end
- def post(url, params = {})
+ def post(url, params = {}, &block)
fields = create_post_fields(params)
fields = [fields] if fields.is_a?(String)
- curb = self.build(url)
+ curb = self.build(url, &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}"
@@ -64,36 +66,28 @@
puts "Body: #{response.body.inspect}"
end
response
end
- def post_file(url, params = {}, filez = {})
+ def post_file(url, params = {}, filez = {}, &block)
fields = create_post_fields(params)
fields += create_file_fields(filez)
- curb = self.build(url)
+ curb = self.build(url, &block)
curb.multipart_form_post = true
begin
curb.http_post(*fields)
rescue Curl::Err::InvalidPostFieldError => e
field_list = (params.merge(filez)).inject([]) { |list, (name, value)| list << "#{name} => #{value.to_s[0..49].inspect}"; list }
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)
- response = CurbFu::Response::Base.from_curb_response(curb)
- if CurbFu.debug?
- puts "Response from server was"
- puts "Status: #{response.status}"
- puts "Headers: #{response.headers.inspect}"
- puts "Body: #{response.body.inspect}"
- end
- response
end
- def delete(url)
- curb = self.build(url)
+ def delete(url, &block)
+ curb = self.build(url, &block)
curb.http_delete
CurbFu::Response::Base.from_curb_response(curb)
end
private
@@ -128,6 +122,6 @@
end
fields
end
end
end
-end
\ No newline at end of file
+end