Class: Dragonfly::Server

Inherits:
Object show all
Defined in:
lib/dragonfly/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server.



15
16
17
18
19
# File 'lib/dragonfly/server.rb', line 15

def initialize(app)
  @app = app
  use_same_log_as(app)
  use_as_fallback_config(app)
end

Instance Method Details

#after_serve(&block) ⇒ Object



26
27
28
# File 'lib/dragonfly/server.rb', line 26

def after_serve(&block)
  self.after_serve_callback = block
end

#before_serve(&block) ⇒ Object



21
22
23
# File 'lib/dragonfly/server.rb', line 21

def before_serve(&block)
  self.before_serve_callback = block
end

#call(env) ⇒ Object



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
56
57
58
# File 'lib/dragonfly/server.rb', line 31

def call(env)
  if dragonfly_url == env["PATH_INFO"]
    dragonfly_response
  elsif (params = url_mapper.params_for(env["PATH_INFO"], env["QUERY_STRING"])) && params['job']
    job = Job.deserialize(params['job'], app)
    job.validate_sha!(params['sha']) if protect_from_dos_attacks
    response = Response.new(job, env)
    catch(:halt) do
      if before_serve_callback && response.served?
        before_serve_callback.call(job, env)
      end
      resp = response.to_response
      if after_serve_callback && resp[0] == 200 && resp[2].is_a?(Dragonfly::TempObject)
        after_serve_callback.call(job, env)
      end
      resp
    end
  else
    [404, {'Content-Type' => 'text/plain', 'X-Cascade' => 'pass'}, ['Not found']]
  end
rescue Serializer::BadString, Job::InvalidArray => e
  log.warn(e.message)
  [404, {'Content-Type' => 'text/plain'}, ['Not found']]
rescue Job::NoSHAGiven => e
  [400, {"Content-Type" => 'text/plain'}, ["You need to give a SHA parameter"]]
rescue Job::IncorrectSHA => e
  [400, {"Content-Type" => 'text/plain'}, ["The SHA parameter you gave (#{e}) is incorrect"]]
end

#url_for(job, opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/dragonfly/server.rb', line 60

def url_for(job, opts={})
  opts = opts.dup
  host = opts.delete(:host) || url_host
  params = stringify_keys(opts)
  params['job'] = job.serialize
  params['sha'] = job.sha if protect_from_dos_attacks
  url = url_mapper.url_for(params)
  "#{host}#{url}"
end