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



28
29
30
31
32
33
34
35
# File 'lib/pact_broker/webhooks/webhook.rb', line 28

def self.from_domain webhook, consumer, provider
  new(
    properties_hash_from_domain(webhook).merge(uuid: webhook.uuid)
  ).tap do | db_webhook |
    db_webhook.consumer_id = consumer.id if consumer
    db_webhook.provider_id = provider.id if provider
  end
end

.not_plain_text_password(password) ⇒ Object



37
38
39
# File 'lib/pact_broker/webhooks/webhook.rb', line 37

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

Instance Method Details

#before_destroyObject



20
21
22
# File 'lib/pact_broker/webhooks/webhook.rb', line 20

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

#is_for?(relationship) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/pact_broker/webhooks/webhook.rb', line 74

def is_for? relationship
  (consumer_id == relationship.consumer_id || !consumer_id) && (provider_id == relationship.provider_id || !provider_id)
end

#parsed_bodyObject



66
67
68
69
70
71
72
# File 'lib/pact_broker/webhooks/webhook.rb', line 66

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

#parsed_headersObject



60
61
62
63
64
# File 'lib/pact_broker/webhooks/webhook.rb', line 60

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



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

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

#request_attributesObject



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

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

#to_domainObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/pact_broker/webhooks/webhook.rb', line 41

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

#update_from_domain(webhook) ⇒ Object



24
25
26
# File 'lib/pact_broker/webhooks/webhook.rb', line 24

def update_from_domain webhook
  set(self.class.properties_hash_from_domain(webhook))
end