Class: Tui::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/tui/upload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Upload

Returns a new instance of Upload.



12
13
14
# File 'lib/tui/upload.rb', line 12

def initialize(path)
  @path = File.realpath(path)
end

Instance Attribute Details

#authorizationObject

Returns the value of attribute authorization.



10
11
12
# File 'lib/tui/upload.rb', line 10

def authorization
  @authorization
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/tui/upload.rb', line 9

def path
  @path
end

Instance Method Details

#as_jsonObject



59
60
61
62
63
64
65
66
# File 'lib/tui/upload.rb', line 59

def as_json
  {
    :original_filename => original_filename,
    :md5sum            => md5sum,
    :size              => size,
    :mime_type         => mime_type
  }
end

#chunksObject



37
38
39
# File 'lib/tui/upload.rb', line 37

def chunks
  @chunks ||= (size / Excon::Connection::CHUNK_SIZE.to_f).ceil
end

#fileObject



16
17
18
# File 'lib/tui/upload.rb', line 16

def file
  @file ||= File.new(path, "r")
end

#md5sumObject



28
29
30
31
# File 'lib/tui/upload.rb', line 28

def md5sum
  @digest ||= Digest::MD5.file(path)
  @md5sum ||= @digest.hexdigest
end

#mime_typeObject



33
34
35
# File 'lib/tui/upload.rb', line 33

def mime_type
  @mime_type ||= MIME::Types.type_for(path).first.to_s
end

#original_filenameObject



24
25
26
# File 'lib/tui/upload.rb', line 24

def original_filename
  @original_filename ||= File.basename(path)
end

#perform(authorization, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tui/upload.rb', line 41

def perform(authorization, &block)
  connection = Excon.new(authorization.upload_url)

  headers = {
    "User-Agent" => user_agent,
  }.merge(authorization.headers)

  connection.request_with_uploadprogress(
    :method          => "PUT",
    :body            => file,
    :headers         => headers,
    :expects         => 200,
    :upload_progress => block
  )

  true
end

#sizeObject



20
21
22
# File 'lib/tui/upload.rb', line 20

def size
  @size ||= File.size(path)
end