github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/lib/trigger.rb (about)

     1  class Trigger
     2    include Model
     3  
     4    attr_reader :attributes, :links
     5  
     6    def self.doctype
     7      "io.cozy.triggers"
     8    end
     9  
    10    def initialize(opts = {})
    11      @attributes = opts
    12    end
    13  
    14    def save(inst)
    15      opts = {
    16        content_type: :json,
    17        accept: :json,
    18        authorization: "Bearer #{inst.token_for doctype}"
    19      }
    20      res = inst.client["/jobs/triggers"].post to_json, opts
    21      j = JSON.parse(res.body)
    22      @couch_id = j["data"]["id"]
    23      @links = j["data"]["links"]
    24    end
    25  
    26    def as_json
    27      { data: { attributes: @attributes } }
    28    end
    29  
    30    def self.from_json(j)
    31      Trigger.new j
    32    end
    33  
    34    module Webhook
    35      def self.create(inst, message, debounce = nil)
    36        attrs = {
    37          type: "@webhook",
    38          worker: "konnector",
    39          message: message
    40        }
    41        attrs[:debounce] = debounce if debounce
    42        t = Trigger.create inst, attrs
    43        t.links["webhook"]
    44      end
    45    end
    46  end