Class: Ipecache::Plugins::Fastly

Inherits:
Plugin
  • Object
show all
Defined in:
lib/ipecache/plugins/fastly.rb

Instance Method Summary collapse

Methods inherited from Plugin

#continue_on_error, #enabled?, hook, hooks, #initialize, #log_file, name, #name, #plugin_puts, #plugin_puts_error, #quiet_mode, #urls

Constructor Details

This class inherits a constructor from Ipecache::Plugins::Plugin

Instance Method Details

#performObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ipecache/plugins/fastly.rb', line 10

def perform
  safe_require 'uri'

  api_key = config.api_key

  if api_key.nil?
    plugin_puts("Fastly API key not specified, Exiting...")
    exit 1
  end

  plugin_puts "Beginning URL Purge from Fastly..."

  urls.each do |u|
    url = u.chomp
    plugin_puts ("Purging #{url}")

    uri = URI.parse("https://api.fastly.com/purge/#{url}")

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    http.verify_mode = OpenSSL::SSL::VERIFY_PEER

    request = Net::HTTP::Post.new(uri.request_uri)
    request.add_field("Accept", "application/json")
    request.add_field("User-Agent", "Ipecache")
    request.add_field("Fastly-Key", api_key)

    response = http.request(request)

    if response.code.to_i != 200
      plugin_puts_error(url,"Response Code: #{response.code}")
      plugin_puts_error(url,response.body)
      exit 1 unless continue_on_error
    else
      plugin_puts "Purge successful!"
    end
  end
end