Class: Takana::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-takana/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rack-takana/middleware.rb', line 10

def initialize(app, options={})
  @path = Dir.pwd.to_s
  @name = Pathname.new(@path).basename.to_s
  @app = app
  @kind, @paths = detect_kind!
  @debug = false
  @host = "localhost"
  @port = 48626

  if Takana.db

    if defined?(Rails)
      Rails.logger.debug "[Takana] #{@name} at #{@path} #{@kind} #{@paths}"
    end

    @project = Takana::Project[name: name] || Takana::Project.create(name: name, path: path, activated: false)

    @paths.each do |path|
      unless @project.load_paths.find { |_| _.path == path.to_s }.present? 
        @project.add_load_path(path: path.to_s, source: 'rack-takana')
      end

    end

    if defined?(Compass) && defined?(Rails) && Rails.application.config.compass.present?

      Rails.logger.debug "[Takana] detected compass. Attempting to load configuration"

      begin
        config = Rails.application.config.compass.serialize

        if config.present?
          attributes = {}

          Compass::Configuration::ATTRIBUTES.each do |k|
            attributes[k] = Rails.application.config.compass.send(k)
          end

          @project.compass_configs.each(&:delete)
          @project.add_compass_config(
            config: config,
            attributes: Marshal.dump(attributes)
          )
        end

        # tell projects to reload
        begin
          url = URI.parse("http://#{@host}:#{@port}/projects/load")
          req = Net::HTTP::Get.new(url.to_s)
          res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
        rescue => e
          Rails.logger.debug "[Takana] project reload request failed, perhaps takana isn't running #{e.inspect}"
        end

      rescue => e
        Rails.logger.debug "[Takana] failed to load compass configuration"

      end
    end
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/rack-takana/middleware.rb', line 3

def app
  @app
end

#debugObject Also known as: debug?

TODO allow this to be toggled from configure block



6
7
8
# File 'lib/rack-takana/middleware.rb', line 6

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/rack-takana/middleware.rb', line 3

def host
  @host
end

#kindObject (readonly)

Returns the value of attribute kind.



3
4
5
# File 'lib/rack-takana/middleware.rb', line 3

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rack-takana/middleware.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/rack-takana/middleware.rb', line 3

def path
  @path
end

#pathsObject (readonly)

Returns the value of attribute paths.



3
4
5
# File 'lib/rack-takana/middleware.rb', line 3

def paths
  @paths
end

Instance Method Details

#call(env) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rack-takana/middleware.rb', line 72

def call(env)
  @env = env

  @status, @headers, @response = @app.call(env)
  
  if is_takana_compatible_response?
    update_response!
    update_content_length!
  end

  [@status, @headers, @response]
end