Class: CloudTempfile::Config

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/cloud_tempfile/config.rb

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cloud_tempfile/config.rb', line 56

def initialize
  self.enabled = true
  self.fog_region = nil
  self.fog_endpoint = '/'
  self.fog_provider = 'Local'

  self.public = false
  self.prefix = "tmp/"
  self.expiry = 600 # 10 Min = 600 Seconds

  self.fail_silently = false
  self.log_silently = true

  self.clean_up = false
  self.clean_up_older_than = 86400 # 1 Day = 86400 Seconds

  self.public_path = Rails.root.join 'public' if defined?(Rails)

  load_yml! if defined?(Rails) && yml_exists?
end

Instance Attribute Details

#aws_access_control_listObject

Amazon AWS



29
30
31
# File 'lib/cloud_tempfile/config.rb', line 29

def aws_access_control_list
  @aws_access_control_list
end

#aws_access_key_idObject

Amazon AWS



29
30
31
# File 'lib/cloud_tempfile/config.rb', line 29

def aws_access_key_id
  @aws_access_key_id
end

#aws_reduced_redundancyObject

Amazon AWS



29
30
31
# File 'lib/cloud_tempfile/config.rb', line 29

def aws_reduced_redundancy
  @aws_reduced_redundancy
end

#aws_secret_access_keyObject

Amazon AWS



29
30
31
# File 'lib/cloud_tempfile/config.rb', line 29

def aws_secret_access_key
  @aws_secret_access_key
end

#clean_upObject

Returns the value of attribute clean_up.



18
19
20
# File 'lib/cloud_tempfile/config.rb', line 18

def clean_up
  @clean_up
end

#clean_up_older_thanObject

Returns the value of attribute clean_up_older_than.



19
20
21
# File 'lib/cloud_tempfile/config.rb', line 19

def clean_up_older_than
  @clean_up_older_than
end

#enabledObject

CloudTempfile



12
13
14
# File 'lib/cloud_tempfile/config.rb', line 12

def enabled
  @enabled
end

#expiryObject

Returns the value of attribute expiry.



17
18
19
# File 'lib/cloud_tempfile/config.rb', line 17

def expiry
  @expiry
end

#fail_silentlyObject

Logging Options



38
39
40
# File 'lib/cloud_tempfile/config.rb', line 38

def fail_silently
  @fail_silently
end

#fog_directoryObject

e.g. ‘the-bucket-name’



24
25
26
# File 'lib/cloud_tempfile/config.rb', line 24

def fog_directory
  @fog_directory
end

#fog_endpointObject

e.g. ‘/’



26
27
28
# File 'lib/cloud_tempfile/config.rb', line 26

def fog_endpoint
  @fog_endpoint
end

#fog_hostObject

FOG configuration



22
23
24
# File 'lib/cloud_tempfile/config.rb', line 22

def fog_host
  @fog_host
end

#fog_providerObject

Currently Supported [‘AWS’, ‘Google’, ‘Rackspace’, ‘Local’]



23
24
25
# File 'lib/cloud_tempfile/config.rb', line 23

def fog_provider
  @fog_provider
end

#fog_regionObject

e.g. ‘eu-west-1’ or ‘us-east-1’ etc…



25
26
27
# File 'lib/cloud_tempfile/config.rb', line 25

def fog_region
  @fog_region
end

#google_storage_access_key_idObject

Google Storage



35
36
37
# File 'lib/cloud_tempfile/config.rb', line 35

def google_storage_access_key_id
  @google_storage_access_key_id
end

#google_storage_secret_access_keyObject

Google Storage



35
36
37
# File 'lib/cloud_tempfile/config.rb', line 35

def google_storage_secret_access_key
  @google_storage_secret_access_key
end

#log_silentlyObject

Returns the value of attribute log_silently.



39
40
41
# File 'lib/cloud_tempfile/config.rb', line 39

def log_silently
  @log_silently
end

#prefixObject

Returns the value of attribute prefix.



14
15
16
# File 'lib/cloud_tempfile/config.rb', line 14

def prefix
  @prefix
end

#publicObject

Returns the value of attribute public.



15
16
17
# File 'lib/cloud_tempfile/config.rb', line 15

def public
  @public
end

#public_pathObject

Returns the value of attribute public_path.



16
17
18
# File 'lib/cloud_tempfile/config.rb', line 16

def public_path
  @public_path
end

#rackspace_api_keyObject

Rackspace



32
33
34
# File 'lib/cloud_tempfile/config.rb', line 32

def rackspace_api_key
  @rackspace_api_key
end

#rackspace_auth_urlObject

Rackspace



32
33
34
# File 'lib/cloud_tempfile/config.rb', line 32

def rackspace_auth_url
  @rackspace_auth_url
end

#rackspace_usernameObject

Rackspace



32
33
34
# File 'lib/cloud_tempfile/config.rb', line 32

def rackspace_username
  @rackspace_username
end

Instance Method Details

#aws?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/cloud_tempfile/config.rb', line 93

def aws?
  fog_provider == 'AWS'
end

#aws_acl?Boolean

Set file’s access control list (ACL). Valid acls: private, public-read, public-read-write, authenticated-read, bucket-owner-read, bucket-owner-full-control

Returns:

  • (Boolean)


103
104
105
# File 'lib/cloud_tempfile/config.rb', line 103

def aws_acl?
  ['private', 'public-read', 'public-read-write', 'authenticated-read', 'bucket-owner-read', 'bucket-owner-full-control'].include?(aws_access_control_list)
end

#aws_rrs?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/cloud_tempfile/config.rb', line 97

def aws_rrs?
  aws_reduced_redundancy == true
end

#clean_up?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/cloud_tempfile/config.rb', line 119

def clean_up?
  clean_up == true
end

#enabled?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/cloud_tempfile/config.rb', line 77

def enabled?
  enabled == true
end

#expiry?Boolean

def public_path

@public_path || Rails.public_path

end

Returns:

  • (Boolean)


89
90
91
# File 'lib/cloud_tempfile/config.rb', line 89

def expiry?
  !expiry.nil?
end

#fail_silently?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/cloud_tempfile/config.rb', line 203

def fail_silently?
  fail_silently || !enabled?
end

#fog_optionsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cloud_tempfile/config.rb', line 123

def fog_options
  #If the CloudTempfile is disabled then revert to local file creation
  @fog_provider = 'Local' if (@enabled != true)

  options = { :provider => fog_provider }
  if aws?
    options.merge!({
      :aws_access_key_id => aws_access_key_id,
      :aws_secret_access_key => aws_secret_access_key
    })
    options.merge!({:host => fog_host}) if !fog_host.blank?
  elsif rackspace?
    options.merge!({
      :rackspace_username => rackspace_username,
      :rackspace_api_key => rackspace_api_key
    })
    options.merge!({
      :rackspace_region => fog_region
    }) if fog_region
    options.merge!({ :rackspace_auth_url => rackspace_auth_url }) if rackspace_auth_url
  elsif google?
    options.merge!({
      :google_storage_secret_access_key => google_storage_secret_access_key,
      :google_storage_access_key_id => google_storage_access_key_id
    })
  elsif local?
    options.merge!({
      :local_root => public_path,
      :endpoint => fog_endpoint
    })
  else
    raise ArgumentError, "CloudTempfile Unknown provider: #{fog_provider} only AWS, Rackspace and Google are supported currently."
  end

  options.merge!({:region => fog_region}) if fog_region && !rackspace?
  return options
end

#google?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/cloud_tempfile/config.rb', line 111

def google?
  fog_provider == 'Google'
end

#load_yml!Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cloud_tempfile/config.rb', line 178

def load_yml!
  self.enabled                = yml["enabled"] if yml.has_key?('enabled')

  self.fog_provider           = yml["fog_provider"] if yml.has_key?('fog_provider')
  self.fog_directory          = yml["fog_directory"]
  self.fog_region             = yml["fog_region"]
  self.fog_host               = yml["fog_host"] if yml.has_key?("fog_host")
  self.aws_access_key_id      = yml["aws_access_key_id"]
  self.aws_secret_access_key  = yml["aws_secret_access_key"]
  self.aws_reduced_redundancy = yml["aws_reduced_redundancy"]
  self.rackspace_username     = yml["rackspace_username"]
  self.rackspace_auth_url     = yml["rackspace_auth_url"] if yml.has_key?("rackspace_auth_url")
  self.rackspace_api_key      = yml["rackspace_api_key"]
  self.google_storage_secret_access_key = yml["google_storage_secret_access_key"]
  self.google_storage_access_key_id     = yml["google_storage_access_key_id"]

  self.clean_up               = yml["clean_up"] if yml.has_key?("clean_up")
  self.clean_up_older_than    = yml["clean_up_older_than"] if yml.has_key?("clean_up_older_than")
  self.fail_silently          = yml["fail_silently"] if yml.has_key?("fail_silently")
  self.prefix                 = yml["prefix"] if yml.has_key?("prefix")
  self.public                 = yml["public"] if yml.has_key?("public")
  self.public_path            = yml["public_path"] if yml.has_key?("public_path")
  self.expiry                 = yml["expiry"] if yml.has_key?("expiry")
end

#local?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/cloud_tempfile/config.rb', line 115

def local?
  fog_provider == 'Local'
end

#log_silently?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/cloud_tempfile/config.rb', line 207

def log_silently?
  self.log_silently == false
end

#public?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/cloud_tempfile/config.rb', line 81

def public?
  public == true
end

#rackspace?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/cloud_tempfile/config.rb', line 107

def rackspace?
  fog_provider == 'Rackspace'
end

#ymlObject



166
167
168
169
170
171
172
# File 'lib/cloud_tempfile/config.rb', line 166

def yml
  begin
    @yml ||= YAML.load(ERB.new(IO.read(yml_path)).result)[Rails.env] rescue nil || {}
  rescue Psych::SyntaxError
    @yml = {}
  end
end

#yml_exists?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/cloud_tempfile/config.rb', line 162

def yml_exists?
  defined?(Rails.root) ? File.exists?(self.yml_path) : false
end

#yml_pathObject



174
175
176
# File 'lib/cloud_tempfile/config.rb', line 174

def yml_path
  Rails.root.join("config", "cloud_tempfile.yml").to_s
end