Class: CC::Service

Inherits:
Object
  • Object
show all
Includes:
HTTP, Helper
Defined in:
lib/cc/service.rb,
lib/cc/service/safe_webhook.rb,
lib/cc/service/response_check.rb,
lib/cc/presenters/pull_requests_presenter.rb

Defined Under Namespace

Modules: CoverageHelper, HTTP, Helper, IssueHelper, QualityHelper, VulnerabilityHelper Classes: Asana, Campfire, Config, Flowdock, Formatter, GitHubIssues, GitHubPullRequests, GitlabMergeRequests, HTTPError, HipChat, Invocation, Jira, Lighthouse, PivotalTracker, PullRequestsPresenter, ResponseCheck, SafeWebhook, Slack, StashPullRequests

Constant Summary collapse

Error =
Class.new(StandardError)
ConfigurationError =
Class.new(Error)
ALL_EVENTS =
%w[
  coverage
  issue
  pull_request
  pull_request_coverage
  pull_request_diff_coverage
  pull_request_total_coverage
  quality
  snapshot
  test
  unit
  vulnerability
].freeze
ABSTRACT_SLUGS =
%w[
  pullrequests
].freeze

Constants included from Helper

Helper::GREEN_HEX, Helper::RED_HEX

Constants included from HTTP

HTTP::REDIRECT_CODES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#changed, #color, #compare_url, #details_url, #emoji, #hex_color, #improved?, #repo_name

Methods included from HTTP

#ca_file, #formatted_post_response, #http, #http_method, #raw_get, #raw_post, #service_get, #service_post, #service_post_with_redirects

Constructor Details

#initialize(config, payload) ⇒ Service

Returns a new instance of Service.



91
92
93
94
95
96
97
98
# File 'lib/cc/service.rb', line 91

def initialize(config, payload)
  @payload = payload.stringify_keys
  @config = create_config(config)
  @event = @payload["name"].to_s

  load_helper
  validate_event
end

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



70
71
72
# File 'lib/cc/service.rb', line 70

def description
  @description
end

.issue_trackerObject

Returns the value of attribute issue_tracker.



71
72
73
# File 'lib/cc/service.rb', line 71

def issue_tracker
  @issue_tracker
end

.titleObject



74
75
76
77
78
79
80
# File 'lib/cc/service.rb', line 74

def self.title
  @title ||= begin
    hook = name.dup
    hook.sub!(/.*:/, "")
    hook
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/cc/service.rb', line 32

def config
  @config
end

#eventObject (readonly)

Returns the value of attribute event.



32
33
34
# File 'lib/cc/service.rb', line 32

def event
  @event
end

#payloadObject (readonly)

Returns the value of attribute payload.



32
33
34
# File 'lib/cc/service.rb', line 32

def payload
  @payload
end

Class Method Details

.by_slug(slug) ⇒ Object



64
65
66
# File 'lib/cc/service.rb', line 64

def self.by_slug(slug)
  services.detect { |s| s.slug == slug }
end

.inherited(svc) ⇒ Object



57
58
59
60
61
62
# File 'lib/cc/service.rb', line 57

def self.inherited(svc)
  unless ABSTRACT_SLUGS.include?(svc.slug)
    Service.services << svc
  end
  super
end

.load_servicesObject



21
22
23
24
# 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

.servicesObject

Tracks the defined services.



53
54
55
# File 'lib/cc/service.rb', line 53

def self.services
  @services ||= []
end

.slugObject



82
83
84
85
86
87
88
89
# File 'lib/cc/service.rb', line 82

def self.slug
  @slug ||= begin
    hook = name.dup
    hook.downcase!
    hook.sub!(/.*:/, "")
    hook
  end
end

Instance Method Details

#receiveObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cc/service.rb', line 100

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