Sha256: 9f0af0b2260cf7680e2a5bc8f523a2735ac89c0b4f72ab16901c6626ae2ee469

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'rest-client'
require 'multi_json'

module CapistranoPayload
  class DeliveryError < StandardError ; end
  
  class Payload
    attr_reader :action
    attr_reader :data
    attr_reader :params
    
    # Initialize a new Payload object
    #
    #   action - Deployment action (deploy, rollback)
    #   data   - Deployment parameters
    #   params - Extra parameters to payload (api_key, etc.), default: {}
    #            Could not contain 'capistrano' key. Will be removed if present.
    # 
    def initialize(action, data, params={})
      @action = action
      @data   = data.merge(:action => action)
      @params = params
      
      # Check if we have 'capistrano' keys (string or symbolic)
      unless @params.empty?
        @params.delete(:capistrano)
        @params.delete('capistrano')
      end
    end
    
    # Performs payload delivery
    #
    #   url - Target url
    #
    def deliver(url)
      payload = MultiJson.encode({:capistrano => @data}.merge(@params))
      begin
        RestClient.post(url, payload, :content_type => :json)
      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.1.0 lib/capistrano-payload/payload.rb