Class: TrackerHookForwarder

Inherits:
Object
  • Object
show all
Defined in:
lib/tracker-hook-forwarder.rb,
lib/tracker-hook-forwarder/version.rb,
lib/tracker-hook-forwarder/forwarding.rb

Defined Under Namespace

Modules: RackIntegration Classes: Forwarding

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TrackerHookForwarder

Returns a new instance of TrackerHookForwarder.



42
43
44
45
46
# File 'lib/tracker-hook-forwarder.rb', line 42

def initialize(env)
  @env = env
  @request = Rack::Request.new(env)
  TrackerHookForwarder.logger.info "#{request.request_method} #{request.fullpath}"
end

Class Method Details

.add_forwarding(project, url) ⇒ Object



24
25
26
27
# File 'lib/tracker-hook-forwarder.rb', line 24

def add_forwarding(project, url)
  forwardings[project.to_sym] ||= []
  forwardings[project.to_sym] << Forwarding.new(url)
end

.call(env) ⇒ Object



8
9
10
# File 'lib/tracker-hook-forwarder.rb', line 8

def call(env)
  new(env).process
end

.forwardingsObject



20
21
22
# File 'lib/tracker-hook-forwarder.rb', line 20

def forwardings
  @forwardings ||= {}
end

.forwardings_for(project) ⇒ Object



29
30
31
32
# File 'lib/tracker-hook-forwarder.rb', line 29

def forwardings_for(project)
  return nil if project.nil? or project.length == 0
  forwardings[project.to_sym]
end

.loggerObject



12
13
14
15
16
17
18
# File 'lib/tracker-hook-forwarder.rb', line 12

def logger
  return @logger if @logger
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::INFO
  @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
  @logger
end

.reset_forwardings!Object



34
35
36
# File 'lib/tracker-hook-forwarder.rb', line 34

def reset_forwardings!
  @forwardings = nil
end

Instance Method Details

#activity_hook_triggered?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/tracker-hook-forwarder.rb', line 65

def activity_hook_triggered?
  request.post? and not forwardings.nil?
end

#forwardingsObject



85
86
87
# File 'lib/tracker-hook-forwarder.rb', line 85

def forwardings
  @forwardings ||= TrackerHookForwarder.forwardings_for(requested_project_name)
end

#post_bodyObject



69
70
71
72
73
74
75
76
# File 'lib/tracker-hook-forwarder.rb', line 69

def post_body
  return @post_body if @post_body
  @post_body = ""
  while data = request.body.read(1024)
    @post_body << data
  end
  @post_body
end

#processObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tracker-hook-forwarder.rb', line 48

def process
  if activity_hook_triggered?
    TrackerHookForwarder.logger.info "Activity Hook triggered for #{requested_project_name} with:\n#{post_body}"
    forwardings.each {|forwarding| forwarding.forward post_body }
    return [201, {"Content-Type" => 'application/xml'}, [post_body]]

  elsif request.get? and request.path == '/'
    return [200, {"Content-Type" => 'text/plain'}, ['Hello.']]
  else
    TrackerHookForwarder.logger.info "Could not find #{request.request_method} #{request.fullpath}"
    return [404, {"Content-Type" => 'text/plain'}, ['Resource not found']]
  end
rescue => err
  TrackerHookForwarder.logger.warn "#{request.request_method} #{request.fullpath} caused an exception: #{err}\n#{err.backtrace}"
  return [500, {"Content-Type" => 'text/plain'}, ['Something went wrong :(']]
end

#requested_project_nameObject



78
79
80
81
82
83
# File 'lib/tracker-hook-forwarder.rb', line 78

def requested_project_name
  path_parts = request.path.split('/').reject {|part| part.strip.length == 0 }
  if path_parts.count == 2 and path_parts.first == 'activity'
    path_parts[1]
  end
end