jnunemaker / github-twitter

[DEAD] This is out of date and is left purely for others to peruse. Not needed anymore due to github-services

This URL has Read+Write access

jnunemaker (author)
Fri Feb 15 20:53:22 -0800 2008
commit  eb40914e7c7f4cec9e6542eb2921c945e75be9eb
tree    a1c62addc436837282a7381840b37d469ed39dd9
parent  cce0040062f39435bb27a8ddcfb2f4e4951cf5c5
github-twitter / github-twitter.rb
100644 38 lines (31 sloc) 0.942 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'rubygems'
require 'json'
require 'twitter'
require 'sinatra'
require 'erb'
 
REPOS = YAML.load_file('config.yml')
 
class GithubTwitter
  
  def initialize(payload)
    payload = JSON.parse(payload)
    return unless payload.keys.include?("repository")
    @repo = payload["repository"]["name"]
    @template = ERB.new(REPOS[@repo]["template"] || "[<%= commit['repo'] %>] <%= commit['url'] %> by <%= commit['author']['name'] %> - <%= commit['message'] %>")
    @twitter = connect(@repo)
    payload["commits"].each { |c| process_commit(c.last) }
  end
  
  def connect(repo)
    credentials = REPOS[repo]
    return Twitter::Base.new(credentials['username'], credentials['password'])
  end
  
  def process_commit(commit)
    commit["repo"] = @repo
    proc = Proc.new do
      commit
    end
    @twitter.post(@template.result(proc)[0,140])
  end
  
end
 
post '/' do
  GithubTwitter.new(params[:payload])
  "OMGPONIES! IT WORKED"
end