Class: Middleman::S3Sync::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman/s3_sync/options.rb

Defined Under Namespace

Classes: BrowserCachePolicy

Constant Summary collapse

OPTIONS =
[
  :prefix,
  :http_prefix,
  :acl,
  :bucket,
  :region,
  :aws_access_key_id,
  :aws_secret_access_key,
  :after_build,
  :delete,
  :encryption,
  :existing_remote_file,
  :build_dir,
  :force,
  :prefer_gzip,
  :reduced_redundancy_storage,
  :path_style,
  :version_bucket,
  :verbose,
  :content_types
]

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



27
28
29
30
# File 'lib/middleman/s3_sync/options.rb', line 27

def initialize
  # read config from .s3_sync on initialization
  self.read_config
end

Instance Method Details

#aclObject



32
33
34
# File 'lib/middleman/s3_sync/options.rb', line 32

def acl
  @acl || 'public-read'
end

#add_caching_policy(content_type, options) ⇒ Object



36
37
38
# File 'lib/middleman/s3_sync/options.rb', line 36

def add_caching_policy(content_type, options)
  caching_policies[content_type.to_s] = BrowserCachePolicy.new(options)
end

#after_buildObject



76
77
78
# File 'lib/middleman/s3_sync/options.rb', line 76

def after_build
  @after_build.nil? ? false : @after_build
end

#aws_access_key_idObject



56
57
58
# File 'lib/middleman/s3_sync/options.rb', line 56

def aws_access_key_id
  @aws_access_key_id || ENV['AWS_ACCESS_KEY_ID']
end

#aws_access_key_id=(aws_access_key_id) ⇒ Object



52
53
54
# File 'lib/middleman/s3_sync/options.rb', line 52

def aws_access_key_id=(aws_access_key_id)
  @aws_access_key_id = aws_access_key_id if aws_access_key_id
end

#aws_secret_access_keyObject



64
65
66
# File 'lib/middleman/s3_sync/options.rb', line 64

def aws_secret_access_key
  @aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY']
end

#aws_secret_access_key=(aws_secret_access_key) ⇒ Object



60
61
62
# File 'lib/middleman/s3_sync/options.rb', line 60

def aws_secret_access_key=(aws_secret_access_key)
  @aws_secret_access_key = aws_secret_access_key if aws_secret_access_key
end

#caching_policiesObject



48
49
50
# File 'lib/middleman/s3_sync/options.rb', line 48

def caching_policies
  @caching_policies ||= Map.new
end

#caching_policy_for(content_type) ⇒ Object



40
41
42
# File 'lib/middleman/s3_sync/options.rb', line 40

def caching_policy_for(content_type)
  caching_policies.fetch(content_type.to_s, caching_policies[:default])
end

#content_typesObject



105
106
107
# File 'lib/middleman/s3_sync/options.rb', line 105

def content_types
  @content_types || {}
end

#default_caching_policyObject



44
45
46
# File 'lib/middleman/s3_sync/options.rb', line 44

def default_caching_policy
  caching_policies[:default]
end

#deleteObject



72
73
74
# File 'lib/middleman/s3_sync/options.rb', line 72

def delete
  @delete.nil? ? true : @delete
end

#encryptionObject



68
69
70
# File 'lib/middleman/s3_sync/options.rb', line 68

def encryption
  @encryption.nil? ? false : @encryption
end

#path_styleObject



84
85
86
# File 'lib/middleman/s3_sync/options.rb', line 84

def path_style
  (@path_style.nil? ? true : @path_style)
end

#prefer_gzipObject



80
81
82
# File 'lib/middleman/s3_sync/options.rb', line 80

def prefer_gzip
  (@prefer_gzip.nil? ? true : @prefer_gzip)
end

#prefixObject



97
98
99
# File 'lib/middleman/s3_sync/options.rb', line 97

def prefix
  @prefix.blank? ? "" : "#{@prefix}/"
end

#prefix=(prefix) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/middleman/s3_sync/options.rb', line 88

def prefix=(prefix)
  http_prefix = @http_prefix ? @http_prefix.sub(%r{^/}, "") : ""
  if http_prefix.split("/").first == prefix
    @prefix = ""
  else
    @prefix = prefix
  end
end

#read_config(io = nil) ⇒ void

This method returns an undefined value.

Read config options from an IO stream and set them on ‘self`. Defaults to reading from the `.s3_sync` file in the MM project root if it exists.

Parameters:

  • io (IO) (defaults to: nil)

    an IO stream to read from



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/middleman/s3_sync/options.rb', line 114

def read_config(io = nil)
  unless io
    root_path = ::Middleman::Application.root
    config_file_path = File.join(root_path, ".s3_sync")

    # skip if config file does not exist
    return unless File.exists?(config_file_path)

    io = File.open(config_file_path, "r")
  end

  config = YAML.load(io).symbolize_keys

  OPTIONS.each do |config_option|
    self.send("#{config_option}=".to_sym, config[config_option]) if config[config_option]
  end
end

#version_bucketObject



101
102
103
# File 'lib/middleman/s3_sync/options.rb', line 101

def version_bucket
  @version_bucket.nil? ? false : @version_bucket
end