Skip to content

Commit

Permalink
Introducing yield! for directives
Browse files Browse the repository at this point in the history
Adding each_line text file helper
  • Loading branch information
mtgrosser committed Sep 11, 2017
1 parent dbeb8c8 commit 769ae58
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
23 changes: 20 additions & 3 deletions lib/ngxc.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Ngxc
VERSION = '0.1.1'
VERSION = '0.2.0'

class Text
def initialize(text)
Expand All @@ -18,6 +18,7 @@ class Configurable
@@root_path = ''
@@directives = []
@@aliases = {}
@@block_stack = []

class << self
def root_path
Expand Down Expand Up @@ -49,8 +50,15 @@ def directive?(name)
@@directives.include?(name)
end

def define(name, &block)
define_method(name) { |*args| instance_exec(*args, &block) }
def define(name, &defn)
define_method(name) do |*args, &block|
begin
@@block_stack.push block
instance_exec(*args, &defn)
ensure
@@block_stack.pop
end
end
end

end
Expand Down Expand Up @@ -171,6 +179,15 @@ def include(file)
instance_eval File.read(file), file
end

def yield!(*args)
instance_exec(*args, &@@block_stack.last) if @@block_stack.last
end

def each_line(file, &block)
file = File.join(self.class.root_path, file) unless file.start_with?('/')
File.read(file).split("\n").map { |l| l.strip }.reject { |l| l.size == 0 }.each(&block)
end

def <<(obj)
case obj
when Proc
Expand Down
5 changes: 5 additions & 0 deletions test/blocklist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/blocked_path
~ \.php$



43 changes: 42 additions & 1 deletion test/expected.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ http {
default_type application/octet-stream;
passenger_default_ruby /opt/bin/ruby;

server 9.10.11.12 {
# this is a comment regarding barfoo.com
server_name barfoo.com;
ssl_certificate /etc/ssl/private/barfoo.com.crt;
ssl_certificate_key /etc/ssl/private/barfoo.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location /blocked_path {
return 404;
}

location ~ \.php$ {
return 404;
}

location /block_argument {
if ($something) {
return 418;
}
}
}

server 5.6.7.8 {
# this is a comment regarding example.com
server_name example.com;
Expand All @@ -30,7 +55,15 @@ http {
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location /wuppy {
location /blocked_path {
return 404;
}

location ~ \.php$ {
return 404;
}

location /block_yield {
if ($something) {
return 418;
}
Expand All @@ -46,6 +79,14 @@ http {
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location /blocked_path {
return 404;
}

location ~ \.php$ {
return 404;
}
# hallo foobar
}
return 404;
Expand Down
7 changes: 7 additions & 0 deletions test/includes/extras.ngxc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define :location_returns do |loc, status|
location(loc) { return! status }
end

define :location_blocklist do |textfile, status = 404|
each_line(textfile) { |loc| location_returns loc, status }
end
2 changes: 2 additions & 0 deletions test/includes/ssl_encryption.ngxc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ define :vserver do |host, ipaddr, extras = nil|
self << "# this is a comment regarding #{host}"
server_name host
ssl_encryption host
location_blocklist 'blocklist.txt'
self << extras
yield!
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/sites/barfoo.com.ngxc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vserver 'barfoo.com', '9.10.11.12', -> {
location '/block_argument' do
if! '($something)' do
return! 418
end
end
}
6 changes: 3 additions & 3 deletions test/sites/example.com.ngxc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vserver 'example.com', '5.6.7.8', -> {
location '/wuppy' do
vserver 'example.com', '5.6.7.8' do
location '/block_yield' do
if! '($something)' do
return! 418
end
end
}
end
4 changes: 2 additions & 2 deletions test/sites/foobar.com.ngxc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vserver 'foobar.com', '1.2.3.4', -> {
vserver 'foobar.com', '1.2.3.4' do
self << "# hallo foobar"
}
end

0 comments on commit 769ae58

Please sign in to comment.