Class: PactBroker::Webhooks::Webhook

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/pact_broker/webhooks/webhook.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_domain(webhook, consumer, provider) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pact_broker/webhooks/webhook.rb', line 18

def self.from_domain webhook, consumer, provider
  is_json_request_body = !(String === webhook.request.body || webhook.request.body.nil?) # Can't rely on people to set content type
  new(
    uuid: webhook.uuid,
    method: webhook.request.method,
    url: webhook.request.url,
    username: webhook.request.username,
    password: not_plain_text_password(webhook.request.password),
    body: (is_json_request_body ? webhook.request.body.to_json : webhook.request.body),
    is_json_request_body: is_json_request_body
  ).tap do | db_webhook |
    db_webhook.consumer_id = consumer.id
    db_webhook.provider_id = provider.id
  end
end

.not_plain_text_password(password) ⇒ Object



34
35
36
# File 'lib/pact_broker/webhooks/webhook.rb', line 34

def self.not_plain_text_password password
  password.nil? ? nil : Base64.strict_encode64(password)
end

Instance Method Details

#before_destroyObject



14
15
16
# File 'lib/pact_broker/webhooks/webhook.rb', line 14

def before_destroy
  WebhookHeader.where(webhook_id: id).destroy
end

#parsed_bodyObject



62
63
64
65
66
67
68
# File 'lib/pact_broker/webhooks/webhook.rb', line 62

def parsed_body
  if body && is_json_request_body
     JSON.parse(body)
  else
    body
  end
end

#parsed_headersObject



56
57
58
59
60
# File 'lib/pact_broker/webhooks/webhook.rb', line 56

def parsed_headers
  WebhookHeader.where(webhook_id: id).all.each_with_object({}) do | header, hash |
    hash[header[:name]] = header[:value]
  end
end

#plain_text_passwordObject



52
53
54
# File 'lib/pact_broker/webhooks/webhook.rb', line 52

def plain_text_password
  password.nil? ? nil : Base64.strict_decode64(password)
end

#request_attributesObject



48
49
50
# File 'lib/pact_broker/webhooks/webhook.rb', line 48

def request_attributes
  values.merge(headers: parsed_headers, body: parsed_body, password: plain_text_password, uuid: uuid)
end

#to_domainObject



38
39
40
41
42
43
44
45
46
# File 'lib/pact_broker/webhooks/webhook.rb', line 38

def to_domain
  Domain::Webhook.new(
    uuid: uuid,
    consumer: consumer,
    provider: provider,
    request: Domain::WebhookRequest.new(request_attributes),
    created_at: created_at,
    updated_at: updated_at)
end