Class: Rack::QiniuMock::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/qiniu_mock/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) {|_self| ... } ⇒ Context

Returns a new instance of Context.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
12
13
# File 'lib/rack/qiniu_mock/context.rb', line 6

def initialize(app, opts={})
  @app = app
  @root = File.expand_path("public", Rails.root)
  @config = YAML.load_file(File.expand_path("config/qiniu_mock.yml", Rails.root))
  @separator = @config['separator'] || '-'
  @suffixs = @config['suffixs'] || {}
  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/qiniu_mock/context.rb', line 15

def call(env)
  original_file, mtime = get_original_file(::Rack::Utils.unescape(env['PATH_INFO']))
  if original_file
    data = original_file.to_blob
    [200, {
      "Content-Type" => original_file.mime_type,
      "Content-Length" => data.size.to_s,
      "Cache-Control" => "public, max-age=31536000",
      "Last-Modified" => mtime.httpdate
      }, [data]]
  else
    @app.call(env)
  end
end