Sha256: fc2c46abf2f9c09acbac4ca0ef4b426723fea7d0ed11f71eb0c6c019934c2bac

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

module Janky
  module GitHub
    class API
      def initialize(url, user, password)
        @url = url
        @user = user
        @password = password
      end

      def create(nwo, secret, url)
        request = Net::HTTP::Post.new(build_path("repos/#{nwo}/hooks"))
        payload = build_payload(url, secret)
        request.body = Yajl.dump(payload)
        request.basic_auth(@user, @password)

        http.request(request)
      end

      def trigger(hook_url)
        path    = build_path(URI(hook_url).path + "/test")
        request = Net::HTTP::Post.new(path)
        request.basic_auth(@user, @password)

        http.request(request)
      end

      def get(hook_url)
        path    = build_path(URI(hook_url).path)
        request = Net::HTTP::Get.new(path)
        request.basic_auth(@user, @password)

        http.request(request)
      end

      def repo_get(nwo)
        path    = build_path("repos/#{nwo}")
        request = Net::HTTP::Get.new(path)
        request.basic_auth(@user, @password)

        http.request(request)
      end

      def build_path(path)
        if path[0] == ?/
          URI.join(@url, path[1..-1]).path
        else
          URI.join(@url, path).path
        end
      end

      def build_payload(url, secret)
        { "name"   => "web",
          "active" => true,
          "config" => {
            "url"          => url,
            "secret"       => secret,
            "content_type" => "json"
          }
        }
      end

      def http
        @http ||= http!
      end

      def http!
        uri  = URI(@url)
        http = Net::HTTP.new(uri.host, uri.port)

        http.use_ssl     = true
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        http.ca_path     = "/etc/ssl/certs"

        http
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
janky-0.9.12 lib/janky/github/api.rb
janky-0.9.11 lib/janky/github/api.rb
janky-0.9.10 lib/janky/github/api.rb
janky-0.9.9 lib/janky/github/api.rb