Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
add a real services class
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jun 3, 2011
1 parent 4ff427a commit baa597f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
27 changes: 10 additions & 17 deletions lib/app.rb
@@ -1,12 +1,16 @@
class Service::App < Sinatra::Base
def self.service(name)
post "/#{name}/" do
def self.service(svc)
post "/#{svc.hook_name}/" do
begin
data = JSON.parse(params[:data])
payload = parse_payload(params[:payload])
Service::Timeout.timeout(20, Service::TimeoutError) { yield data, payload }
status 200
""
if svc.receive(:push, data, payload)
status 200
""
else
status 404
status "#{svc.hook_name} Service does not respond to 'push' events"
end
rescue Service::ConfigurationError => boom
status 400
boom.message
Expand Down Expand Up @@ -79,20 +83,9 @@ module GitHub
ServiceConfigurationError = Service::ConfigurationError

def service(name)
Service::App.service(name)
end

def shorten_url(url)
Service::Timeout.timeout(6, Service::TimeoutError) do
short = Net::HTTP.get("api.bit.ly", "/shorten?version=2.0.1&longUrl=#{url}&login=github&apiKey=R_261d14760f4938f0cda9bea984b212e4")
short = JSON.parse(short)
short["errorCode"].zero? ? short["results"][url]["shortUrl"] : url
end
rescue Service::TimeoutError
url
end
end

include GitHub

Dir["#{File.dirname(__FILE__)}/services/**/*.rb"].each { |service| load service }
Dir["#{File.dirname(__FILE__)}/../services/**/*.rb"].each { |service| load service }
39 changes: 39 additions & 0 deletions lib/service.rb
@@ -1,4 +1,43 @@
class Service
class << self
attr_reader :hook_name

def hook_name=(value)
@hook_name = value
Service::App.service(self)
end

def receive(event_type, data, payload)
svc = new(event_type)
event_method = "receive_#{event_type}"
if svc.respond_to?(event_method)
Service::Timeout.timeout(20, TimeoutError) do
svc.send(event_method, data, payload)
end

true
else
false
end
end
end

attr_reader :event_type

def initialize(event_type)
@event_type = event_type
end

def shorten_url(url)
Service::Timeout.timeout(6, Service::TimeoutError) do
short = Net::HTTP.get("api.bit.ly", "/shorten?version=2.0.1&longUrl=#{url}&login=github&apiKey=R_261d14760f4938f0cda9bea984b212e4")
short = JSON.parse(short)
short["errorCode"].zero? ? short["results"][url]["shortUrl"] : url
end
rescue Service::TimeoutError
url
end

# Raised when an unexpected error occurs during service hook execution.
class Error < StandardError
attr_reader :original_exception
Expand Down
34 changes: 19 additions & 15 deletions services/acunote.rb
@@ -1,20 +1,24 @@
service :acunote do |data, payload|
token = data['token']
path = "/source_control/github/#{token}"
class AcunoteService < Service
self.hook_name = :acunote

req = Net::HTTP::Post.new(path)
req.set_form_data('payload' => payload.to_json)
req["Content-Type"] = 'application/x-www-form-urlencoded'
def receive_push(data, payload)
token = data['token']
path = "/source_control/github/#{token}"

http = Net::HTTP.new("www.acunote.com", 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
begin
http.start do |connection|
connection.request(req)
req = Net::HTTP::Post.new(path)
req.set_form_data('payload' => payload.to_json)
req["Content-Type"] = 'application/x-www-form-urlencoded'

http = Net::HTTP.new("www.acunote.com", 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
begin
http.start do |connection|
connection.request(req)
end
rescue Net::HTTPBadResponse
raise GitHub::ServiceConfigurationError, "Invalid configuration"
end
rescue Net::HTTPBadResponse
raise GitHub::ServiceConfigurationError, "Invalid configuration"
nil
end
nil
end

0 comments on commit baa597f

Please sign in to comment.