class CC::Service

Constants

ABSTRACT_SLUGS
ALL_EVENTS
ConfigurationError
Error

Attributes

description[RW]
issue_tracker[RW]
title[W]
config[R]
event[R]
payload[R]

Public Class Methods

by_slug(slug) click to toggle source
# File lib/cc/service.rb, line 62
def self.by_slug(slug)
  services.detect { |s| s.slug == slug }
end
inherited(svc) click to toggle source
Calls superclass method
# File lib/cc/service.rb, line 55
def self.inherited(svc)
  unless ABSTRACT_SLUGS.include?(svc.slug)
    Service.services << svc
  end
  super
end
load_services() click to toggle source
# File lib/cc/service.rb, line 21
def self.load_services
  path = File.expand_path("../services/**/*.rb", __FILE__)
  Dir[path].sort.each { |lib| require(lib) }
end
new(config, payload) click to toggle source
# File lib/cc/service.rb, line 89
def initialize(config, payload)
  @payload = payload.stringify_keys
  @config = create_config(config)
  @event = @payload["name"].to_s

  load_helper
  validate_event
end
services() click to toggle source

Tracks the defined services.

# File lib/cc/service.rb, line 51
def self.services
  @services ||= []
end
slug() click to toggle source
# File lib/cc/service.rb, line 80
def self.slug
  @slug ||= begin
    hook = name.dup
    hook.downcase!
    hook.sub!(/.*:/, "")
    hook
  end
end
title() click to toggle source
# File lib/cc/service.rb, line 72
def self.title
  @title ||= begin
    hook = name.dup
    hook.sub!(/.*:/, "")
    hook
  end
end

Public Instance Methods

receive() click to toggle source
# File lib/cc/service.rb, line 98
def receive
  methods = [:receive_event, :"receive_#{event}"]

  methods.each do |method|
    if respond_to?(method)
      return public_send(method)
    end
  end

  { ok: false, ignored: true, message: "No service handler found" }
end

Private Instance Methods

config_class() click to toggle source
# File lib/cc/service.rb, line 135
def config_class
  if defined?("#{self.class.name}::Config")
    "#{self.class.name}::Config".constantize
  else
    Config
  end
end
create_config(config) click to toggle source
# File lib/cc/service.rb, line 127
def create_config(config)
  config_class.new(config).tap do |c|
    unless c.valid?
      raise ConfigurationError, "Invalid config: #{config.inspect}"
    end
  end
end
load_helper() click to toggle source
# File lib/cc/service.rb, line 112
def load_helper
  helper_name = "#{event.classify}Helper"

  if Service.const_defined?(helper_name)
    @helper = Service.const_get(helper_name)
    extend @helper
  end
end
validate_event() click to toggle source
# File lib/cc/service.rb, line 121
def validate_event
  unless ALL_EVENTS.include?(event)
    raise ArgumentError, "Invalid event: #{event}"
  end
end