Class: Shrine::Plugins::RackResponse::FileBody

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/plugins/rack_response.rb

Overview

Implements the interface of a Rack response body object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, range: nil) ⇒ FileBody

Returns a new instance of FileBody.



120
121
122
123
# File 'lib/shrine/plugins/rack_response.rb', line 120

def initialize(file, range: nil)
  @file  = file
  @range = range
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Rack::Sendfile is activated when response body responds to #to_path.



145
146
147
# File 'lib/shrine/plugins/rack_response.rb', line 145

def method_missing(name, *args, &block)
  name == :to_path && path or super
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



118
119
120
# File 'lib/shrine/plugins/rack_response.rb', line 118

def file
  @file
end

#rangeObject (readonly)

Returns the value of attribute range.



118
119
120
# File 'lib/shrine/plugins/rack_response.rb', line 118

def range
  @range
end

Instance Method Details

#closeObject

Closes the file when response body is closed by the web server.



135
136
137
# File 'lib/shrine/plugins/rack_response.rb', line 135

def close
  file.close
end

#each(&block) ⇒ Object

Streams the uploaded file directly from the storage.



126
127
128
129
130
131
132
# File 'lib/shrine/plugins/rack_response.rb', line 126

def each(&block)
  if range
    read_partial_chunks(&block)
  else
    read_chunks(&block)
  end
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Rack::Sendfile is activated when response body responds to #to_path.

Returns:

  • (Boolean)


140
141
142
# File 'lib/shrine/plugins/rack_response.rb', line 140

def respond_to_missing?(name, include_private = false)
  name == :to_path && path
end