lib/mirage/web.rb in mirage-1.3.6 vs lib/mirage/web.rb in mirage-2.0.0.alpha1
- old
+ new
@@ -1,49 +1,58 @@
+require 'net/http'
+require 'uri'
module Mirage
module Web
class FileResponse
+ attr_reader :response
def initialize response
@response = response
end
def save_as path
@response.save_as(path)
end
end
- def http_get url, params={}
- using_mechanize do |browser|
- params[:body] ? browser.put(url, params[:body]) : browser.get(url, params)
+ def put url, entity, headers={}
+ uri = URI.parse(url)
+ request = Net::HTTP::Put.new(uri.request_uri)
+
+ if entity.is_a? File
+ request.body_stream=entity
+ request.content_length=entity.lstat.size
+ else
+ request.body=entity
end
+ headers.each { |field, value| request.add_field(field, value) }
+
+ Net::HTTP.new(uri.host, uri.port).request(request)
end
- def http_post url, params={}
- using_mechanize do |browser|
- browser.post(url, params)
- end
+ def get url, params={}, headers={}
+ uri = URI.parse(url)
+ request = Net::HTTP::Get.new(uri.request_uri)
+ request.set_form_data params
+ headers.each { |field, value| request.add_field(field, value) }
+ Net::HTTP.new(uri.host, uri.port).request(request)
end
- private
- def using_mechanize
- begin
- browser = Mechanize.new
- browser.keep_alive = false
- response = yield browser
+ def post url, params={}, headers={}
+ uri = URI.parse(url)
+ request = Net::HTTP::Post.new(uri.request_uri)
+
+ params.is_a?(Hash) ? request.set_form_data(params) : request.body = params
+
+ headers.each { |field, value| request.add_field(field, value) }
+ Net::HTTP.new(uri.host, uri.port).request(request)
+ end
- def response.code
- @code.to_i
- end
- rescue Exception => e
- response = e
-
- def response.code
- self.response_code.to_i
- end
-
- def response.body
- ""
- end
- end
- response
+ def delete url, params={}, headers={}
+ uri = URI.parse(url)
+ request = Net::HTTP::Delete.new(uri.request_uri)
+ params.is_a?(Hash) ? request.set_form_data(params) : request.body = params
+ headers.each { |field, value| request.add_field(field, value) }
+ Net::HTTP.new(uri.host, uri.port).request(request)
end
+
end
end