Module: API::Helpers::WebHooksHelpers

Extended by:
Grape::API::Helpers
Defined in:
lib/api/helpers/web_hooks_helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_hook_paramsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/api/helpers/web_hooks_helpers.rb', line 34

def create_hook_params
  hook_params = declared_params(include_missing: false)
  url_variables = hook_params.delete(:url_variables)

  if url_variables.present?
    hook_params[:url_variables] = url_variables.to_h { [_1[:key], _1[:value]] }
  end

  custom_headers = hook_params.delete(:custom_headers)

  if custom_headers.present?
    hook_params[:custom_headers] = custom_headers.to_h { [_1[:key], _1[:value]] }
  end

  hook_params
end

#find_hookObject



30
31
32
# File 'lib/api/helpers/web_hooks_helpers.rb', line 30

def find_hook
  hook_scope.find(params.delete(:hook_id))
end

#save_hook(hook, entity) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/api/helpers/web_hooks_helpers.rb', line 75

def save_hook(hook, entity)
  if hook.save
    present hook, with: entity
  else
    error!("Invalid url given", 422) if hook.errors[:url].present?
    error!("Invalid branch filter given", 422) if hook.errors[:push_events_branch_filter].present?

    render_validation_error!(hook, 422)
  end
end

#update_hook(entity:) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/api/helpers/web_hooks_helpers.rb', line 51

def update_hook(entity:)
  hook = find_hook
  update_params = update_hook_params(hook)

  hook.assign_attributes(update_params)

  save_hook(hook, entity)
end

#update_hook_params(hook) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/api/helpers/web_hooks_helpers.rb', line 60

def update_hook_params(hook)
  update_params = declared_params(include_missing: false)
  url_variables = update_params.delete(:url_variables) || []
  url_variables = url_variables.to_h { [_1[:key], _1[:value]] }
  update_params[:url_variables] = hook.url_variables.merge(url_variables) if url_variables.present?

  custom_headers = update_params.delete(:custom_headers) || []
  custom_headers = custom_headers.to_h { [_1[:key], _1[:value]] }
  update_params[:custom_headers] = hook.custom_headers.merge(custom_headers) if custom_headers.present?

  error!('No parameters provided', :bad_request) if update_params.empty?

  update_params
end