Class: NightcrawlerSwift::Upload

Inherits:
Command
  • Object
show all
Defined in:
lib/nightcrawler_swift/commands/upload.rb

Instance Method Summary collapse

Methods inherited from Command

#connection, #options

Instance Method Details

#execute(path, file, opts = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nightcrawler_swift/commands/upload.rb', line 4

def execute path, file, opts = {}
  if path.nil? or path.empty?
    raise Exceptions::ValidationError.new "Upload command requires a path parameter"
  end

  body = file.read
  headers = {etag: etag(body), content_type: content_type(file)}

  max_age = opts[:max_age] || options.max_age
  headers.merge!(cache_control: "public, max-age=#{max_age}") if max_age

  expires = opts[:expires]
  headers.merge!(expires: CGI.rfc1123_date(expires)) if expires

  content_encoding = opts[:content_encoding] || options.content_encoding
  headers.merge!(content_encoding: content_encoding.to_s) if content_encoding

  custom_headers = opts[:custom_headers]
  headers.merge!(custom_headers) if custom_headers

  response = put "#{connection.upload_url}/#{path}", body: body, headers: headers
  response.code >= 200 && response.code < 300
end