Sha256: 9cd94cffac47babb13a9d8b161af0fee45d8cc44943e7d904329084fc75fa145

Contents?: true

Size: 877 Bytes

Versions: 1

Compression:

Stored size: 877 Bytes

Contents

module NightcrawlerSwift
  class Upload < Command

    def execute path, file, opts = {}
      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}", expires: expires(max_age)) if max_age

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

      response = put "#{connection.upload_url}/#{path}", body: body, headers: headers
      [200, 201].include?(response.code)
    end

    private

    def content_type file
      MultiMime.by_file(file).content_type
    end

    def etag content
      Digest::MD5.hexdigest(content)
    end

    def expires max_age
      CGI.rfc1123_date(Time.now + max_age)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nightcrawler_swift-0.9.0 lib/nightcrawler_swift/commands/upload.rb