Permalink
Browse files

Little refactoring. Access token for posting data added.

  • Loading branch information...
Štefan Ľupták
Štefan Ľupták committed May 23, 2012
1 parent f2e1172 commit ce575627efbd776edbea36a2309a20580fff0a70
View
@@ -1 +1,3 @@
.rvmrc
+config/gitlab_hook.yml
+config/twitter.yml
View
@@ -1,4 +1,2 @@
-require './gitlab_tweet'
+require './gitlab_hook'
run Sinatra::Application
-
-# 192.168.2.76:9292
@@ -0,0 +1,2 @@
+hostname: 'http://<YOUR_APP_NAME>.heroku.com'
+access_token: '<YOUR_SECRET_PASSPHRASE>'
@@ -0,0 +1,4 @@
+consumer_key: # YOUR CONSUMER KEY
+consumer_secret: # YOUR CONSUMER SECRET
+oauth_token: # YOUR OAUTH TOKEN
+oauth_token_secret: # YOUR OAUTH TOKEN SECRET
View
@@ -0,0 +1,35 @@
+require 'rubygems'
+require 'bundler/setup'
+require_relative 'lib/gitlab_hook'
+
+include GitLabHook
+
+Bundler.require
+
+configure do
+ TwitterClient.config(path: 'config/twitter.yml')
+ CONFIG = YAML.load_file('config/gitlab_tweet.yml')
+ ShorterURL.hostname = CONFIG['hostname']
+end
+
+get '/:token' do
+ token = params[:token]
+ if url = ShorterURL.get(token)
+ redirect url, 302
+ else
+ halt 404, "URL with token #{token} not found."
+ end
+end
+
+post '*' do
+ access_token = params[:access_token]
+ if access_token != CONFIG['access_token']
+ halt 403, "Please provide correct access_token."
+ else
+ messages = GitLabHook::HookParser.new(json: request.body.read, shortener: ShorterURL).messages
+ messages.each do |tweet|
+ TwitterClient.update(tweet)
+ end
+ ""
+ end
+end
View
@@ -1,29 +0,0 @@
-require 'rubygems'
-require 'bundler/setup'
-require_relative 'lib/gitlab_tweet'
-
-include GitLabTweet
-
-Bundler.require
-
-configure do
- TwitterClient = GitLabTweet::TwitterClient.new(config_path: 'config/twitter.yml')
- GitLabTweet::ShorterURL.hostname = YAML.load_file('config/gitlab_tweet.yml')['hostname']
-end
-
-get '/:token' do
- token = params[:token]
- if url = ShorterURL.get(token)
- redirect url, 302
- else
- halt 404, "URL with token #{token} not found."
- end
-end
-
-post '*' do
- tweets = GitLabTweet::HookParser.new(json: request.body.read, shortener: GitLabTweet::ShorterURL).tweets
- tweets.each do |tweet|
- TwitterClient.update(tweet)
- end
- ""
-end
View
@@ -0,0 +1,4 @@
+require_relative 'hash_extensions'
+require_relative 'hook_parser'
+require_relative 'shorter_url'
+require_relative 'twitter_client'
View
@@ -1,3 +0,0 @@
-require_relative 'hook_parser'
-require_relative 'shorter_url'
-require_relative 'twitter_client'
View
@@ -0,0 +1,10 @@
+class Hash
+
+ def symbolize_keys
+ inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
+
+end
View
@@ -1,9 +1,9 @@
-module GitLabTweet
+module GitLabHook
class HookParser
- attr_reader :body, :tweets, :shortener
+ attr_reader :body, :messages, :shortener
def initialize(options)
- @tweets = []
+ @messages = []
@body = JSON.parse(options[:json])
@shortener = options[:shortener]
parse
@@ -21,10 +21,10 @@ def parse
gitlab_url = commit['url']
url = shortener.set(gitlab_url)
- tweet = "[#{repo_name}] #{url} #{user_name} - #{message}"
- tweet = tweet[0..136] + '...' if tweet.size > 140
+ msg = "[#{repo_name}] #{url} #{user_name} - #{message}"
+ msg = msg[0..136] + '...' if msg.size > 140
- @tweets << tweet
+ @messages << msg
end
end
View
@@ -1,6 +1,6 @@
require 'singleton'
-module GitLabTweet
+module GitLabHook
class ShorterURL
include Singleton
@@ -13,7 +13,7 @@ def self.get(token)
end
def self.set(url)
- # TODO check for duplicit token
+ # TODO check for duplicate token
new_token = token
store.set(new_token, url)
"#{@@hostname}/#{new_token}"
View
@@ -1,26 +1,17 @@
-module GitLabTweet
- class TwitterClient
-
- attr_reader :client
+require 'singleton'
- def initialize(options)
- config = YAML.load_file(options[:config_path])
- config = symbolize_keys(config)
- @client = Twitter::Client.new(config)
- end
+module GitLabHook
+ class TwitterClient
+ include Singleton
- def update(tweet)
- client.update(tweet)
+ def self.config(options)
+ config = YAML.load_file(options[:path])
+ config = config.symbolize_keys
+ @@client = Twitter::Client.new(config)
end
- private
-
- def symbolize_keys(hash)
- h = {}
- hash.each_pair do |key, value|
- h[(key.to_sym rescue key)] = hash[key]
- end
- h
+ def self.update(tweet)
+ @@client.update(tweet)
end
end

0 comments on commit ce57562

Please sign in to comment.