lib/bare/curl.rb in bare-0.1.0 vs lib/bare/curl.rb in bare-0.1.1
- old
+ new
@@ -12,24 +12,23 @@
def setCookie(url, parameters={}, https=false)
uri, http = setupHttp(url, https)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(parameters)
+
response = http.request(request)
cookie = response.response['set-cookie']
self.cookie = cookie
+
return cookie
end
def get(url, parameters={}, https=false, cookie=self.cookie)
reset_attr
full_url = url
+ full_url += "?" unless parameters.empty?
- if !parameters.empty?
- full_url += "?"
- end
-
parameters.each_with_index do |(k,v), index|
if index == 0
full_url += "#{k.to_s}=#{v.to_s}"
else
full_url += "&#{k.to_s}=#{v.to_s}"
@@ -38,13 +37,11 @@
uri, http = setupHttp(full_url, https)
request = Net::HTTP::Get.new(uri.request_uri)
- if !cookie.nil?
- request['Cookie'] = cookie
- end
+ request['Cookie'] = cookie unless cookie.nil?
set_attr(http.request(request))
return self.response
end
@@ -53,13 +50,11 @@
uri, http = setupHttp(url, https)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(parameters)
- if !cookie.nil?
- request['Cookie'] = cookie
- end
+ request['Cookie'] = cookie unless cookie.nil?
set_attr(http.request(request))
return self.response
end
@@ -68,28 +63,30 @@
uri, http = setupHttp(url, https)
request = Net::HTTP::Put.new(uri.request_uri)
request.set_form_data(parameters)
- if !cookie.nil?
- request['Cookie'] = cookie
- end
+ request['Cookie'] = cookie unless cookie.nil?
set_attr(http.request(request))
return self.response
end
- def delete(url, https=false, cookie=self.cookie)
+ def delete(url, parameters={}, https=false, cookie=self.cookie)
reset_attr
uri, http = setupHttp(url, https)
request = Net::HTTP::Delete.new(uri.request_uri)
+ request.set_form_data(parameters)
+ request['Cookie'] = cookie unless cookie.nil?
+
set_attr(http.request(request))
return self.response
end
protected
+
def reset_attr
self.body = nil
self.code = nil
self.response = nil
end