Sha256: 198231f00f529106bfb3f3e9c62207dea7046fc3f58abdfbef3e9080aaaa0091

Contents?: true

Size: 1022 Bytes

Versions: 9

Compression:

Stored size: 1022 Bytes

Contents

require "aws-sdk"
require "time"

module Ustyle
  REGION = 'eu-west-1'
  BUCKET = 'uswitch-assets-eu'
  CLOUDFRONT_DISTRIBUTION = 'E3F1XI0HIG20E0'

  ONE_YEAR_IN_S = 1 * 365 * 24 * 60 * 60
  ONE_YEAR_FROM_NOW = Time.now + ONE_YEAR_IN_S

  def self.s3_upload to, from, content_type, bucket = BUCKET
    bucket = s3.bucket(bucket)
    object = bucket.object(to)
    object.put(
      body: open(from),
      content_type: content_type,
      acl: 'public-read',
      cache_control: "max-age=#{ONE_YEAR_IN_S}, public",
      expires: ONE_YEAR_FROM_NOW.httpdate
    )
  end

  def self.invalidate files
    cloudfront = Aws::CloudFront::Client.new(region: REGION)
    cloudfront.create_invalidation(
      distribution_id: CLOUDFRONT_DISTRIBUTION,
      invalidation_batch: {
        paths: {
          quantity: files.length,
          items: files
        },
        caller_reference: "ustyle invalidation at #{Time.now.to_s}"
      }
    )
  end

  def self.s3
    @conn ||= Aws::S3::Resource.new(region: REGION)
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
reustyle-2.0.9 lib/ustyle/deploy.rb
reustyle-2.0.8 lib/ustyle/deploy.rb
reustyle-2.0.7 lib/ustyle/deploy.rb
ustyle-1.19.2 lib/ustyle/deploy.rb
ustyle-1.16.2 lib/ustyle/deploy.rb
ustyle-1.16.1 lib/ustyle/deploy.rb
ustyle-1.15.3 lib/ustyle/deploy.rb
ustyle-1.14.11 lib/ustyle/deploy.rb
ustyle-1.14.10 lib/ustyle/deploy.rb