Class: PagerDuty::Models::Service

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/pagerduty/models/service.rb

Constant Summary collapse

ACTIVE =

5 potential PagerDuty API ‘service#status` values

See Also:

  • schema from https://api-reference.pagerduty.com/#!/Services/get_services
'active'
WARNING =
'warning'
CRITICAL =
'critical'
DISABLED =
'disabled'
MAINTENANCE =
'maintenance'
STATUSES =
[ACTIVE, WARNING, CRITICAL, MAINTENANCE, DISABLED].freeze

Class Method Summary collapse

Class Method Details

.build!(service) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/pagerduty/models/service.rb', line 51

def build!(service)
  external_service = Service.new(
    service: external_service_in(service),
    service_id: PagerDuty::Configuration.service_map[service['id']].to_s,
    status: service['status'],
    last_incident_timestamp: service['last_incident_timestamp']
  )

  validate! external_service
end

.eligible!(services) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pagerduty/models/service.rb', line 43

def eligible!(services)
  services.select do |service|
    name_present!(service)

    service['name'].start_with?(Settings.maintenance.service_query_prefix)
  end
end

.external_service_in(service) ⇒ Object



62
63
64
# File 'lib/pagerduty/models/service.rb', line 62

def external_service_in(service)
  service['name'].delete_prefix(Settings.maintenance.service_query_prefix).strip
end

.name_present!(service) ⇒ Object



66
67
68
# File 'lib/pagerduty/models/service.rb', line 66

def name_present!(service)
  raise Common::Exceptions::InvalidFieldValue.new('name', 'nil') if service['name'].blank?
end

.statuses_for(services) ⇒ Array<Service>

Maps over the raw PagerDuty service hashes returned from PagerDuty’s API GET /services call, and converts those into PagerDuty::Models::Service objects

Parameters:

  • services (Array<Hash>)

    An array of service hashes from PagerDuty’s REST API

Returns:

  • (Array<Service>)

    An array of PagerDuty::Models::Service objects

See Also:



38
39
40
# File 'lib/pagerduty/models/service.rb', line 38

def self.statuses_for(services)
  eligible!(services).map { |service| build!(service) }
end

.validate!(external_service) ⇒ Object



70
71
72
73
74
# File 'lib/pagerduty/models/service.rb', line 70

def validate!(external_service)
  raise Common::Exceptions::ValidationErrors, external_service unless external_service.valid?

  external_service
end