Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Shortener cez bit.ly

  • Loading branch information...
commit b8972707ad27480578905b5992d631ba26e7ceab 1 parent 512630f
Štefan Ľupták authored
View
2  Gemfile
@@ -4,4 +4,4 @@ gem 'thin'
gem 'twitter'
gem 'sinatra'
gem 'json'
-gem 'redis'
+gem 'bitly'
View
3  config/gitlab_hook.yml.erb
@@ -5,3 +5,6 @@ twitter:
consumer_secret: <%= ENV['TWITTER_CONSUMER_SECRET'] || '' %>
oauth_token: <%= ENV['TWITTER_OAUTH_TOKEN'] || '' %>
oauth_token_secret: <%= ENV['TWITTER_OAUTH_TOKEN_SECRET'] || '' %>
+bitly:
+ username: <%= ENV['BITLY_USERNAME'] || '' %>
+ api_key: <%= ENV['BITLY_API_KEY'] || '' %>
View
13 gitlab_hook.rb
@@ -9,16 +9,7 @@
configure do
CONFIG = GitLabHook::Config.load
TwitterClient = Twitter::Client.new(CONFIG[:twitter])
- 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
+ BitlyClient = Bitly.new(CONFIG[:bitly][:username], CONFIG[:bitly][:api_key])
end
post '*' do
@@ -26,7 +17,7 @@
if access_token != CONFIG[:access_token]
halt 403, "Please provide correct access_token."
else
- messages = HookParser.new(json: request.body.read, shortener: ShorterURL).messages
+ messages = HookParser.new(json: request.body.read, shortener: BitlyClient).messages
messages.each do |tweet|
TwitterClient.update(tweet)
end
View
4 lib/hook_parser.rb
@@ -20,8 +20,8 @@ def parse
message = commit['message']
gitlab_url = commit['url']
- url = shortener.set(gitlab_url)
- msg = "[#{repo_name}] #{url} #{user_name} - #{message}"
+ url = shortener.shorten(gitlab_url)
+ msg = "[#{repo_name}] #{url.jmp_url} #{user_name} - #{message}"
msg = msg[0..136] + '...' if msg.size > 140
@messages << msg
View
34 lib/shorter_url.rb
@@ -1,34 +0,0 @@
-require 'singleton'
-
-module GitLabHook
- class ShorterURL
- include Singleton
-
- def self.hostname=(url)
- @@hostname = url
- end
-
- def self.get(token)
- store.get(token)
- end
-
- def self.set(url)
- # TODO check for duplicate token
- new_token = token
- store.set(new_token, url)
- "#{@@hostname}/#{new_token}"
- end
-
- private
-
- def self.store
- uri ||= URI.parse(ENV["REDISTOGO_URL"])
- @@store ||= Redis.new(host: uri.host, port: uri.port, password: uri.password)
- end
-
- def self.token
- rand(36**8).to_s(36)
- end
-
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.