class Tapjoy::PagerDuty::Base

Public Class Methods

new() click to toggle source

Initializer services to import values from pg_connect.yaml to configure organization-specific values (currently, subdomain and api_token)

# File lib/tapjoy/pagerduty/base.rb, line 7
def initialize
  config_file = "#{ENV['PAGERDUTY_CONFIG_DIR'] ? ENV['PAGERDUTY_CONFIG_DIR'] + 'pgconnect.yml' : ENV['HOME'] + '/.pgutils/pgconnect.yaml'}"
  pg_conn = YAML.load_file(config_file) if File.readable?(config_file)

  @AUTH_HEADER = {
    subdomain:    ENV['PAGERDUTY_SUBDOMAIN'] || pg_conn[:subdomain],
    token_string: "Token token=#{ENV['PAGERDUTY_API_TOKEN'] || pg_conn[:api_token]}"
  }

  raise 'Missing subdomain value' if @AUTH_HEADER[:subdomain].nil?
  raise 'Missing API token' if @AUTH_HEADER[:token_string].nil?
end

Public Instance Methods

get_incidents(query_start:, query_end:) click to toggle source
# File lib/tapjoy/pagerduty/base.rb, line 85
def get_incidents(query_start:, query_end:)
  since_date = Date.today - query_start
  until_date = since_date + query_end
  endpoint = "#{return_pagerduty_url(:incidents)}?since=#{since_date}&until=#{until_date}"
  return get_object(endpoint)
end
get_schedule_id(schedule_name) click to toggle source

Given the name of a schedule return the schedule_id that pagerduty uses for lookups

# File lib/tapjoy/pagerduty/base.rb, line 28
def get_schedule_id(schedule_name)
  endpoint = return_pagerduty_url(:schedules)
  out_array = get_object(endpoint)['schedules'].select { |i| i['name'].eql?(schedule_name)}
  return Hash[*out_array]['id']
end
get_user_details(user_id) click to toggle source

Given a specific user, return all details about the user that we can parse through as needed

# File lib/tapjoy/pagerduty/base.rb, line 61
def get_user_details(user_id)
  endpoint = return_pagerduty_url(:users) + "/#{user_id}/on_call"
  return get_object(endpoint)
end
get_user_id(email) click to toggle source

Given an email address return the user_id that pagerduty uses for lookups

# File lib/tapjoy/pagerduty/base.rb, line 21
def get_user_id(email)
  endpoint = return_pagerduty_url(:users)
  out_array = get_object(endpoint)['users'].select { |i| i['email'].eql?(email) }
  return Hash[*out_array]['id']
end
get_users_on_call() click to toggle source

Return all users on call for all schedules, which we can parse through later

# File lib/tapjoy/pagerduty/base.rb, line 54
def get_users_on_call
  endpoint = return_pagerduty_url(:escalation_on_call)
  return get_object(endpoint)
end
post_trigger(service_key:, incident_key:, description:, client:, client_url:, details:) click to toggle source

Create a page to the first person on call for a given service key

# File lib/tapjoy/pagerduty/base.rb, line 67
def post_trigger(service_key:, incident_key:, description:, client:,
  client_url:, details:)

  # Ruby 2.x style kw-args is required here to make hash passing easier
  endpoint = return_pagerduty_url(:create_trigger)
  data = {
    service_key: service_key,
    incident_key: incident_key,
    event_type: 'trigger',
    description: description,
    client: client,
    client_url: client_url,
    details: details,
  }

  post_object(endpoint, data)
end
set_override(query_start:, query_end:, override_start:, override_end:, user_id:, schedule_id:) click to toggle source

The set_override method takes in several variables and returns the REST response upon (attempting) completion of an override action

# File lib/tapjoy/pagerduty/base.rb, line 36
def set_override(query_start:, query_end:, override_start:, override_end:,
  user_id:, schedule_id:)
  # Ruby 2.x style kw-args is required here to make hash passing easier

  endpoint = "#{return_pagerduty_url(:schedules)}/#{schedule_id}/overrides?since=#{query_start}&until=#{query_end}"

  data = {
    override: {
      user_id: user_id,
      start: override_start,
      end: override_end,
    }
  }

  post_object(endpoint, data)
end

Private Instance Methods

get_object(endpoint) click to toggle source

Helper method for all GETs

# File lib/tapjoy/pagerduty/base.rb, line 94
def get_object(endpoint)
  response = HTTParty.get(
    endpoint,
    headers: {
      'Content-Type' => 'application/json', 'Authorization' => @AUTH_HEADER[:token_string]
    }
  )
  return JSON.load(response.body)
end
post_object(endpoint, data) click to toggle source

Helper method for all PUTs

# File lib/tapjoy/pagerduty/base.rb, line 105
def post_object(endpoint, data)
  response = HTTParty.post(
    endpoint,
    body: data.to_json,
    headers: {
      'Content-Type' => 'application/json', 'Authorization' => @AUTH_HEADER[:token_string]
    }
  )

  return response.body
end
return_pagerduty_url(object_type) click to toggle source

Helper method for building PagerDuty URLs

# File lib/tapjoy/pagerduty/base.rb, line 118
def return_pagerduty_url(object_type)
  rest_api_url = "https://#{@AUTH_HEADER[:subdomain]}.pagerduty.com/api/v1"
  integration_api_url = 'https://events.pagerduty.com/generic/2010-04-15'
  case object_type
  when :users
    return rest_api_url + '/users'
  when :schedules
    return rest_api_url + '/schedules'
  when :escalation_on_call
    return rest_api_url + '/escalation_policies/on_call'
  when :incidents
    return rest_api_url + '/incidents'
  when :create_trigger
    return integration_api_url + '/create_event.json'
  else
    abort("Unknown object type: #{object_type}. Can't build URL.")
  end
end