Created
November 15, 2016 18:25
-
-
Save mscoutermarsh/b79068669a1910d96f62fe446ad23f75 to your computer and use it in GitHub Desktop.
Example Ruby for purging imgix cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: Set env variable IMGIX_API_KEY to your api key | |
# Need to add httparty to your gemfile | |
# | |
# Cdn::Purge.cache.call('urlhere') | |
class Cdn::PurgeCache | |
include HTTParty | |
API_KEY = ENV.fetch('IMGIX_API_KEY'.freeze) | |
base_uri 'https://api.imgix.com/v2'.freeze | |
class << self | |
def call(url) | |
new(url).call | |
end | |
end | |
def initialize(url) | |
@options = { body: { url: url }, basic_auth: { username: API_KEY, password: '' } } | |
end | |
def call | |
purge = self.class.post('/image/purger'.freeze, @options) | |
purge.success? | |
rescue Errno::ECONNRESET, SocketError | |
{ 'status' => 'failed' } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment