• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ninoseki / mihari / 7248022748

18 Dec 2023 12:25PM UTC coverage: 93.908% (-0.6%) from 94.504%
7248022748

push

github

web-flow
Merge pull request #880 from ninoseki/renew-service-classess-commands

refactor: renew service classes & comamnds

73 of 100 new or added lines in 12 files covered. (73.0%)

1 existing line in 1 file now uncovered.

3376 of 3595 relevant lines covered (93.91%)

84.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.59
/lib/mihari/web/endpoints/alerts.rb
1
# frozen_string_literal: true
2

3
module Mihari
4✔
4
  module Web
4✔
5
    module Endpoints
4✔
6
      #
7
      # Alert API endpoint
8
      #
9
      class Alerts < Grape::API
4✔
10
        class AlertCreator < Service
4✔
11
          #
12
          # @param [Hash] params
13
          #
14
          # @return [Mihari::Models::Alert]
15
          #
16
          def call(params)
4✔
17
            proxy = Services::AlertProxy.new(**params.to_snake_keys)
4✔
18
            Services::AlertRunner.call proxy
4✔
19
          end
20
        end
21

22
        namespace :alerts do
4✔
23
          desc "List/search alerts", {
8✔
24
            is_array: true,
25
            success: Entities::AlertsWithPagination,
26
            summary: "List/search alerts"
27
          }
28
          params do
8✔
29
            optional :q, type: String, default: ""
8✔
30
            optional :page, type: Integer, default: 1
8✔
31
            optional :limit, type: Integer, default: 10
8✔
32
          end
33
          get "/" do
8✔
34
            value = Services::AlertSearcher.call(params.to_h)
4✔
35
            present(
4✔
36
              {
37
                alerts: value.results,
38
                total: value.total,
39
                current_page: value.filter[:page].to_i,
40
                page_size: value.filter[:limit].to_i
41
              },
42
              with: Entities::AlertsWithPagination
43
            )
44
          end
45

46
          desc "Get an alert", {
8✔
47
            success: Entities::Alert,
48
            failure: [{ code: 404, model: Entities::Message }],
49
            summary: "Get an alert"
50
          }
51
          params do
8✔
52
            requires :id, type: Integer
8✔
53
          end
54
          get "/:id" do
8✔
NEW
55
            id = params[:id].to_i
×
NEW
56
            result = Services::AlertGetter.result(id)
×
NEW
57
            return present(result.value!, with: Entities::Alert) if result.success?
×
58

NEW
59
            case result.failure
×
60
            when ActiveRecord::RecordNotFound
NEW
61
              error!({ message: "ID:#{id} is not found" }, 404)
×
62
            end
NEW
63
            raise result.failure
×
64
          end
65

66
          desc "Delete an alert", {
8✔
67
            success: { code: 204, model: Entities::Message },
68
            failure: [{ code: 404, model: Entities::Message }],
69
            summary: "Delete an alert"
70
          }
71
          params do
8✔
72
            requires :id, type: Integer
8✔
73
          end
74
          delete "/:id" do
8✔
75
            status 204
8✔
76

77
            id = params["id"].to_i
8✔
78
            result = Services::AlertDestroyer.result(id)
8✔
79
            return present({ message: "" }, with: Entities::Message) if result.success?
8✔
80

81
            case result.failure
4✔
82
            when ActiveRecord::RecordNotFound
83
              error!({ message: "ID:#{id} is not found" }, 404)
4✔
84
            end
85
            raise result.failure
×
86
          end
87

88
          desc "Create an alert", {
8✔
89
            success: { code: 201, model: Entities::Alert },
90
            failure: [{ code: 404, model: Entities::Message }],
91
            summary: "Create an alert"
92
          }
93
          params do
8✔
94
            requires :ruleId, type: String, documentation: { param_type: "body" }
8✔
95
            requires :artifacts, type: Array, documentation: { type: String, is_array: true, param_type: "body" }
8✔
96
          end
97
          post "/" do
8✔
98
            status 201
4✔
99

100
            result = AlertCreator.result(params)
4✔
101
            return present(result.value!, with: Entities::Alert) if result.success?
4✔
102

103
            case result.failure
×
104
            when ActiveRecord::RecordNotFound
105
              error!({ message: "Rule:#{params["ruleId"]} is not found" }, 404)
×
106
            end
107
            raise result.failure
×
108
          end
109
        end
110
      end
111
    end
112
  end
113
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc