Class: PactBroker::Repositories::Webhook

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/pact_broker/repositories/webhook_repository.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_model(webhook, consumer, provider) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 63

def self.from_model 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



79
80
81
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 79

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

Instance Method Details

#before_destroyObject



59
60
61
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 59

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

#parsed_bodyObject



107
108
109
110
111
112
113
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 107

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

#parsed_headersObject



101
102
103
104
105
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 101

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



97
98
99
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 97

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

#request_attributesObject



93
94
95
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 93

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

#to_modelObject



83
84
85
86
87
88
89
90
91
# File 'lib/pact_broker/repositories/webhook_repository.rb', line 83

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