diff --git a/.travis.yml b/.travis.yml index 618ed7b..6b00267 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: ruby rvm: - - 1.9.3 - 2.0.0 - 2.1.0 - 2.1.1 diff --git a/README.md b/README.md index 30e6f52..50a14bc 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ [CloudFlare](http://www.cloudflare.com/) is a service that protects and speeds up websites. Capistrano Cloudflare provides [Capistrano](https://github.com/capistrano/capistrano/wiki/Documentation-v2.x) tasks to update your CloudFlare settings. -Capistrano CloudFlare Version 1.0 and above supports the new Capistrano v3 API. For compatbility with Capistrano v2, please use version `0.0.2`. +Capistrano CloudFlare Version 2.0 and above supports the new Capistrano v3 API. For compatbility with Capistrano v2, please use version `0.0.2`. + +This fork of the library supports CloudFlare's version 4 API Currently only cache purging is supported. @@ -27,12 +29,12 @@ When using Capistrano, add: require 'capistrano/cloudflare' set :cloudflare_options, { - :domain => 'example.com', + :zone => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', :email => 'me@example.com', :api_key => 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' } -to config/deploy.rb. These options can be found in your [CloudFlare account seetings](https://www.cloudflare.com/my-account). +to config/deploy.rb. Your api key can be found in your [CloudFlare account seetings](https://www.cloudflare.com/my-account). Your Zone can be determined by called the CloudFlare API [List Zones Endpoint](https://api.cloudflare.com/#zone-list-zones) The following Capistrano tasks should now be available: diff --git a/capistrano-cloudflare.gemspec b/capistrano-cloudflare.gemspec index f3822a5..65c1061 100644 --- a/capistrano-cloudflare.gemspec +++ b/capistrano-cloudflare.gemspec @@ -2,8 +2,8 @@ require File.expand_path('../lib/capistrano/cloudflare/version', __FILE__) Gem::Specification.new do |gem| - gem.authors = ["Nathan L Smith"] - gem.email = ["nlloyds@gmail.com"] + gem.authors = ["Nathan L Smith", "Tristan D Havelick"] + gem.email = ["nlloyds@gmail.com", "tristan@havelick.com"] gem.description = %q{Capistrano extensions for CloudFlare} gem.summary = %q{Lets you make CloudFlare API calls when deploying with Capistrano.} gem.homepage = "https://github.com/cramerdev/capistrano-cloudflare" @@ -14,9 +14,11 @@ Gem::Specification.new do |gem| gem.name = "capistrano-cloudflare" gem.require_paths = ["lib"] gem.version = Capistrano::CloudFlare::VERSION + gem.required_ruby_version = '>= 2.0.0' gem.add_dependency 'capistrano', '>= 2.0' gem.add_dependency 'json' + gem.add_dependency 'httparty' gem.add_development_dependency 'rake' gem.add_development_dependency 'rspec' diff --git a/lib/capistrano/cloudflare.rb b/lib/capistrano/cloudflare.rb index d2434e8..cc0769a 100644 --- a/lib/capistrano/cloudflare.rb +++ b/lib/capistrano/cloudflare.rb @@ -1,25 +1,29 @@ require 'capistrano' require 'capistrano/cloudflare/version' require 'json' -require 'net/http' +require 'httparty' require 'rake' 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] + zone = options[:zone] + params = { + :purge_everything => true, + } + headers = { + 'Content-Type' => 'application/json', + 'X-Auth-Email' => options[:email], + 'X-Auth-Key' => options[:api_key] + } - }) - response = JSON.parse(http.request(request).body) + resp = HTTParty.delete( + "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache", + :body => params.to_json, + :headers => headers + ) + + JSON.parse(resp.body) end end end diff --git a/lib/capistrano/cloudflare/version.rb b/lib/capistrano/cloudflare/version.rb index 320e021..9c42a6c 100644 --- a/lib/capistrano/cloudflare/version.rb +++ b/lib/capistrano/cloudflare/version.rb @@ -1,5 +1,5 @@ module Capistrano module CloudFlare - VERSION = '1.0.0' + VERSION = '2.0' end end diff --git a/lib/capistrano/tasks/cloudflare.rake b/lib/capistrano/tasks/cloudflare.rake index 8df7628..42a48c9 100644 --- a/lib/capistrano/tasks/cloudflare.rake +++ b/lib/capistrano/tasks/cloudflare.rake @@ -2,13 +2,13 @@ namespace :cloudflare do namespace :cache do desc "Purge the CloudFlare cache" task :purge do - on roles(:all) do + run_locally do raise 'Missing CloudFlare configuration.' unless fetch(:cloudflare_options).respond_to?(:[]) response = Capistrano::CloudFlare.send_request(fetch(:cloudflare_options)) - if response['result'] == 'success' - info "Purged CloudFlare cache for #{fetch(:cloudflare_options)[:domain]}" + if response['success'] + info "Purged CloudFlare cache for #{fetch(:cloudflare_options)[:zone]}" else - error "CloudFlare cache purge failed. Reason: #{response['msg'] || 'unknown.'}" + error "CloudFlare cache purge failed. Reason: #{response['errors'].first['message'] || 'unknown.'}" end end end diff --git a/spec/capistrano/cloudflare_spec.rb b/spec/capistrano/cloudflare_spec.rb index 442c152..0273a3f 100644 --- a/spec/capistrano/cloudflare_spec.rb +++ b/spec/capistrano/cloudflare_spec.rb @@ -4,19 +4,19 @@ it { should be_a Module } describe '.send_request' do - it 'should POST to the cloudflare API and return the response body as a hash' do + it 'should call out to the cloudflare API and return the response body as a hash' do options = { - domain: 'example.com', + zone: 'Z', email: 'me@example.com', api_key: 'F' } - body = { 'result' => 'success' } - stub_request(:post, 'https://www.cloudflare.com/api_json.html').to_return( + body = { 'success' => true } + stub_request(:delete, "https://api.cloudflare.com/client/v4/zones/Z/purge_cache").to_return( :status => 200, :body => body.to_json ) - Capistrano::CloudFlare.send_request(options).should eq(body) + Capistrano::CloudFlare.send_request(options)['success'].should eq(true) end end end