Class: Ipecache::Plugins::SwisstxtCdn

Inherits:
Plugin
  • Object
show all
Defined in:
lib/ipecache/plugins/swisstxt_cdn.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

#connectionObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ipecache/plugins/swisstxt_cdn.rb', line 41

def connection
  if config.api_key.nil?
    plugin_puts("SWISS TXT CDN API api_key not specified, Exiting...")
    exit 1
  end

  if config.api_secret.nil?
    plugin_puts("SWISS TXT CDN API api_secret not specified, Exiting...")
    exit 1
  end

  Faraday.new(url: config.url || 'https://cdn-api.swisstxt.ch') do |conn|
    conn.request :url_encoded
    conn.headers[:user_agent]     = 'Ipecache'
    conn.headers[:accept]         = 'application/json'
    conn.response :json, content_type: /\b(json)$/
    conn.headers['X-API-KEY']     = config.api_key
    conn.headers['X-API-SECRET']  = config.api_secret
    conn.adapter Faraday.default_adapter
  end
end

#performObject



9
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
# File 'lib/ipecache/plugins/swisstxt_cdn.rb', line 9

def perform
  safe_require 'uri'
  safe_require 'faraday_middleware'

  plugin_puts "Beginning URL Purge from SWISS TXT CDN..."

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

    begin
      conn = connection
      unless host = URI.parse(url).host
        raise "Invalid URL: No host found in URL. Missing schema?"
      end
      path = URI.parse(url).path
      response = conn.post('/purge', host: host, path: path)
    rescue => e
      plugin_puts_error(url, e.message)
      continue_on_error ? next : exit(1)
    end

    if response.status != 200
      plugin_puts_error(url, "Response Code: #{response.status}")
      plugin_puts_error(url, response.body['error'] || response.body)
      exit 1 unless continue_on_error
    else
      plugin_puts "Purge successful!"
    end
  end
end