lib/notify-integrity.rb in notify-integrity-0.0.1 vs lib/notify-integrity.rb in notify-integrity-0.1.0

- old
+ new

@@ -1,4 +1,45 @@ +require "net/http" +require "uri/http" +require "mechanize" + class NotifyIntegrity - #nya + + # # + # Adds authentication information of Integrity instance. + # + def auth user, pass + @integrity_user = user + @integrity_pass = pass + end + + # uses Net::HTTP + def post_request host, path, payload = {} + uri = URI("#{host}#{path}") + req = Net::HTTP::Post.new uri.path + req.basic_auth @integrity_user, @integrity_pass unless @integrity_user.nil? + req.set_form_data payload + + result = nil + Net::HTTP.start(uri.hostname, uri.port) do |http| + result = http.request(req) + end + + if result.instance_of? Net::HTTPNotFound + raise Net::HTTPNotFound, "Requested page does not exist (404). host: #{host}, path: #{path}" + result = nil + end + + result + end + + # uses Mechanize + def request_page uri + raise "invalid uri" if uri.nil? + raise "integrity user or pass missing" if @integrity_user.nil? or @integrity_pass.nil? + + agent = Mechanize.new + agent.add_auth uri, @integrity_user, @integrity_pass + agent.get uri + end end \ No newline at end of file