edge_rack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.
@@ -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,11 @@
1
+ require 'rack'
2
+ require 'net/http'
3
+ require 'pathname'
4
+
5
+ module EdgeRack
6
+ end
7
+
8
+ require 'edge_rack/middleware'
9
+ if defined?(Rails)
10
+ require 'edge_rack/railtie'
11
+ end
@@ -0,0 +1,79 @@
1
+ module EdgeRack
2
+ class Middleware
3
+ def initialize(app, options={})
4
+ @app = app
5
+
6
+ @options = {
7
+ project_path: Dir.pwd,
8
+ project_name: Pathname.new(Dir.pwd).basename,
9
+ host: "http://localhost"
10
+ }.merge(options)
11
+
12
+ Thread.new do
13
+ Net::HTTP.post_form(
14
+ URI.parse('http://localhost:48626/project'),
15
+ {
16
+ project_path: @options[:project_path],
17
+ project_name: @options[:project_name]
18
+ }
19
+ )
20
+ end
21
+ end
22
+
23
+ def call(env)
24
+ @env = env
25
+
26
+ @status, @headers, @response = @app.call(env)
27
+
28
+ if is_edge_compatible_response?
29
+ update_response!
30
+ update_content_length!
31
+ end
32
+
33
+ [@status, @headers, @response]
34
+ end
35
+
36
+ private
37
+
38
+ def update_response!
39
+ @response.each do |part|
40
+ if is_regular_request? && is_html_response?
41
+ insert_at = part.index('</body')
42
+ unless insert_at.nil?
43
+ part.insert(insert_at, render_edge_scripts)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def update_content_length!
50
+ new_size = 0
51
+ @response.each{|part| new_size += part.bytesize}
52
+ @headers.merge!("Content-Length" => new_size.to_s)
53
+ end
54
+
55
+ def is_regular_request?
56
+ !is_ajax_request?
57
+ end
58
+
59
+ def is_ajax_request?
60
+ @env.has_key?("HTTP_X_REQUESTED_WITH") && @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
61
+ end
62
+
63
+ def is_html_response?
64
+ @headers["Content-Type"].include?("text/html") if @headers.has_key?("Content-Type")
65
+ end
66
+
67
+ def is_edge_compatible_response?
68
+ return false if @status == 302
69
+ return false if @env.has_key?("HTTP_SKIP_EDGE_MIDDLEWARE")
70
+ is_html_response?
71
+ end
72
+
73
+ def render_edge_scripts
74
+ <<-EOT
75
+ <script type="text/javascript" src="http://localhost:48626/edge.js" data-project="#{@options[:project_name]}"/>
76
+ EOT
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,9 @@
1
+ module EdgeRack
2
+ class Railtie < Rails::Railtie
3
+ initializer "edge_rack.configure_rails_initialization" do |app|
4
+ if Rails.env.development?
5
+ app.middleware.use EdgeRack::Middleware
6
+ end
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: edge_rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nc
9
+ - bmalet
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: jeweler
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 1.8.4
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.4
47
+ description: Connects your rack app to Edge (http://getedge.io) so you can live edit
48
+ stylesheets within your app
49
+ email: support@mech.io
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files:
53
+ - LICENSE.txt
54
+ - README.rdoc
55
+ files:
56
+ - lib/edge_rack.rb
57
+ - lib/edge_rack/middleware.rb
58
+ - lib/edge_rack/railtie.rb
59
+ - LICENSE.txt
60
+ - README.rdoc
61
+ homepage: http://github.com/nc/edge_rack
62
+ licenses:
63
+ - MIT
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: -3313618066453336128
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Edge plugin for rack apps
89
+ test_files: []