lib/plugins/http.rb in auger-1.3.1 vs lib/plugins/http.rb in auger-1.3.2
- old
+ new
@@ -1,5 +1,25 @@
+## HTTP plugin for auger; requests look like this:
+##
+## http 80 do
+##
+## get "/foo" do
+## header "User-Agent: AugerExample/1.0"
+## test "HTTP Status Code" do |r|
+## Result r.code, r.code == 200
+## end
+## end
+##
+## post "/bar" do
+## data :a => "hello", :b => "world"
+## test "POST request body" do |r|
+## r.body
+## end
+## end
+##
+## end
+
require "net/http"
module Auger
class Project
@@ -15,14 +35,17 @@
end
class Http < Auger::Connection
def get(url, &block)
- @url = url
- @requests << Auger::HttpRequest.load(url, &block)
+ @requests << Auger::HttpGet.load(url, &block)
end
+ def post(url, &block)
+ @requests << Auger::HttpPost.load(url, &block)
+ end
+
def open(host, options)
http = Net::HTTP.new(host, options[:port])
http.use_ssl = options[:ssl]
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:insecure]
http.open_timeout = options[:timeout]
@@ -36,17 +59,23 @@
end
end
class HttpRequest < Auger::Request
- attr_accessor :headers, :user, :password
+ attr_accessor :method, :headers, :user, :password, :data
def initialize(url)
+ @method ||= :get # default
@headers = {}
+ @data = {}
super
end
+ def data(hash)
+ @data = hash
+ end
+
def header(h)
key, value = h.split /\s*:\s*/
@headers[key] = value
end
@@ -57,14 +86,29 @@
def password(password)
@password = password
end
def run(http)
- get = Net::HTTP::Get.new(@arg)
- get.basic_auth(@user, @password||'') if @user
- @headers.each { |k,v| get[k] = v }
- http.request(get)
+ request = Net::HTTP::const_get(@method.capitalize).new(@arg) # e.g. Net::HTTP::Get
+ request.basic_auth(@user, @password || '') if @user
+ @headers.each { |k,v| request[k] = v }
+ request.set_form_data(@data)
+ http.request(request)
end
+ end
+
+ class HttpGet < Auger::HttpRequest
+ def initialize(url)
+ @method = :get
+ super
+ end
+ end
+
+ class HttpPost < Auger::HttpRequest
+ def initialize(url)
+ @method = :post
+ super
+ end
end
end