Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github_changelog_generator
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project=spage
user=nulty
exclude-labels=duplicate,question,invalid,wontfix,weekly-digest
3 changes: 3 additions & 0 deletions lib/spage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ class Error < StandardError; end
autoload :Api, 'spage/api'
autoload :Page, 'spage/resources/page'
autoload :Incident, 'spage/resources/incident'
autoload :IncidentUpdate, 'spage/resources/incident_update'
autoload :Component, 'spage/resources/component'

module Api
autoload :Page, 'spage/api/page'
autoload :Incident, 'spage/api/incident'
autoload :IncidentUpdate, 'spage/api/incident_update'
autoload :Component, 'spage/api/component'
end

module Serializers
autoload :Page, 'spage/serializers/page'
autoload :Incident, 'spage/serializers/incident'
autoload :IncidentUpdate, 'spage/serializers/incident_update'
autoload :Component, 'spage/serializers/component'
end

Expand Down
40 changes: 40 additions & 0 deletions lib/spage/api/incident_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Spage
# Api Module
#
module Api
# Incident resources in the statuspage.io API
#
class IncidentUpdate
include Api

def update(incident_update, id, page_id:)
json = Spage::Serializers::IncidentUpdate.new(incident_update,
update: true).to_json

response = client.patch(
"pages/#{page_id}/incidents/#{incident_update.incident_id}/incident_updates", id, json
)

handle_response(response) do
Spage::IncidentUpdate.new(response.body)
end
end
end

private

def handle_response(response)
case response
when Net::HTTPSuccess
yield
when Net::HTTPUnauthorized
raise(Error, 'Unauthorized: wrong API Key')
else
# Net::HTTPBadRequest, Net::HTTPUnprocessableEntity, Net::HTTPForbidden
raise(Error, response.body)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/spage/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def put(resource, id, body)
make_request(Net::HTTP::Put, resource, id, body)
end

def patch(resource, id, body)
make_request(Net::HTTP::Patch, resource, id, body)
end

def post(resource, body)
make_request(Net::HTTP::Post, resource, nil, body)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/spage/resources/incident.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module Spage
# Page resource in statuspage.io
#
class Incident
# rubocop: disable Metrics/MethodLength, Metrics/AbcSize, Layout/LineLength
# rubocop: disable Metrics/MethodLength, Metrics/AbcSize
def initialize(attrs)
@id = attrs['id']
@components = attrs['components'] # Array of Component
@impact = attrs['impact']
@impact_override = attrs['impact_override']
@incident_updates = attrs['incident_updates'] # Array of IncidentUpdate
@incident_updates = attrs['incident_updates']&.map { |iu| IncidentUpdate.new(iu) }
@metadata = attrs['metadata'] # Hash of values
@name = attrs['name']
@page_id = attrs['page_id']
Expand Down Expand Up @@ -49,12 +49,12 @@ def initialize(attrs)
@component_ids = attrs['component_ids']
@scheduled_auto_transition = attrs['scheduled_auto_transition']
end
# rubocop: enable Metrics/MethodLength, Metrics/AbcSize, Layout/LineLength
# rubocop: enable Metrics/MethodLength, Metrics/AbcSize

# rubocop: disable Layout/LineLength
attr_reader :id, :created_at, :impact, :incident_updates, :monitoring_at, :page_id, :postmortem_body, :postmortem_body_last_updated_at, :postmortem_ignored, :postmortem_notified_subscribers, :postmortem_notified_twitter, :postmortem_published_at, :resolved_at, :shortlink, :updated_at

attr_accessor :name, :status, :impact_override, :scheduled_for, :scheduled_until, :scheduled_remind_prior, :scheduled_auto_in_progress, :scheduled_auto_completed, :metadata, :deliver_notifications, :auto_transition_deliver_notifications_at_end, :auto_transition_deliver_notifications_at_start, :auto_transition_to_maintenance_state, :auto_transition_to_operational_state, :auto_tweet_at_beginning, :auto_tweet_on_completion, :auto_tweet_on_creation, :auto_tweet_one_hour_before, :backfill_date, :backfilled, :body, :components, :component_ids, :scheduled_auto_transition
attr_accessor :name, :status, :impact_override, :scheduled_for, :scheduled_until, :scheduled_remind_prior, :scheduled_reminded_at, :scheduled_auto_in_progress, :scheduled_auto_completed, :metadata, :deliver_notifications, :auto_transition_deliver_notifications_at_end, :auto_transition_deliver_notifications_at_start, :auto_transition_to_maintenance_state, :auto_transition_to_operational_state, :auto_tweet_at_beginning, :auto_tweet_on_completion, :auto_tweet_on_creation, :auto_tweet_one_hour_before, :backfill_date, :backfilled, :body, :components, :component_ids, :scheduled_auto_transition
# rubocop: enable Layout/LineLength

private
Expand Down
57 changes: 57 additions & 0 deletions lib/spage/resources/incident_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Spage
# Page resource in statuspage.io
#
class IncidentUpdate
# rubocop: disable Metrics/MethodLength, Metrics/AbcSize
def initialize(attrs)
@id = attrs['id']
@incident_id = attrs['incident_id']
@affected_components = attrs['affected_components']
@body = attrs['body']
@custom_tweet = bool(attrs['custom_tweet'])
@deliver_notifications = attrs['deliver_notifications']
@display_at = date_parse(attrs['display_at'])
@status = attrs['status']
@tweet_id = attrs['tweet_id']
@twitter_updated_at = date_parse(attrs['twitter_updated_at'])
@wants_twitter_update = bool(attrs['wants_twitter_update'])
@created_at = date_parse(attrs['created_at'])
@updated_at = date_parse(attrs['updated_at'])

# vars only used to update/create incident_update
end
# rubocop: enable Metrics/MethodLength, Metrics/AbcSize

attr_accessor :incident_id,
:body,
:deliver_notifications,
:display_at,
:wants_twitter_update
attr_reader :id,
:affected_components,
:custom_tweet,
:status,
:tweet_id,
:twitter_updated_at,
:updated_at,
:created_at

private

def date_parse(str)
return str if str.nil?
return str if str.is_a?(DateTime)

DateTime.parse(str)
end

def bool(val)
case val
when '1' then true
when '0' then false
end
end
end
end
53 changes: 53 additions & 0 deletions lib/spage/serializers/incident_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module Spage
module Serializers
class IncidentUpdate
def initialize(incident_update, update: false)
@incident_update = incident_update
@ivars = ivars(update)
end

def to_json(obj = nil)
as_json.to_json(obj)
end

def as_json
to_hash
end

def to_hash
{
'incident_update' => Hash[
@ivars.map do |name|
[name[1..-1], @incident_update.instance_variable_get(name)]
end
]
}
end

private

def ivars(update)
if update
@incident_update.instance_variables.select do |name|
processed_name = name[1..-1]
update_attrs.include?(processed_name) &&
!@incident_update.send(processed_name).nil?
end
else
@incident_update.instance_variables
end
end

def update_attrs
%w[
body
deliver_notifications
display_at
wants_twitter_update
]
end
end
end
end
35 changes: 35 additions & 0 deletions spec/api/incident_update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Spage::Api::IncidentUpdate do
describe '#update' do
context '200 incident_update is updated' do
it 'updates status from investigating to identified' do
incident_update = Spage::IncidentUpdate.new(
'id' => 'tkd7x7m9t9wc',
'incident_id' => 'yqr926fhwmh3',
'body' => 'Empathize with those',
'twitter_updated_at' => nil,
'delivery_notifications' => true,
'wants_twitter_update' => false,
'affected_components' => [],
'display_at' => '2022-06-17T19:30:00+00:00',
'updated_at' => '2022-06-17T19:30:00+00:00',
'created_at' => '2022-06-17T19:30:00+00:00',
'page_id' => 'fmd7kj5n71y9'
)

VCR.use_cassette('incident_updates/update_200_incident_update_is_updated') do
updated = Spage::Api::IncidentUpdate.new
.update(incident_update,
'tkd7x7m9t9wc',
page_id: 'fmd7kj5n71y9')

expect(updated.id).to eq('tkd7x7m9t9wc')
expect(updated.body).to eq('Empathize with those')
end
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.