Class: Imgix::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/imgix/client.rb

Constant Summary collapse

DEFAULTS =
{ use_https: true }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/imgix/client.rb', line 12

def initialize(options = {})
  options = DEFAULTS.merge(options)
  @domain = options[:domain]

  validate_domain!

  @secure_url_token = options[:secure_url_token]
  @api_key = options[:api_key]
  @use_https = options[:use_https]
  @include_library_param = options.fetch(:include_library_param, true)
  @library = options.fetch(:library_param, "rb")
  @version = options.fetch(:library_version, Imgix::VERSION)
end

Instance Method Details

#path(path) ⇒ Object



26
27
28
29
30
# File 'lib/imgix/client.rb', line 26

def path(path)
  p = Path.new(prefix, @secure_url_token, path)
  p.ixlib("#{@library}-#{@version}") if @include_library_param
  p
end

#prefixObject



48
49
50
# File 'lib/imgix/client.rb', line 48

def prefix
  "#{@use_https ? 'https' : 'http'}://#{@domain}"
end

#purge(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/imgix/client.rb', line 32

def purge(path)
  api_key_error = "A valid API key is required to send purge requests"
  raise api_key_error if @api_key.nil?

  endpoint = URI.parse("https://api.imgix.com/api/v1/purge")
  # Ensure the path has been prefixed with '/'.
  path = path.start_with?("/") ? path : "/#{path}"
  url = prefix + path

  req = create_request(endpoint, url, :json_data_from)

  sock = Net::HTTP.new(endpoint.host, endpoint.port)
  sock.use_ssl = true
  sock.start { |http| http.request(req) }
end