Module: Api::Hooks

Extended by:
ActiveSupport::Concern
Included in:
ChallengesController, NeedsController, PostsController, ProjectsController
Defined in:
app/controllers/concerns/api/hooks.rb

Instance Method Summary collapse

Instance Method Details

#create_external_hookObject



65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/concerns/api/hooks.rb', line 65

def create_external_hook
  @hook = @obj.externalhooks.create(validate_hook)
  @hook.save!
  if validate_hook[:hook_type] == 'Slack'
    @hook.slackhooks.create(validate_hook_params[:hook_params])
    render json: { data: 'Hook created' }, status: :created
  else
    render json: { data: 'Hook Type not recognized' }, status: :unprocessable_entity
  end
end

#delete_external_hookObject



104
105
106
107
108
109
110
111
# File 'app/controllers/concerns/api/hooks.rb', line 104

def delete_external_hook
  @hook = Externalhook.find(params[:hook_id])
  if @hook.delete
    render json: { data: 'Hook deleted' }, status: :ok
  else
    render json: { data: 'Something went wrong' }, status: :unprocessable_entity
  end
end

#get_external_hooksObject



76
77
78
# File 'app/controllers/concerns/api/hooks.rb', line 76

def get_external_hooks
  render json: @obj.externalhooks, each_serializer: Api::HookSerializer
end

#hook_new_member(hooks, user, obj) ⇒ Object

the params for this controller look like this: params

{ hook: { hook_type: "Slack", hook_params: { hook_url: "URL", channel: "channel", username: "username" } } }



25
26
27
28
29
30
31
# File 'app/controllers/concerns/api/hooks.rb', line 25

def hook_new_member(hooks, user, obj)
  hooks.map do |hook|
    next unless hook.trigger_member

    SlackHookWorker.perform_async(hook.slackhooks.first.id, 'join', 'User', user.id, obj.class.name, obj.id) if hook.hook_type == 'Slack'
  end
end

#hook_new_need(hooks, need) ⇒ Object



41
42
43
44
45
46
47
# File 'app/controllers/concerns/api/hooks.rb', line 41

def hook_new_need(hooks, need)
  hooks.map do |hook|
    next unless hook.trigger_need

    SlackHookWorker.perform_async(hook.slackhooks.first.id, 'new', 'Need', need.id) if hook.hook_type == 'Slack'
  end
end

#hook_new_post(hooks, post) ⇒ Object



33
34
35
36
37
38
39
# File 'app/controllers/concerns/api/hooks.rb', line 33

def hook_new_post(hooks, post)
  hooks.map do |hook|
    next unless hook.trigger_post

    SlackHookWorker.perform_async(hook.slackhooks.first.id, 'new', 'Post', post.id) if hook.hook_type == 'Slack'
  end
end

#hook_new_project(hooks) ⇒ Object



49
50
51
52
53
54
55
# File 'app/controllers/concerns/api/hooks.rb', line 49

def hook_new_project(hooks)
  hooks.map do |hook|
    next unless hook.trigger_project

    SlackHookWorker.perform_async(hook.slackhooks.first.id, message) if hook.hook_type == 'Slack'
  end
end

#hook_new_project_challenge(hooks, project) ⇒ Object



57
58
59
60
61
62
63
# File 'app/controllers/concerns/api/hooks.rb', line 57

def hook_new_project_challenge(hooks, project)
  hooks.map do |hook|
    next unless hook.trigger_project

    SlackHookWorker.perform_async(hook.slackhooks.first.id, 'attach', 'Project', project.id) if hook.hook_type == 'Slack'
  end
end

#update_external_hookObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/concerns/api/hooks.rb', line 80

def update_external_hook
  hook_params = validate_hook
  @hook = Externalhook.find(params[:hook_id])
  old_hook = @hook.hook_type
  render json: { data: 'Something went wrong' }, status: :unprocessable_entity unless @hook.update(hook_params)
  # IF the hook type has changed
  if hook_params[:hook_type] != old_hook
    # First delete the previous hook
    @hook.slackhook.first.delete if old_hook == 'Slack'
    # Then recreate the new hook
    @hook.slackhooks.create(validate_hook_params[:hook_params]) if hook_params[:hook_type] == 'Slack'
    render json: { data: 'Hook updated' }, status: :ok
  else
    # We just update the attributes
    if hook_params[:hook_type] == 'Slack'
      slackhook = @hook.slackhooks.first
      slackhook.update(validate_hook_params[:hook_params])
      render json: { data: 'Hook updated' }, status: :ok
    else
      render json: { data: 'Hook Type not recognized' }, status: :unprocessable_entity
    end
  end
end