Sha256: 5d4ff6fdaa7d88fbfde22107bce21389ba385ca227706d047ae972291b1cc7d5

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module CapistranoPayload
  class ConfigurationError < StandardError ; end
  class DeliveryError      < StandardError ; end
  
  class Payload
    include CapistranoPayload::Format
    include CapistranoPayload::Request
    
    # Allowed actions
    ACTIONS = ['deploy', 'rollback']
  
    attr_reader :action
    attr_reader :message
    attr_reader :data
    attr_reader :format
    attr_reader :params
    
    # Initialize a new Payload object
    #
    # action  - Deployment action (deploy, rollback)
    # message - Deployment message (optional)
    # data    - Payload data
    # format  - Payload format (:json, :form). Default: json
    # params  - Extra parameters to payload (api_key, etc.), default: {}
    #           Could not contain 'capistrano' key. Will be removed if present.
    #           Should be a hash
    # 
    def initialize(action, message, data, format=:json, params={})
      @action  = action
      @message = message.strip
      @data    = data.merge(:action => @action, :message => @message)
      @format  = format.to_sym
      @params  = params.kind_of?(Hash) ? params : {}
      
      unless ACTIONS.include?(@action)
        raise ConfigurationError, "Invalid payload action: #{action}."
      end
      
      # Check if we have an invalid format
      unless FORMATS.include?(@format)
        raise ConfigurationError, "Invalid payload format: #{format}."
      end
      
      # Check if we have 'payload' keys (string or symbolic)
      unless @params.empty?
        @params.delete(:payload)
        @params.delete('payload')
      end
    end
    
    # Performs payload delivery
    #
    #   url - Target url
    #
    def deliver(url)
      payload = self.send(FORMAT_METHODS[@format])
      begin
        request(:post, url, {:payload => payload}.merge(@params), format)
      rescue Exception => ex
        raise DeliveryError, ex.message
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capistrano-payload-0.3.0 lib/capistrano-payload/payload.rb