Class: Janky::GitHub::API

Inherits:
Object
  • Object
show all
Defined in:
lib/janky/github/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, user, password) ⇒ API

Returns a new instance of API.



4
5
6
7
8
# File 'lib/janky/github/api.rb', line 4

def initialize(url, user, password)
  @url = url
  @user = user
  @password = password
end

Instance Method Details

#branch(nwo, branch) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/janky/github/api.rb', line 51

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

  http.request(request)
end

#build_path(path) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/janky/github/api.rb', line 67

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

#build_payload(url, secret) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/janky/github/api.rb', line 75

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

#commit(nwo, sha) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/janky/github/api.rb', line 59

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

  http.request(request)
end

#create(nwo, secret, url) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/janky/github/api.rb', line 10

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

#delete(hook_url) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/janky/github/api.rb', line 19

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

  http.request(request)
end

#get(hook_url) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/janky/github/api.rb', line 35

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

#httpObject



86
87
88
# File 'lib/janky/github/api.rb', line 86

def http
  @http ||= http!
end

#http!Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/janky/github/api.rb', line 90

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

#repo_get(nwo) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/janky/github/api.rb', line 43

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

  http.request(request)
end

#trigger(hook_url) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/janky/github/api.rb', line 27

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