grape-dsl 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7fd4082602f01e51467ed2c8140655173728963
4
- data.tar.gz: 2f7021dfcfb5c9ca5c62f7c7b0e8fd5e52a8cea0
3
+ metadata.gz: 08ef9da27a9c2af48368055da13388a3cf91c1c7
4
+ data.tar.gz: ad675add2489ab8ead52b5b1bbaa930610f49da2
5
5
  SHA512:
6
- metadata.gz: e10239cd86baaa86728eb9bf0dc68fd7d01d1dca05905a1b196a7e83c65014a6f216018251255170342b67cf177e2785763ded55807b5e678d240f41bb1b1ff6
7
- data.tar.gz: ef7859cc6a82767ec9b0246b59f432bbd6d4dd5a06e93c217418ed46f94d29b33a5ca44864529dffe6d16d34f462f3f74a63f0959ffe7067d8c10b1b18e343e9
6
+ metadata.gz: 568a7b360a75a70d099538af348253afff6347b2cde05aedb77b1ddd9b6370f7bedd0ac0103afbc3440a5ea43686c6f3d21774c5dda905c0e798fea7d3a146cb
7
+ data.tar.gz: 93d78d9beabcf5bf9e2e6f34a1ff9fce6858733c29378332ee3ca6fc201752e841f773557d1d75afa56789b842b6b5fee3f4ff8c2fb2ac9d0a3205db6ab93091
data/README.md CHANGED
@@ -92,6 +92,11 @@ In the default usecase, the mount process will read the method source for docume
92
92
  path: "hello_world",
93
93
  args: [[:opts,:yaml],[:args,:json]]
94
94
 
95
+ #> or
96
+
97
+ mount_method Test.method(:test),
98
+ args: [[:opts,:yaml],[:args,:json]]
99
+
95
100
 
96
101
  end
97
102
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
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
- def generate_ip_regexp_collection *args
5
+ module Helpers
6
+ class << self
6
7
 
7
- if args.empty?
8
- raise ArgumentError, "missing ip(s) for allowed sources"
9
- end
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
- @cached_regexp_collection ||= {}
12
- if @cached_regexp_collection[args].nil?
13
- @cached_regexp_collection= {}
18
+ ip_regex_collection= []
19
+ args.each do |ip_addr|
14
20
 
15
- ip_regex_collection= []
16
- args.each do |ip_addr|
21
+ ip_regex_builder= [[],[],[],[]]
17
22
 
18
- ip_regex_builder= [[],[],[],[]]
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
- #ip_addr.to_s.check(/([0-9\*]{1,3}\.){3}([0-9\*]{1,3})/)#(/([0-9\*]{1,3}\.){3}(0|\*)$/)
21
- if (ip_addr.to_s =~ /([0-9\*]{1,3}\.){3}([0-9\*]{1,3})/).nil? ? false : true
26
+ ip_addr_index= 0
27
+ ip_addr.split('.').each do |ip_addr_part|
22
28
 
23
- ip_addr_index= 0
24
- ip_addr.split('.').each do |ip_addr_part|
29
+ # 0.0.0.0
30
+ # 255.255.255.255
25
31
 
26
- # 0.0.0.0
27
- # 255.255.255.255
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
- ip_regex_builder[ip_addr_index].push(ip_addr_part)
44
+ next
33
45
  end
34
46
 
35
- # increment index
36
- ip_addr_index += 1
47
+ ip_regex_builder.map! do |element|
37
48
 
38
- end
49
+ case true
39
50
 
40
- else
41
- next
42
- end
51
+ when element.class <= Regexp
52
+ element.inspect[1..(element.inspect.length-2)]
43
53
 
44
- ip_regex_builder.map! do |element|
54
+ when element.class <= String
55
+ element
45
56
 
46
- case true
57
+ when element.class <= Array
58
+ "(#{element.join('|')})"
47
59
 
48
- when element.class <= Regexp
49
- element.inspect[1..(element.inspect.length-2)]
60
+ else
61
+ element.to_s
50
62
 
51
- when element.class <= String
52
- element
63
+ end
53
64
 
54
- when element.class <= Array
55
- "(#{element.join('|')})"
65
+ end
56
66
 
57
- else
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
- ip_regex_collection.push /#{ip_regex_builder.join('\.')}/
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 <= String
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
- unless desc.class <= Hash
20
- desc ::Hashie::Mash.new
21
- end
42
+ var= ::GrapeDSL::Extend::APIMNT::Description.new(*args)
22
43
 
23
- unless self.content_types.keys.empty?
44
+ unless self.content_types.keys.empty?
24
45
 
25
- content_type_name= nil
26
- [:json,:xml,:txt].each do |element|
27
- if self.content_types.keys.include? element
28
- content_type_name ||= element.to_s.upcase
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
- desc[:convent_type] ||= content_type_name
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 opts= {}, &block
37
+ def mount_method *args, &block
38
38
 
39
- unless opts.class <= Hash
40
- raise ArgumentError, "invalid input, must be Hash like obj"
41
- end
39
+ # process params && validations
40
+ begin
42
41
 
43
- # required
44
- opts[:method] ||= opts[:m] || raise(ArgumentError,"missing method input(:method)")
42
+ opts= args.select{|e|(e.class <= ::Hash)}.reduce( {}, :merge! )
45
43
 
46
- # optional
47
- opts[:options] ||= opts[:o] || {}
48
- opts[:rest_method] ||= opts[:r] || opts[:protocol] || opts[:rest] || opts[:rm] || :get
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
- if opts[:method].class <= String || opts[:method].class <= Symbol
48
+ opts[:class] ||= opts[:c] || opts[:module] || raise(ArgumentError,"missing method input(:method)")
49
+ opts[:method] = opts[:class].method(opts[:method])
54
50
 
55
- opts[:class] ||= opts[:c] || opts[:module] || raise(ArgumentError,"missing method input(:method)")
56
- opts[:method] = opts[:class].method(opts[:method])
51
+ end
57
52
 
58
- end
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
- if opts[:args].class <= Array
60
+ if opts[:args].class <= Array
61
61
 
62
- tmp_hash = Hash.new
63
- opts[:args].each do |array_obj|
64
- if array_obj.count == 2
65
- tmp_hash[array_obj[0]]= array_obj[1]
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
- end
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
- options: Hash,
75
- rest_method: Symbol,
76
- proc: Proc,
77
- path: String,
78
- args: Hash,
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
- }.each { |key,type|
82
- unless opts[key].class <= type
83
- raise(ArgumentError,"invalid #{key} value, must instance of an inherited class from #{type}")
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
- opts[:rest_method] = opts[:rest_method].to_s.downcase.to_sym
92
+ end
88
93
 
89
- desc opts[:method].get_comments
94
+ # do grape command generation
95
+ begin
96
+ desc opts[:method].get_comments
90
97
 
91
- params do
98
+ params do
92
99
 
93
- opts[:method].parameters.each do |array_obj|
100
+ opts[:method].parameters.each do |array_obj|
94
101
 
95
- case array_obj[0]
102
+ case array_obj[0]
96
103
 
97
- when :req
98
- requires array_obj[1]
99
- when :opt
100
- optional array_obj[1]
101
- when :rest
102
- optional array_obj[1],
103
- type: Array
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
- #when :block
106
- # optional array_obj[1],
107
- # type: String,
108
- # desc: "Ruby code to be used"
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
- end
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
- opts[:proc].call_with_binding self.binding?
120
- opts[:method].call(
126
+ opts[:proc].call_with_binding self.binding?
127
+ opts[:method].call(
121
128
 
122
- *opts[:method].parameters.map { |element|
129
+ *opts[:method].parameters.map { |element|
123
130
 
124
- unless params[element[1]].nil?
131
+ unless params[element[1]].nil?
125
132
 
126
- # parse if requested
127
- case opts[:args][element[1]].to_s
128
- when 'json'
129
- params[element[1]]= JSON.parse(params[element[1]])
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
- when 'yaml', 'yml'
132
- params[element[1]]= YAML.parse(params[element[1]])
138
+ when 'yaml', 'yml'
139
+ params[element[1]]= YAML.parse(params[element[1]])
133
140
 
134
- end
141
+ end
135
142
 
136
- # add new element
137
- params[element[1]]
143
+ # add new element
144
+ params[element[1]]
138
145
 
139
- end
146
+ end
140
147
 
141
- }.compact
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.1
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-04-15 00:00:00.000000000 Z
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mpatch