grape-dsl 2.0.1 → 2.0.2
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 +4 -4
- data/README.md +5 -0
- data/VERSION +1 -1
- data/lib/grape-dsl/ace.rb +49 -44
- data/lib/grape-dsl/dsl.rb +41 -15
- data/lib/grape-dsl/mnt.rb +78 -69
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08ef9da27a9c2af48368055da13388a3cf91c1c7
|
4
|
+
data.tar.gz: ad675add2489ab8ead52b5b1bbaa930610f49da2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 568a7b360a75a70d099538af348253afff6347b2cde05aedb77b1ddd9b6370f7bedd0ac0103afbc3440a5ea43686c6f3d21774c5dda905c0e798fea7d3a146cb
|
7
|
+
data.tar.gz: 93d78d9beabcf5bf9e2e6f34a1ff9fce6858733c29378332ee3ca6fc201752e841f773557d1d75afa56789b842b6b5fee3f4ff8c2fb2ac9d0a3205db6ab93091
|
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.0.
|
1
|
+
2.0.2
|
data/lib/grape-dsl/ace.rb
CHANGED
@@ -2,80 +2,86 @@ module GrapeDSL
|
|
2
2
|
module Include
|
3
3
|
module AccessControlEndpoint
|
4
4
|
|
5
|
-
|
5
|
+
module Helpers
|
6
|
+
class << self
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def generate_ip_regexp_collection *args
|
9
|
+
|
10
|
+
if args.empty?
|
11
|
+
raise ArgumentError, "missing ip(s) for allowed sources"
|
12
|
+
end
|
13
|
+
|
14
|
+
@cached_regexp_collection ||= {}
|
15
|
+
if @cached_regexp_collection[args].nil?
|
16
|
+
@cached_regexp_collection= {}
|
10
17
|
|
11
|
-
|
12
|
-
|
13
|
-
@cached_regexp_collection= {}
|
18
|
+
ip_regex_collection= []
|
19
|
+
args.each do |ip_addr|
|
14
20
|
|
15
|
-
|
16
|
-
args.each do |ip_addr|
|
21
|
+
ip_regex_builder= [[],[],[],[]]
|
17
22
|
|
18
|
-
|
23
|
+
#ip_addr.to_s.check(/([0-9\*]{1,3}\.){3}([0-9\*]{1,3})/)#(/([0-9\*]{1,3}\.){3}(0|\*)$/)
|
24
|
+
if (ip_addr.to_s =~ /([0-9\*]{1,3}\.){3}([0-9\*]{1,3})/).nil? ? false : true
|
19
25
|
|
20
|
-
|
21
|
-
|
26
|
+
ip_addr_index= 0
|
27
|
+
ip_addr.split('.').each do |ip_addr_part|
|
22
28
|
|
23
|
-
|
24
|
-
|
29
|
+
# 0.0.0.0
|
30
|
+
# 255.255.255.255
|
25
31
|
|
26
|
-
|
27
|
-
|
32
|
+
if ip_addr_part.include?("*")
|
33
|
+
ip_regex_builder[ip_addr_index]= "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
|
34
|
+
else
|
35
|
+
ip_regex_builder[ip_addr_index].push(ip_addr_part)
|
36
|
+
end
|
37
|
+
|
38
|
+
# increment index
|
39
|
+
ip_addr_index += 1
|
40
|
+
|
41
|
+
end
|
28
42
|
|
29
|
-
if ip_addr_part.include?("*")
|
30
|
-
ip_regex_builder[ip_addr_index]= "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
|
31
43
|
else
|
32
|
-
|
44
|
+
next
|
33
45
|
end
|
34
46
|
|
35
|
-
|
36
|
-
ip_addr_index += 1
|
47
|
+
ip_regex_builder.map! do |element|
|
37
48
|
|
38
|
-
|
49
|
+
case true
|
39
50
|
|
40
|
-
|
41
|
-
|
42
|
-
end
|
51
|
+
when element.class <= Regexp
|
52
|
+
element.inspect[1..(element.inspect.length-2)]
|
43
53
|
|
44
|
-
|
54
|
+
when element.class <= String
|
55
|
+
element
|
45
56
|
|
46
|
-
|
57
|
+
when element.class <= Array
|
58
|
+
"(#{element.join('|')})"
|
47
59
|
|
48
|
-
|
49
|
-
|
60
|
+
else
|
61
|
+
element.to_s
|
50
62
|
|
51
|
-
|
52
|
-
element
|
63
|
+
end
|
53
64
|
|
54
|
-
|
55
|
-
"(#{element.join('|')})"
|
65
|
+
end
|
56
66
|
|
57
|
-
|
58
|
-
element.to_s
|
67
|
+
ip_regex_collection.push /#{ip_regex_builder.join('\.')}/
|
59
68
|
|
60
69
|
end
|
61
70
|
|
71
|
+
@cached_regexp_collection[args]= ip_regex_collection
|
72
|
+
|
62
73
|
end
|
63
74
|
|
64
|
-
|
75
|
+
return @cached_regexp_collection[args]
|
65
76
|
|
66
77
|
end
|
67
78
|
|
68
|
-
@cached_regexp_collection[args]= ip_regex_collection
|
69
|
-
|
70
79
|
end
|
71
|
-
|
72
|
-
return @cached_regexp_collection[args]
|
73
|
-
|
74
80
|
end
|
75
81
|
|
76
82
|
def allowed_ips *args
|
77
83
|
|
78
|
-
tests= generate_ip_regexp_collection(*args).map{ |regexp|
|
84
|
+
tests= ::GrapeDSL::Include::AccessControlEndpoint::Helpers.generate_ip_regexp_collection(*args).map{ |regexp|
|
79
85
|
request.instance_variable_get("@env")['REMOTE_ADDR'] =~ regexp
|
80
86
|
}.compact
|
81
87
|
|
@@ -89,7 +95,7 @@ module GrapeDSL
|
|
89
95
|
|
90
96
|
def banned_ips *args
|
91
97
|
|
92
|
-
tests= generate_ip_regexp_collection(*args).map{ |regexp|
|
98
|
+
tests= ::GrapeDSL::Include::AccessControlEndpoint::Helpers.generate_ip_regexp_collection(*args).map{ |regexp|
|
93
99
|
request.instance_variable_get("@env")['REMOTE_ADDR'] =~ regexp
|
94
100
|
}.compact
|
95
101
|
|
@@ -102,7 +108,6 @@ module GrapeDSL
|
|
102
108
|
alias :banned_ip :banned_ips
|
103
109
|
|
104
110
|
|
105
|
-
|
106
111
|
end
|
107
112
|
end
|
108
113
|
end
|
data/lib/grape-dsl/dsl.rb
CHANGED
@@ -4,31 +4,57 @@ module GrapeDSL
|
|
4
4
|
|
5
5
|
module APIMNT
|
6
6
|
|
7
|
+
class Description
|
8
|
+
|
9
|
+
def initialize opts={}
|
10
|
+
raise unless opts.class <= ::Hash
|
11
|
+
opts.each{|k,v| self.__send__("#{k}=",v) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def [] sym
|
15
|
+
self.__send__ sym.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
def []= sym,value
|
19
|
+
self.__send__ "#{sym.to_s}=",value
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_accessor :description,:body,:content_type
|
23
|
+
alias desc= description=
|
24
|
+
alias desc description
|
25
|
+
alias type= content_type=
|
26
|
+
alias type content_type
|
27
|
+
|
28
|
+
def value
|
29
|
+
{description: description,content_type: content_type,body: body}
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
7
34
|
# defaults
|
8
35
|
# desc -> description for path
|
9
36
|
# body -> return body from the call
|
10
37
|
# convent_type -> real content type
|
11
|
-
def description
|
38
|
+
def description(*args)
|
12
39
|
|
13
|
-
if desc.class
|
14
|
-
tmp_string= desc
|
15
|
-
desc ::Hashie::Mash.new
|
16
|
-
desc[:desc]= tmp_string
|
17
|
-
end
|
40
|
+
if desc.class != ::GrapeDSL::Extend::APIMNT::Description
|
18
41
|
|
19
|
-
|
20
|
-
desc ::Hashie::Mash.new
|
21
|
-
end
|
42
|
+
var= ::GrapeDSL::Extend::APIMNT::Description.new(*args)
|
22
43
|
|
23
|
-
|
44
|
+
unless self.content_types.keys.empty?
|
24
45
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
46
|
+
content_type_name= nil
|
47
|
+
[:json,:xml,:txt].each do |element|
|
48
|
+
if self.content_types.keys.include? element
|
49
|
+
content_type_name ||= element.to_s.upcase
|
50
|
+
end
|
29
51
|
end
|
52
|
+
var.content_type= content_type_name
|
53
|
+
|
30
54
|
end
|
31
|
-
|
55
|
+
|
56
|
+
var.desc= desc.to_s
|
57
|
+
desc var
|
32
58
|
|
33
59
|
end
|
34
60
|
|
data/lib/grape-dsl/mnt.rb
CHANGED
@@ -34,116 +34,125 @@ module GrapeDSL
|
|
34
34
|
#
|
35
35
|
# you can give hash options just like to any other get,post put delete etc methods, it will work
|
36
36
|
#
|
37
|
-
def mount_method
|
37
|
+
def mount_method *args, &block
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
39
|
+
# process params && validations
|
40
|
+
begin
|
42
41
|
|
43
|
-
|
44
|
-
opts[:method] ||= opts[:m] || raise(ArgumentError,"missing method input(:method)")
|
42
|
+
opts= args.select{|e|(e.class <= ::Hash)}.reduce( {}, :merge! )
|
45
43
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
opts[:proc] ||= opts[:h] || opts[:prc] || opts[:hook] || block || Proc.new{}
|
50
|
-
opts[:path] ||= opts[:p] || opts[:method].name
|
51
|
-
opts[:args] ||= opts[:a] || opts[:arg] || {}
|
44
|
+
# required
|
45
|
+
opts[:method] ||= opts[:m] || args.select{|e|(e.class <= ::Method)}[0] || raise(ArgumentError,"missing method input(:method)")
|
46
|
+
unless [::String,::Symbol].select{|klass|(opts[:method].class <= klass)}.empty?
|
52
47
|
|
53
|
-
|
48
|
+
opts[:class] ||= opts[:c] || opts[:module] || raise(ArgumentError,"missing method input(:method)")
|
49
|
+
opts[:method] = opts[:class].method(opts[:method])
|
54
50
|
|
55
|
-
|
56
|
-
opts[:method] = opts[:class].method(opts[:method])
|
51
|
+
end
|
57
52
|
|
58
|
-
|
53
|
+
# optional
|
54
|
+
opts[:options] ||= opts[:o] || {}
|
55
|
+
opts[:rest_method] ||= opts[:r] || opts[:protocol] || opts[:rest] || opts[:rm] || :get
|
56
|
+
opts[:proc] ||= opts[:h] || opts[:prc] || opts[:hook] || block || Proc.new{}
|
57
|
+
opts[:path] ||= opts[:p] || opts[:method].name
|
58
|
+
opts[:args] ||= opts[:a] || opts[:arg] || {}
|
59
59
|
|
60
|
-
|
60
|
+
if opts[:args].class <= Array
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
tmp_hash = Hash.new
|
63
|
+
opts[:args].each do |array_obj|
|
64
|
+
if array_obj.size == 2 && array_obj.class <= ::Array
|
65
|
+
tmp_hash[array_obj[0]]= array_obj[1]
|
66
|
+
end
|
66
67
|
end
|
68
|
+
opts[:args]= tmp_hash
|
69
|
+
|
67
70
|
end
|
68
|
-
opts[:args]= tmp_hash
|
69
71
|
|
70
|
-
|
72
|
+
{
|
71
73
|
|
72
|
-
|
74
|
+
options: ::Hash,
|
75
|
+
rest_method: ::Symbol,
|
76
|
+
proc: ::Proc,
|
77
|
+
path: ::String,
|
78
|
+
args: ::Hash,
|
79
|
+
method: ::Method
|
73
80
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
method: Method
|
81
|
+
}.each { |key,type|
|
82
|
+
unless opts[key].class <= type
|
83
|
+
raise(ArgumentError,"invalid #{key} value, must instance of an inherited class from #{type}")
|
84
|
+
end
|
85
|
+
}
|
80
86
|
|
81
|
-
|
82
|
-
unless opts[
|
83
|
-
raise(ArgumentError,"invalid
|
87
|
+
opts[:rest_method]= opts[:rest_method].to_s.downcase.to_sym
|
88
|
+
unless [:get,:post,:put,:delete,:options].include?(opts[:rest_method])
|
89
|
+
raise(ArgumentError,"invalid rest method: #{opts[:rest_method]}")
|
84
90
|
end
|
85
|
-
}
|
86
91
|
|
87
|
-
|
92
|
+
end
|
88
93
|
|
89
|
-
|
94
|
+
# do grape command generation
|
95
|
+
begin
|
96
|
+
desc opts[:method].get_comments
|
90
97
|
|
91
|
-
|
98
|
+
params do
|
92
99
|
|
93
|
-
|
100
|
+
opts[:method].parameters.each do |array_obj|
|
94
101
|
|
95
|
-
|
102
|
+
case array_obj[0]
|
96
103
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
+
when :req
|
105
|
+
requires array_obj[1]
|
106
|
+
when :opt
|
107
|
+
optional array_obj[1]
|
108
|
+
when :rest
|
109
|
+
optional array_obj[1],
|
110
|
+
type: ::Array
|
104
111
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
112
|
+
#when :block
|
113
|
+
# optional array_obj[1],
|
114
|
+
# type: String,
|
115
|
+
# desc: "Ruby code to be used"
|
109
116
|
|
110
117
|
|
118
|
+
end
|
119
|
+
|
111
120
|
end
|
112
121
|
|
113
122
|
end
|
114
123
|
|
115
|
-
|
116
|
-
|
117
|
-
self.__send__(opts[:rest_method], opts[:path], opts[:options]) do
|
124
|
+
self.__send__(opts[:rest_method], opts[:path], opts[:options]) do
|
118
125
|
|
119
|
-
|
120
|
-
|
126
|
+
opts[:proc].call_with_binding self.binding?
|
127
|
+
opts[:method].call(
|
121
128
|
|
122
|
-
|
129
|
+
*opts[:method].parameters.map { |element|
|
123
130
|
|
124
|
-
|
131
|
+
unless params[element[1]].nil?
|
125
132
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
133
|
+
# parse if requested
|
134
|
+
case opts[:args][element[1]].to_s
|
135
|
+
when 'json'
|
136
|
+
params[element[1]]= JSON.parse(params[element[1]])
|
130
137
|
|
131
|
-
|
132
|
-
|
138
|
+
when 'yaml', 'yml'
|
139
|
+
params[element[1]]= YAML.parse(params[element[1]])
|
133
140
|
|
134
|
-
|
141
|
+
end
|
135
142
|
|
136
|
-
|
137
|
-
|
143
|
+
# add new element
|
144
|
+
params[element[1]]
|
138
145
|
|
139
|
-
|
146
|
+
end
|
140
147
|
|
141
|
-
|
148
|
+
}.compact
|
142
149
|
|
143
|
-
|
150
|
+
)
|
144
151
|
|
152
|
+
end
|
145
153
|
end
|
146
154
|
|
155
|
+
return nil
|
147
156
|
|
148
157
|
end
|
149
158
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-dsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mpatch
|