Class: Middleman::Cli::RackspaceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-cdn/clients/rackspace.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, api_key) ⇒ RackspaceClient

Returns a new instance of RackspaceClient.



7
8
9
10
11
# File 'lib/middleman-cdn/clients/rackspace.rb', line 7

def initialize(username, api_key)
  @username = username
  @api_key = api_key
  @auth_data = nil
end

Instance Method Details

#invalidate(region, container, file, notification_email: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/middleman-cdn/clients/rackspace.rb', line 13

def invalidate(region, container, file, notification_email: nil)
  headers = { "x-auth-token" => get_auth_token }
  headers.merge!({ "x-purge-email" => notification_email }) if notification_email.present?
  response = HTTParty.delete("#{get_cdn_endpoint(region)}/#{container}#{URI.escape(file)}", { :headers => headers })
  case response.header.code
  when "204"
    # success
  when "400"
    error_message = response.headers["x-purge-failed-reason"]
    raise "400, #{error_message}" if error_message.present?
    raise "400, an error occurred."
  when "403"
    raise "403, the server refused to respond to the request. Check your credentials."
  when "404"
    raise "404, the requested resource could not be found."
  else
    error_message = response.body
    raise "#{response.header.code}, an error occurred. #{error_message}".rstrip
  end
end