Class: Gct::TempRemoteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gct/temp_remote_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, content) ⇒ TempRemoteFile

Returns a new instance of TempRemoteFile.



5
6
7
8
# File 'lib/gct/temp_remote_file.rb', line 5

def initialize(url, content)
  @uri = URI.parse(url)
  @content = content
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/gct/temp_remote_file.rb', line 3

def uri
  @uri
end

Instance Method Details

#closeObject



19
20
21
22
# File 'lib/gct/temp_remote_file.rb', line 19

def close
  @file.close
  @file.unlink
end

#encodingObject



28
29
30
31
# File 'lib/gct/temp_remote_file.rb', line 28

def encoding
  io.rewind
  io.read.encoding
end

#fileObject



10
11
12
13
14
15
16
17
# File 'lib/gct/temp_remote_file.rb', line 10

def file
  @file ||= Tempfile.open(tmp_filename, tmp_folder, encoding: encoding) do |f|
    puts f
    io.rewind
    f.write(io.read)
    f.close
  end
end

#ioObject



24
25
26
# File 'lib/gct/temp_remote_file.rb', line 24

def io
  @io ||= uri.open
end

#tmp_filenameObject



33
34
35
36
37
38
# File 'lib/gct/temp_remote_file.rb', line 33

def tmp_filename
  [
    Pathname.new(uri.path).basename,
    Pathname.new(uri.path).extname
  ]
end

#tmp_folderObject



40
41
42
# File 'lib/gct/temp_remote_file.rb', line 40

def tmp_folder
  Generator::GctFile.temp_folder_path
end