Class: Middleman::Cli::FastlyCDN

Inherits:
BaseCDN
  • Object
show all
Defined in:
lib/middleman-cdn/cdns/fastly.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCDN

example_configuration, #say_status

Class Method Details

.example_configuration_elementsObject



12
13
14
15
16
17
# File 'lib/middleman-cdn/cdns/fastly.rb', line 12

def self.example_configuration_elements
  {
    api_key: ['"..."', "# default ENV['FASTLY_API_KEY']"],
    base_urls: [['http://www.example.com', 'https://www.example.com'], ""]
  }
end

.keyObject



8
9
10
# File 'lib/middleman-cdn/cdns/fastly.rb', line 8

def self.key
  "fastly"
end

Instance Method Details

#invalidate(options, files, all: false) ⇒ Object



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
49
50
51
52
# File 'lib/middleman-cdn/cdns/fastly.rb', line 19

def invalidate(options, files, all: false)
  options[:api_key] ||= ENV['FASTLY_API_KEY']

  [:api_key, :base_urls].each do |key|
    if options[key].blank?
      say_status(ANSI.red{ "Error: Configuration key fastly[:#{key}] is missing." })
      raise
    end
  end

  options[:base_urls] = [options[:base_urls]] if options[:base_urls].is_a?(String)
  if !options[:base_urls].is_a?(Array)
    say_status(ANSI.red{ "Error: Configuration key fastly[:base_urls] must be an array and contain at least one base url." })
    raise
  end

  fastly = ::Fastly.new({
    :api_key => options[:api_key]
  })

  options[:base_urls].each do |base_url|
    files.each do |file|
      begin
        url = "#{base_url}#{file}"
        say_status("Invalidating #{url}... ", newline: false)
        fastly.purge("#{base_url}#{file}")
      rescue => e
        say_status(ANSI.red{ ", " + "error: #{e.message}" }, header: false)
      else
        say_status(ANSI.green{ "" }, header: false)
      end
    end
  end
end