nimbu 0.6.4 → 0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05ea5b7593ec34ac1b9021a9717c648cc990869d
4
- data.tar.gz: f4c39215744c4c55fcf49185164b7329a7fcc6c0
3
+ metadata.gz: 2849d39ff894ad5145ff30a8c440214496519b81
4
+ data.tar.gz: c46c6fe8fc3ee24688b0877a34907adfca08a365
5
5
  SHA512:
6
- metadata.gz: a8814292a860e967494b4fc8f45b9aeeb2f39da95adbec81bb3db0bbb751874a55a21beb9e846894730b22ee485794758d8abf8643da4f2809dc957c3d68ea29
7
- data.tar.gz: 06224ba232d2dbf588095deeb8d0c507ea3218e2ba43586905cb8a09f277eb2fa8e7825a39973a3ce08fec7de04ff79e29c000ec1c47a20540ca33c588b8fd01
6
+ metadata.gz: 65d192f03292576b9014adc87b1da3b0e832e9e90874181be424f3ce547d633b0451e3b833c8de760bac7a66818cc6805b9e39d73cce02d3c70e908742305724
7
+ data.tar.gz: 4e8edae2409f1dee25cedcec5a1b3f7cc44d9c77e69b25e520f16e4b93ed1390a810fe09b0418588c24b4e5a00060ed3f47c48315b16209cf87887d5f2e5873f
data/lib/nimbu/auth.rb CHANGED
@@ -10,6 +10,13 @@ class Nimbu::Auth
10
10
  attr_accessor :credentials
11
11
  attr_accessor :configuration
12
12
 
13
+ def simulator_id
14
+ return @simulator_id if defined? @simulator_id
15
+
16
+ ranges = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten
17
+ @simulator_id ||= (1..40).map{ ranges[rand(ranges.length)] }.join
18
+ end
19
+
13
20
  def client
14
21
  @client ||= begin
15
22
  Nimbu::Client.new(:oauth_token => token, :endpoint => host, :user_agent => self.user_agent)
@@ -78,8 +78,7 @@ class Nimbu::Command::Server < Nimbu::Command::Base
78
78
  [:INT, :TERM].each do |sig|
79
79
  Signal.trap(sig) do
80
80
  puts green("== Stopping HAML watcher\n")
81
- haml_listener.stop
82
- puts haml_listener
81
+ Thread.new { haml_listener.stop }
83
82
  end
84
83
  end
85
84
  Process.waitall
@@ -2,6 +2,7 @@
2
2
  require 'sinatra'
3
3
  require "sinatra/reloader"
4
4
  require "sinatra/multi_route"
5
+ require "sinatra/cookies"
5
6
  require "vendor/nimbu/okjson"
6
7
  require 'term/ansicolor'
7
8
  require "base64"
@@ -12,209 +13,69 @@ require 'rack/streaming_proxy'
12
13
  module Nimbu
13
14
  module Server
14
15
  class Base < Sinatra::Base
15
- helpers Sinatra::JSON
16
16
  include Term::ANSIColor
17
- register Sinatra::MultiRoute
18
17
 
19
- configure :development do
20
- register Sinatra::Reloader
21
- end
18
+ helpers Sinatra::JSON
19
+ helpers Sinatra::Cookies
20
+ register Sinatra::MultiRoute
22
21
 
23
22
  set :method_override, true
24
23
  set :static, true # set up static file routing
25
24
  set :public_folder, Dir.pwd # set up the static dir (with images/js/css inside)
26
25
 
27
26
  set :views, File.expand_path('../views', __FILE__) # set up the views dir
28
- set :haml, { :format => :html5 } # if you use haml
27
+ set :haml, { format: :html5 } # if you use haml
29
28
 
30
29
  # Your "actions" go here…
31
30
  #
32
31
 
33
- get '/media/*' do
34
- proxy = Rack::StreamingProxy::ProxyRequest.new(request,"http://#{nimbu.host}/media/#{params[:splat].first}")
35
- return [proxy.status, proxy.headers, proxy]
36
- end
37
-
38
32
  get '/__sinatra__/*' do
39
33
  return ""
40
34
  end
41
35
 
42
36
  get '/favicon.ico' do
43
- return ""
37
+ proxy = Rack::StreamingProxy::ProxyRequest.new(request,"http://#{Nimbu::Auth.site}.#{Nimbu::Auth.admin_host}/favicon.ico")
38
+ return [proxy.status, proxy.headers, proxy]
44
39
  end
45
40
 
46
- # post '*' do
47
- # path = request.path == "/" ? request.path : request.path.gsub(/\/$/,'')
48
- # result = json_decode(nimbu.post_request({:path => path, :extra => params, :session => session}))
41
+ route :get, :post, :put, :delete, :patch, '*' do
42
+ # clear the session after a restart of the browser
49
43
 
50
- # session[:logged_in] = true if result["logged_in"]
51
- # session[:flash] = result["flash"] if result["flash"]
52
- # redirect result["redirect_to"] and return if result["redirect_to"]
53
- # end
44
+ method = detect_http_method(request)
45
+ @templates = {}
54
46
 
55
- route :get, :post, :put, :delete, '*' do
56
-
57
- method = (
58
- if request.get? then "get"
59
- elsif request.post? then "post"
60
- elsif request.put? then "put"
61
- elsif request.delete? then "delete"
62
- end
63
- )
64
- puts green("#{method.upcase} #{request.fullpath}")
65
-
66
- # if request.post? || request.put? || request.delete?
67
- # ##### POST / PUT / DELET #####
68
- # path = request.path == "/" ? request.path : request.path.gsub(/\/$/,'')
69
- # begin
70
- # response = nimbu.post_request({:path => path, :extra => params, :method => method, :client_session => session, :ajax => request.xhr? })
71
- # puts "RESPONSE: #{response}" if Nimbu.debug
72
- # result = json_decode(response)
73
- # parse_session(result)
74
- # rescue Exception => e
75
- # if e.respond_to?(:http_body)
76
- # return e.http_body
77
- # else
78
- # raise e
79
- # end
80
- # end
81
-
82
- # session[:flash] = result["flash"] if result["flash"]
83
- # if request.xhr?
84
- # if !result["json"].nil?
85
- # puts "JSON: #{result["json"]["data"]}" if Nimbu.debug
86
- # status result["json"]["status"].to_i
87
- # return json(result["json"]["data"], :encoder => :to_json, :content_type => :js)
88
- # end
89
- # else
90
- # redirect result["redirect_to"] and return if result["redirect_to"]
91
- # end
92
- # else
93
- # # First get the template name and necessary subtemplates
94
- # ##### GET #####
95
- # path = request.path == "/" ? request.path : request.path.gsub(/\/$/,'')
96
- # begin
97
- # result = json_decode(nimbu.get_template({:path => path, :extra => params, :method => "get", :extra => params, :client_session => session, :ajax => request.xhr? }))
98
- # puts result if Nimbu.debug
99
- # parse_session(result)
100
- # rescue Exception => e
101
- # return e.http_body
102
- # end
103
-
104
- # redirect result["redirect_to"] and return if result["redirect_to"]
105
- # end
106
- ### GET THE TEMPLATES ###
107
- path = request.path == "/" ? request.path : request.path.gsub(/\/$/,'')
108
-
109
- begin
110
- params = ({} || params).merge({:simulator => {
111
- :host => request.host,
112
- :port => request.port,
113
- :path => path,
114
- :method => method,
115
- :session => session,
116
- :headers => request.env.to_json,
117
- }})
118
- result = nimbu.simulator(:subdomain => Nimbu::Auth.site).recipe(params)
119
- puts result if Nimbu.debug
120
- rescue Exception => e
121
- if e.respond_to?(:http_body)
122
- return e.http_body
123
- else
124
- raise e
125
- end
47
+ if cookies["nimbu_simulator_id"] != Nimbu::Auth.simulator_id
48
+ puts yellow("Refreshing session for simulation...")
49
+ cookies.each {|k,v| cookies.delete(k)}
50
+ response.set_cookie "nimbu_simulator_id", { :value => Nimbu::Auth.simulator_id, :http_only => false, :path => "/" }
51
+ redirect to(request.path) and return
126
52
  end
127
53
 
128
- if result["template"].nil? && !request.xhr?
129
- raise Sinatra::NotFound
130
- end
131
-
132
- unless result["template"] == "<<<< REDIRECT >>>>"
133
- template = result["template"].gsub(/buddha$/,'liquid')
134
- # Then render everything
135
- puts green(" => using template '#{template}'")
136
- # Read the template file
137
- template_file = File.join(Dir.pwd,'templates',template)
138
- template_file_haml = File.join(Dir.pwd,'templates',"#{template}.haml")
139
-
140
- if File.exists?(template_file)
141
- template_code = IO.read(template_file).force_encoding('UTF-8')
142
- elsif File.exists?(template_file_haml)
143
- template_code = IO.read(template_file_haml).force_encoding('UTF-8')
144
- template = "#{template}.haml"
145
- else
146
- puts red("Layout file '#{template_file}' is missing...")
147
- return render_missing(File.join('templates',template),'template')
148
- end
149
-
150
- if template_code=~ /You have an Error in your HAML code/
151
- return template_code
152
- end
153
-
154
- # Parse template file for a special layout
155
- search = Regexp.new("\{\% layout (.*) \%\}")
156
- if search =~ template_code
157
- # There seems to be a special layout?
158
- layout = $1.strip.gsub(/,$/, '').gsub(/^'/, '').gsub(/'$/, '').gsub(/^"/, '').gsub(/"$/, '')
159
- if !(layout =~ /\.liquid$/)
160
- layout = "#{layout}.liquid"
161
- end
162
- else
163
- layout = 'default.liquid'
164
- end
165
-
166
- # Read the layout file
167
- layout_file = File.join(Dir.pwd,'layouts',layout)
168
- layout_file_haml = File.join(Dir.pwd,'layouts',"#{layout}.haml")
169
-
170
- if File.exists?(layout_file)
171
- layout_code = IO.read(layout_file).force_encoding('UTF-8')
172
- elsif File.exists?(layout_file_haml)
173
- layout = "#{layout}.haml"
174
- layout_code = IO.read(layout_file_haml).force_encoding('UTF-8')
175
- else
176
- puts red("Layout file '#{layout_file}' is missing...")
177
- return render_missing(File.join('layouts',layout),'layout')
178
- end
179
-
180
- puts green(" using layout '#{layout}'")
54
+ puts green("#{method.upcase} #{request.fullpath}")
181
55
 
182
- begin
183
- snippets = parse_snippets(template_code)
184
- snippets = parse_snippets(layout_code,snippets)
185
- rescue Exception => e
186
- # If there is a snippet missing, we raise an error
187
- puts red("Snippet file '#{e.message}' is missing...")
188
- return render_missing(e.message,'snippet')
189
- end
190
-
191
- if snippets.any?
192
- puts green(" using snippets '#{snippets.keys.join('\', \'')}'")
193
- end
194
- else
195
- template_file = ""
196
- end
56
+ ### GET THE RECIPE FOR RENDERING THIS PAGE ###
57
+ path = extract_path(request)
58
+ templates = pack_templates!
59
+
60
+ params = request.params || {}
61
+ params.merge!({
62
+ simulator: {
63
+ version: "v2",
64
+ path: path,
65
+ code: templates,
66
+ request: {
67
+ host: request.host,
68
+ port: request.port,
69
+ method: method,
70
+ session: session,
71
+ headers: request.env.to_json
72
+ }
73
+ }
74
+ })
197
75
 
198
76
  # Send the templates to the browser
199
77
  begin
200
- params = ({} || params).merge({:simulator => {
201
- :path => path,
202
- :code => {
203
- :template_name => template,
204
- :template => template_code,
205
- :layout_name => layout,
206
- :layout => layout_code,
207
- :snippets => snippets,
208
- },
209
- :request => {
210
- :host => request.host,
211
- :port => request.port,
212
- :method => method,
213
- :session => session,
214
- :headers => request.env.to_json
215
- }
216
- }})
217
- results = nimbu.simulator(:subdomain => Nimbu::Auth.site).render(params)
78
+ results = nimbu.simulator(subdomain: Nimbu::Auth.site).render(params)
218
79
  puts results["status"] if Nimbu.debug
219
80
  puts results["headers"] if Nimbu.debug
220
81
  puts Base64.decode64(results["body"]).gsub(/\n/,'') if Nimbu.debug
@@ -223,9 +84,8 @@ module Nimbu
223
84
  headers results["headers"] unless results["headers"] == ""
224
85
  body Base64.decode64(results["body"])
225
86
  rescue Exception => error
226
- puts "Error!"
227
87
  puts "Error! #{error.http_body}"
228
- html = error.http_body
88
+ error.http_body
229
89
  end
230
90
  end
231
91
 
@@ -235,6 +95,23 @@ module Nimbu
235
95
 
236
96
  protected
237
97
 
98
+ def detect_http_method(request)
99
+ if request.get? then "get"
100
+ elsif request.post? then "post"
101
+ elsif request.patch? then "patch"
102
+ elsif request.put? then "put"
103
+ elsif request.delete? then "delete"
104
+ end
105
+ end
106
+
107
+ def extract_path(request)
108
+ if request.path == "/"
109
+ request.path
110
+ else
111
+ request.path.gsub(/\/$/,'')
112
+ end
113
+ end
114
+
238
115
  def render_missing_template(template)
239
116
  @messag = ""
240
117
  @messag += "<h1>A template file is missing!</h1>"
@@ -287,35 +164,25 @@ module Nimbu
287
164
  Nimbu::Auth.client
288
165
  end
289
166
 
290
- def parse_snippets(code, snippets = {})
291
- # Parse template file for snippets
292
- search = /\{\% include ([^\s]*).*\%\}/
293
- matches = code.scan(search)
294
- if !matches.empty?
295
- matches.each do |snippet|
296
- # There seems to be a special layout?
297
- snippet_name = snippet[0].strip.gsub(/,$/, '').gsub(/^'/, '').gsub(/'$/, '').gsub(/^"/, '').gsub(/"$/, '')
298
- if !(snippet_name =~ /\.liquid$/)
299
- snippet_name = "#{snippet_name}.liquid"
300
- end
301
- # Read the snippet file
302
- snippet_file = File.join(Dir.pwd,'snippets',snippet_name)
303
- snippet_file_haml = File.join(Dir.pwd,'snippets',"#{snippet_name}.haml")
167
+ def debug(message)
168
+ puts message if Nimbu.debug
169
+ end
170
+
171
+ def pack_templates!
172
+ ["layouts","templates","snippets"].each do |type|
173
+ load_files(type)
174
+ end
175
+ Base64.encode64(Zlib::Deflate.deflate(json_encode(@templates), Zlib::DEFAULT_COMPRESSION))
176
+ end
304
177
 
305
- if File.exists?(snippet_file)
306
- snippet_code = IO.read(snippet_file).force_encoding('UTF-8')
307
- snippets[snippet_name.to_sym] = snippet_code
308
- self.parse_snippets(snippet_code, snippets)
309
- elsif File.exists?(snippet_file_haml)
310
- snippet_code = IO.read(snippet_file_haml).force_encoding('UTF-8')
311
- snippets["#{snippet_name}.haml".to_sym] = snippet_code
312
- self.parse_snippets(snippet_code, snippets)
313
- else
314
- raise "#{snippet_file}"
315
- end
316
- end
178
+ def load_files(directory)
179
+ glob = Dir["#{Dir.pwd}/#{directory}/**/*.liquid","#{Dir.pwd}/#{directory}/**/*.liquid.haml"]
180
+ glob.each do |file|
181
+ name = file.gsub("#{Dir.pwd}/#{directory}/","")
182
+ code = IO.read(file).force_encoding('UTF-8')
183
+ @templates[directory] ||= {}
184
+ @templates[directory][name.to_s] = code
317
185
  end
318
- return snippets
319
186
  end
320
187
 
321
188
  end
data/lib/nimbu/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Nimbu
3
- VERSION = "0.6.4"
3
+ VERSION = "0.7"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nimbu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zenjoy BVBA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-06 00:00:00.000000000 Z
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: term-ansicolor