Class: S3Master::LocalPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_master/local_policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, bucket_name, policy_type, options = {}) ⇒ LocalPolicy

Returns a new instance of LocalPolicy.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/s3_master/local_policy.rb', line 5

def initialize(cfg, bucket_name, policy_type, options={})
  @config = cfg
  @bucket_name = bucket_name
  @policy_type = policy_type.to_sym
  @options = options
  @policy_id = options[:id]

  if @config["buckets"][@bucket_name].nil?
    raise(RuntimeError, "No bucket named '#{@bucket_name}' found in loaded config.")
  end

  load_policy if !options[:skip_load]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/s3_master/local_policy.rb', line 3

def body
  @body
end

Instance Method Details

#basenameObject



23
# File 'lib/s3_master/local_policy.rb', line 23

def basename() @config.template_relname(@bucket_name, @policy_type, @policy_id) ; end

#empty?Boolean

Returns:

  • (Boolean)


20
# File 'lib/s3_master/local_policy.rb', line 20

def empty?() @body.nil? || @body.empty? ; end

#load_policyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/s3_master/local_policy.rb', line 26

def load_policy
  @body = if basename.nil? || basename == false
            # Empty policy
            {}
          else
            JSON.parse(File.binread(path))
          end

  if ! self.preserve_keys?
    @body.deep_transform_keys!{|k| k.underscore.to_sym }
  end

  @body
end

#pathObject



24
# File 'lib/s3_master/local_policy.rb', line 24

def path() File.join(@options[:"policy-dir"], self.basename) ; end

#preserve_keys?Boolean

Returns:

  • (Boolean)


19
# File 'lib/s3_master/local_policy.rb', line 19

def preserve_keys?() S3Master::RemotePolicy::POLICIES[@policy_type][:preserve_keys] ; end

#pretty_bodyObject



21
# File 'lib/s3_master/local_policy.rb', line 21

def pretty_body() JSON.neat_generate(body, sort: (self.preserve_keys? ? false : true)) ; end

#write(other_policy) ⇒ Object



41
42
43
44
45
# File 'lib/s3_master/local_policy.rb', line 41

def write(other_policy)
  File.open(self.path, "wb") do |fh|
    fh.puts other_policy.pretty_body
  end
end