Class: Admin::WebHooksController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/web_hooks_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_eventsObject



100
101
102
103
104
# File 'app/controllers/admin/web_hooks_controller.rb', line 100

def bulk_events
  params.require(:ids)
  web_hook_events = @web_hook.web_hook_events.where(id: params[:ids])
  render_serialized(web_hook_events, AdminWebHookEventSerializer)
end

#createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/admin/web_hooks_controller.rb', line 43

def create
  web_hook = WebHook.new(web_hook_params)

  if web_hook.save
    StaffActionLogger.new(current_user).log_web_hook(
      web_hook,
      UserHistory.actions[:web_hook_create],
    )
    render_serialized(web_hook, AdminWebHookSerializer, root: "web_hook")
  else
    render_json_error web_hook.errors.full_messages
  end
end

#destroyObject



70
71
72
73
74
75
76
77
# File 'app/controllers/admin/web_hooks_controller.rb', line 70

def destroy
  @web_hook.destroy!
  StaffActionLogger.new(current_user).log_web_hook(
    @web_hook,
    UserHistory.actions[:web_hook_destroy],
  )
  render json: success_json
end

#editObject



39
40
41
# File 'app/controllers/admin/web_hooks_controller.rb', line 39

def edit
  render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/admin/web_hooks_controller.rb', line 6

def index
  limit = 50
  offset = params[:offset].to_i

  web_hooks =
    WebHook
      .limit(limit)
      .offset(offset)
      .includes(:web_hook_event_types)
      .includes(:categories)
      .includes(:groups)

  json = {
    web_hooks: serialize_data(web_hooks, AdminWebHookSerializer),
    extras: {
      event_types: WebHookEventType.active,
      default_event_types: WebHook.default_event_types,
      content_types: WebHook.content_types.map { |name, id| { id: id, name: name } },
      delivery_statuses:
        WebHook.last_delivery_statuses.map { |name, id| { id: id, name: name.to_s } },
    },
    total_rows_web_hooks: WebHook.count,
    load_more_web_hooks:
      admin_web_hooks_path(limit: limit, offset: offset + limit, format: :json),
  }

  render json: MultiJson.dump(json), status: 200
end

#list_eventsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/admin/web_hooks_controller.rb', line 79

def list_events
  limit = 50
  offset = params[:offset].to_i

  json = {
    web_hook_events:
      serialize_data(
        @web_hook.web_hook_events.limit(limit).offset(offset),
        AdminWebHookEventSerializer,
      ),
    total_rows_web_hook_events: @web_hook.web_hook_events.count,
    load_more_web_hook_events:
      web_hook_events_admin_api_index_path(limit: limit, offset: offset + limit, format: :json),
    extras: {
      web_hook_id: @web_hook.id,
    },
  }

  render json: MultiJson.dump(json), status: 200
end

#pingObject



119
120
121
122
123
124
125
126
127
# File 'app/controllers/admin/web_hooks_controller.rb', line 119

def ping
  Jobs.enqueue(
    :emit_web_hook_event,
    web_hook_id: @web_hook.id,
    event_type: "ping",
    event_name: "ping",
  )
  render json: success_json
end

#redeliver_eventObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/admin/web_hooks_controller.rb', line 106

def redeliver_event
  web_hook_event = WebHookEvent.find_by(id: params[:event_id])

  if web_hook_event
    web_hook = web_hook_event.web_hook
    emitter = WebHookEmitter.new(web_hook, web_hook_event)
    emitter.emit!(headers: MultiJson.load(web_hook_event.headers), body: web_hook_event.payload)
    render_serialized(web_hook_event, AdminWebHookEventSerializer, root: "web_hook_event")
  else
    render json: failed_json
  end
end

#showObject



35
36
37
# File 'app/controllers/admin/web_hooks_controller.rb', line 35

def show
  render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
end

#updateObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/admin/web_hooks_controller.rb', line 57

def update
  if @web_hook.update(web_hook_params)
    StaffActionLogger.new(current_user).log_web_hook(
      @web_hook,
      UserHistory.actions[:web_hook_update],
      changes: @web_hook.saved_changes,
    )
    render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
  else
    render_json_error @web_hook.errors.full_messages
  end
end