Sha256: 2ac94dc9a8f1585d0b17bae46b66255ce2ec93d3c9af0164b220bc88fdbe7aaf

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'capistrano'
require 'capistrano/cloudflare/version'
require 'json'
require 'net/http'

module Capistrano
  module CloudFlare
    def self.send_request(options = {})
      uri = URI('https://www.cloudflare.com/api_json.html')
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      request = Net::HTTP::Post.new(uri.request_uri)
      request.set_form_data({
        :v     => 1,
        :a     => 'fpurge_ts',
        :z     => options[:domain],
        :tkn   => options[:api_key],
        :email => options[:email]

      })
      response = JSON.parse(http.request(request).body)
    end

    def self.load_into(configuration)
      configuration.set :capistrano_cloudflare, self
      configuration.load do
        namespace :cloudflare do
          namespace :cache do
            desc "Purge the CloudFlare cache"
            task :purge do
              raise unless fetch(:cloudflare_options).respond_to?(:[])
              response = capistrano_cloudflare.send_request(cloudflare_options)
              if response['result'] == 'success'
                logger.info("Purged CloudFlare cache for #{cloudflare_options[:domain]}")
              else
                logger.info("CloudFlare cache purge failed. Reason: #{response['msg'] || 'unknown.'}")
              end
            end
          end
        end

      end
    end
  end
end

if Capistrano::Configuration.instance
  Capistrano::CloudFlare.load_into(Capistrano::Configuration.instance)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capistrano-cloudflare-0.0.2 lib/capistrano/cloudflare.rb