Class: Dailycred::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/dailycred/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, client_id, opts = {}) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
9
10
11
12
13
# File 'lib/dailycred/middleware.rb', line 5

def initialize(app, client_id, opts={})
  @opts = opts
  @opts[:url] ||= "https://www.dailycred.com"
  @opts[:modal] ||= false
  @opts[:sdk] ||= false
  @opts[:triggers] ||= []
  @app = app
  @client_id = client_id
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



3
4
5
# File 'lib/dailycred/middleware.rb', line 3

def client_id
  @client_id
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dailycred/middleware.rb', line 15

def call(env)
  @env = env
  @status, @headers, @response = @app.call(env)

  if @headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
    body = ""
    @response.each { |part| body << part }
    index = body.rindex("</body>")
    if index
      body.insert(index, render_dailycred_scripts)
      @headers["Content-Length"] = body.length.to_s
      @response = [body]
    end
  end

  [@status, @headers, @response]
end