forked from rubygems/rubygems.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastly.rb
37 lines (33 loc) · 1.33 KB
/
fastly.rb
1
2
3
4
5
6
7
8
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
require 'net/http'
class Net::HTTP::Purge < Net::HTTPRequest
METHOD = 'PURGE'.freeze
REQUEST_HAS_BODY = false
RESPONSE_HAS_BODY = true
end
class Fastly
def self.purge(path, soft = false)
return unless ENV['FASTLY_DOMAINS']
ENV['FASTLY_DOMAINS'].split(",").each do |domain|
url = "https://#{domain}/#{path}"
headers = soft ? { 'Fastly-Soft-Purge' => 1 } : {}
response = RestClient::Request.execute(method: :purge,
url: url,
timeout: 10,
headers: headers)
json = JSON.parse(response)
Rails.logger.debug "Fastly purge url=#{url} status=#{json['status']} id=#{json['id']}"
end
end
def self.purge_key(key, soft = false)
headers = { 'Fastly-Key' => ENV['FASTLY_API_KEY'] }
headers['Fastly-Soft-Purge'] = 1 if soft
url = "https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/purge/#{key}"
response = RestClient::Request.execute(method: :post,
url: url,
timeout: 10,
headers: headers)
json = JSON.parse(response)
Rails.logger.debug "Fastly purge url=#{url} status=#{json['status']} id=#{json['id']}"
json
end
end