app.rb 1008 Bytes
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1
2
3
4
5
6
7
8
9
10
require 'json'

require './lib/marketo_client'
require './lib/user'

class MarketoTools < Sinatra::Base
  use Rack::Auth::Basic do |username, password|
    username == ENV['AUTH_USERNAME'] && password == ENV['AUTH_PASSWORD']
  end

11
12
13
14
  get '/' do
    'success'
  end

Robert Speicher's avatar
Robert Speicher committed
15
16
17
18
19
  # Receives GitLab System Hooks
  #
  # Specifically, we're interested in the `user_create` event.
  #
  # See http://gitlab.com/help/system_hooks/system_hooks
20
  post '/system_hook' do
Robert Speicher's avatar
Robert Speicher committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    begin
      params = JSON.parse(request.body.read)
    rescue JSON::ParserError
      params = {}
    end

    if params['event_name'] == 'user_create'
      user = User.new(params)

      # TODO (rspeicher): Do we need to do anything with this besides
      # create/update a lead?
      client.createupdate_leads([user.to_param], lookup_field: :email)
    end

    # https://dev.gitlab.org/gitlab/mailchimp-tools/commit/a93a0b97a906008e97c0fe6eccdf09ce32f944ea
    'success'
  end

  private

  def client
    @client ||= MarketoClient.new
  end
end