Class: FileStore::Providers::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/file_store/providers/provider.rb

Direct Known Subclasses

S3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Provider

Returns a new instance of Provider.



8
9
10
11
12
13
14
15
# File 'lib/file_store/providers/provider.rb', line 8

def initialize(opts = {})
  if (missing_fields = required_configs - opts.keys).length > 0
    fail "missing fields: #{missing_fields.join(', ')}"
  end

  @mock = false
  @options = opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/file_store/providers/provider.rb', line 6

def options
  @options
end

Instance Method Details

#download_url(file_id, opts = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/file_store/providers/provider.rb', line 24

def download_url(file_id, opts = {})
  # Extract the provider from the file_id. i.e. For S3: s3://file/id/path
  # Validate that the current provider matches the file_id's provider
  extracted_file_id = file_id.gsub(/\A(.*):\/\//, '')
  fail "invalid provider: #{$1}" unless $1 == _provider_name
  download_url!(extracted_file_id, opts)
end

#mock!Object



32
33
34
# File 'lib/file_store/providers/provider.rb', line 32

def mock!
  @mock = true
end

#mock_download_url(file_id, opts = {}) ⇒ Object



45
46
47
48
# File 'lib/file_store/providers/provider.rb', line 45

def mock_download_url(file_id, opts = {})
  extracted_file_id = file_id.gsub(/\A(.*):\/\//, '')
  "http://mocked_download_url.com/#{extracted_file_id}"
end

#mock_upload(prefix, file_name, data = nil) ⇒ Object



40
41
42
43
# File 'lib/file_store/providers/provider.rb', line 40

def mock_upload(prefix, file_name, data = nil)
  ext = _generate_ext_if_needed(file_name)
  "mock://#{prefix}/000/000/000/000/#{file_name}#{ext}"
end

#mocked?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/file_store/providers/provider.rb', line 36

def mocked?
  !!@mock
end

#upload(prefix, file_name, data = nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/file_store/providers/provider.rb', line 17

def upload(prefix, file_name, data = nil)
  ext = _generate_ext_if_needed(file_name)
  file_path = generate_unique_path(prefix) + "/#{file_name}#{ext}"
  file_id = upload!(file_path, data)
  "#{_provider_name}://#{file_id}"
end