class AutoJenkins::Helpers

Public Class Methods

_build_url(jurl, command, args) click to toggle source
# File lib/autojenkins.rb, line 121
def self._build_url(jurl, command, args)
    return URI.parse("#{jurl}" + (URLs[command] % args))
end
_get_request(jurl, command, args, auth, post_args=nil, content_type=nil) click to toggle source
# File lib/autojenkins.rb, line 58
def self._get_request(jurl, command, args, auth, 
                      post_args=nil, content_type=nil)

    url = Helpers._build_url(jurl, command, args)

    url_str = (url.query == nil) ? url.path : "#{url.path}?#{url.query}"

    if post_args.nil?:
        req = Net::HTTP::Get.new(url_str)
    else
        req = Net::HTTP::Post.new(url_str)
        req.body = post_args
    end

    req.basic_auth auth[LOGIN], auth[PASSWD] 

    res = Net::HTTP.start(url.host, url.port) {|http|

        if content_type
            req["Content-Type"] = content_type
        end

        ret = http.request(req)

        if not [Net::HTTPOK, Net::HTTPFound, Net::HTTPNotFound].include? ret.class 
            if ret.instance_of? Net::HTTPUnauthorized
                raise ExUnauthorized, "Unauthorized, check your credentials" 
            else
                raise ExUndefined, "Http request failed!"
            end
        end

        ret
    }

    #Some requests do not return JSON. Such as config.xml
    ct = res.get_fields('Content-type')

    if ct.nil?
        ct = "text/plain"
    else
        ct = ct[0] 
    end

    ctype = ct.split(';')[0]

    if ctype == 'application/javascript' or
            ctype == 'application/json'
        #handle JSON
        begin
            return JSON.parse(res.body)
        rescue JSON::ParserError
            return "{}"
        end
    end

    unless res.body.nil?
        return res.body
    end

    return "{}"
end
parseConfig(config) click to toggle source
# File lib/autojenkins.rb, line 40
def self.parseConfig(config)
    tree = {}

    begin
        tree = YAML::parse(File.open(config)).transform
    rescue 
        raise IOError, "Config file not found"
    end

    ['URL', 'USER', 'PASSWD'].each do |k|
        if not tree.has_key?(k)
            raise IndexError.new("Key %s not found in config file" % k)
        end
    end

    return tree
end