Class: RestFtpDaemon::Location

Inherits:
Object
  • Object
show all
Includes:
CommonHelpers
Defined in:
lib/rest-ftp-daemon/location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonHelpers

#dashboard_url, #exception_to_error, #format_bytes, #identifier, #underscore

Constructor Details

#initialize(original) ⇒ Location

Returns a new instance of Location.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rest-ftp-daemon/location.rb', line 29

def initialize original
  # Strip spaces before/after, copying original "path" at the same time
  @original = original
  location_uri = original.strip

  # Replace tokens, fix scheme for local paths
  resolve_tokens! location_uri
  fix_scheme! location_uri

  # Ensure result does not contain tokens after replacement
  detected_tokens = detect_tokens(location_uri)
  unless detected_tokens.empty?
    raise RestFtpDaemon::UnresolvedTokens, detected_tokens.join(' ')
  end

  # Parse URL and do specific initializations
  parse_url location_uri
  case @uri
  when URI::FILE  then init_file
  when URI::S3    then init_aws               # Match AWS URL with BUCKET.s3.amazonaws.com
  end

  # Check that scheme is supported
  unless @uri.scheme
    raise RestFtpDaemon::UnsupportedScheme, url
  end
end

Instance Attribute Details

#aws_bucketObject (readonly)

Returns the value of attribute aws_bucket.



17
18
19
# File 'lib/rest-ftp-daemon/location.rb', line 17

def aws_bucket
  @aws_bucket
end

#aws_idObject (readonly)

Returns the value of attribute aws_id.



18
19
20
# File 'lib/rest-ftp-daemon/location.rb', line 18

def aws_id
  @aws_id
end

#aws_regionObject (readonly)

Returns the value of attribute aws_region.



16
17
18
# File 'lib/rest-ftp-daemon/location.rb', line 16

def aws_region
  @aws_region
end

#aws_secretObject (readonly)

Returns the value of attribute aws_secret.



19
20
21
# File 'lib/rest-ftp-daemon/location.rb', line 19

def aws_secret
  @aws_secret
end

#dirObject (readonly)

Returns the value of attribute dir.



14
15
16
# File 'lib/rest-ftp-daemon/location.rb', line 14

def dir
  @dir
end

#nameObject

Accessors



9
10
11
# File 'lib/rest-ftp-daemon/location.rb', line 9

def name
  @name
end

#originalObject (readonly)

Returns the value of attribute original.



11
12
13
# File 'lib/rest-ftp-daemon/location.rb', line 11

def original
  @original
end

#schemeObject (readonly)

Returns the value of attribute scheme.



13
14
15
# File 'lib/rest-ftp-daemon/location.rb', line 13

def scheme
  @scheme
end

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/rest-ftp-daemon/location.rb', line 12

def uri
  @uri
end

Instance Method Details

#generate_temp_name!Object



83
84
85
# File 'lib/rest-ftp-daemon/location.rb', line 83

def generate_temp_name!
  @name = "#{@name}.temp-#{identifier(JOB_TEMPFILE_LEN)}"
end

#is?(kind) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rest-ftp-daemon/location.rb', line 57

def is? kind
  @uri.is_a? kind
end

#pathObject Also known as: to_s



61
62
63
64
# File 'lib/rest-ftp-daemon/location.rb', line 61

def path
  return @name if @dir.nil?
  File.join(@dir.to_s, @name.to_s)
end

#scan_filesObject



67
68
69
70
71
72
73
74
# File 'lib/rest-ftp-daemon/location.rb', line 67

def scan_files
  Dir.glob(path).collect do |file|
    next unless File.readable? file
    next unless File.file? file
    # Create a new location object
    self.class.new(file)
  end
end

#sizeObject



76
77
78
79
80
81
# File 'lib/rest-ftp-daemon/location.rb', line 76

def size
  return unless uri.is_a? URI::FILE
  local_fil_path = path
  return unless File.exist? local_fil_path
  return File.size local_fil_path
end