rack-takana 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 nc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = edge-rack
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to edge-rack
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 nc. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,33 @@
1
+ require 'rack'
2
+ require 'pathname'
3
+ require 'sequel'
4
+
5
+ module Takana
6
+ def self.sqlite_filepath
7
+ Pathname.new(File.expand_path("~")).join("Library", "Application Support", "Edge", "takana.db").to_s
8
+ end
9
+
10
+ def self.initialize_db
11
+ @db = Sequel.connect("sqlite://#{URI.escape(Takana.sqlite_filepath)}") rescue nil
12
+
13
+ Sequel::Model.db = @db
14
+ end
15
+
16
+ def self.db
17
+ @db
18
+ end
19
+ end
20
+
21
+ Takana.initialize_db
22
+
23
+ if Takana.db
24
+ require 'takana-rack/models/project'
25
+ require 'takana-rack/models/load_path'
26
+ require 'takana-rack/models/compass_config'
27
+ end
28
+
29
+ require 'takana-rack/middleware'
30
+
31
+ if defined?(Rails)
32
+ require 'takana-rack/railtie'
33
+ end
@@ -0,0 +1,106 @@
1
+ module Takana
2
+ class Middleware
3
+ attr_reader :name, :path, :app, :kind, :paths, :host
4
+
5
+ # TODO allow this to be toggled from configure block
6
+ attr_accessor :debug
7
+
8
+ alias_method :debug?, :debug
9
+
10
+ def initialize(app, options={})
11
+ @path = Dir.pwd.to_s
12
+ @name = Pathname.new(@path).basename.to_s
13
+ @app = app
14
+ @kind, @paths = detect_kind!
15
+ @debug = false
16
+ @host = "localhost"
17
+
18
+ if Takana.db
19
+ if defined?(Rails)
20
+ Rails.logger.debug "[Takana] #{@name} at #{@path} #{@kind} #{@paths}"
21
+ end
22
+
23
+ @project = Takana::Project[name: name] || Takana::Project.create(name: name, path: path, activated: false)
24
+
25
+ @paths.each { |p| @project.add_load_path(path: p, source: 'takana-rack') }
26
+
27
+ if defined?(Compass) && defined?(Rails)
28
+ begin
29
+ compass_config = Rails.application.config.compass.serialize
30
+ if compass_config.present? && @project.compass_config.nil?
31
+ @project.add_compass_config(config: compass_config)
32
+ end
33
+ rescue => e
34
+ Rails.logger.debug "[Takana] failed to detect / save compass config #{e}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def call(env)
41
+ @env = env
42
+
43
+ @status, @headers, @response = @app.call(env)
44
+
45
+ if is_takana_compatible_response?
46
+ update_response!
47
+ update_content_length!
48
+ end
49
+
50
+ [@status, @headers, @response]
51
+ end
52
+
53
+ private
54
+
55
+ def detect_kind!
56
+ if defined?(Rails)
57
+ paths = (Rails.application.config.sass[:load_paths] rescue []) + Rails.application.config.assets[:paths]
58
+
59
+ ["rails", paths]
60
+ else
61
+ ["rack", []]
62
+ end
63
+ end
64
+
65
+ def update_response!
66
+ @response.each do |part|
67
+ if is_regular_request? && is_html_response?
68
+ insert_at = part.index('</body')
69
+ unless insert_at.nil?
70
+ part.insert(insert_at, render_takana_scripts)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def update_content_length!
77
+ new_size = 0
78
+ @response.each{|part| new_size += part.bytesize}
79
+ @headers.merge!("Content-Length" => new_size.to_s)
80
+ end
81
+
82
+ def is_regular_request?
83
+ !is_ajax_request?
84
+ end
85
+
86
+ def is_ajax_request?
87
+ @env.has_key?("HTTP_X_REQUESTED_WITH") && @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
88
+ end
89
+
90
+ def is_html_response?
91
+ @headers["Content-Type"].include?("text/html") if @headers.has_key?("Content-Type")
92
+ end
93
+
94
+ def is_takana_compatible_response?
95
+ return false if @status == 302
96
+ return false if @env.has_key?("HTTP_SKIP_TAKANA_MIDDLEWARE")
97
+ is_html_response?
98
+ end
99
+
100
+ def render_takana_scripts
101
+ <<-EOT
102
+ <script type="text/javascript" src="http://#{host}:48626/javascripts/edge.js" data-project="#{name}" data-debug="#{debug?}"></script>
103
+ EOT
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,5 @@
1
+ module Takana
2
+ class CompassConfig < Sequel::Model
3
+
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Takana
2
+ class LoadPath < Sequel::Model
3
+
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Takana
2
+ class Project < Sequel::Model
3
+ one_to_many :load_paths
4
+ one_to_one :compass_config
5
+
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Takana
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "rack-tanaka.configure_rails_initialization" do |app|
5
+
6
+ if Rails.env.development?
7
+ app.middleware.use Takana::Middleware
8
+
9
+ end
10
+ end
11
+
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-takana
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mechio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sequel
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ description: Connects your rack app to Takana (http://www.usetakana.com) so you can
79
+ live edit stylesheets within a Rails app
80
+ email: support@mech.io
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files:
84
+ - LICENSE.txt
85
+ - README.rdoc
86
+ files:
87
+ - lib/rack-takana.rb
88
+ - lib/rack-takana/middleware.rb
89
+ - lib/rack-takana/models/compass_config.rb
90
+ - lib/rack-takana/models/load_path.rb
91
+ - lib/rack-takana/models/project.rb
92
+ - lib/rack-takana/railtie.rb
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ homepage: https://github.com/mechio/takana-rack
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
+ - 0
110
+ hash: -306390926943832107
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Takana plugin for rack apps
123
+ test_files: []