class NightcrawlerSwift::Upload

Public Instance Methods

execute(path, file, opts = {}) click to toggle source
# 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

Private Instance Methods

content_type(file) click to toggle source
# File lib/nightcrawler_swift/commands/upload.rb, line 30
def content_type file
  MultiMime.by_file(file).content_type
end
etag(content) click to toggle source
# File lib/nightcrawler_swift/commands/upload.rb, line 34
def etag content
  Digest::MD5.hexdigest(content)
end