haveapi 0.11.1 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG +2 -0
- data/Gemfile +2 -0
- data/haveapi.gemspec +5 -5
- data/lib/haveapi/action.rb +6 -5
- data/lib/haveapi/actions/default.rb +3 -0
- data/lib/haveapi/authentication/basic/provider.rb +2 -0
- data/lib/haveapi/authentication/token/provider.rb +2 -0
- data/lib/haveapi/authentication/token/resources.rb +3 -0
- data/lib/haveapi/client_examples/curl.rb +1 -0
- data/lib/haveapi/client_examples/fs_client.rb +2 -0
- data/lib/haveapi/client_examples/http.rb +1 -0
- data/lib/haveapi/client_examples/js_client.rb +2 -0
- data/lib/haveapi/client_examples/php_client.rb +2 -0
- data/lib/haveapi/client_examples/ruby_cli.rb +1 -0
- data/lib/haveapi/client_examples/ruby_client.rb +1 -0
- data/lib/haveapi/example.rb +56 -3
- data/lib/haveapi/example_list.rb +28 -0
- data/lib/haveapi/extensions/action_exceptions.rb +2 -0
- data/lib/haveapi/extensions/exception_mailer.rb +1 -0
- data/lib/haveapi/model_adapters/active_record.rb +2 -0
- data/lib/haveapi/model_adapters/hash.rb +2 -0
- data/lib/haveapi/output_formatters/json.rb +2 -0
- data/lib/haveapi/resource.rb +2 -0
- data/lib/haveapi/resources/action_state.rb +2 -0
- data/lib/haveapi/server.rb +7 -1
- data/lib/haveapi/validators/acceptance.rb +2 -0
- data/lib/haveapi/validators/confirmation.rb +2 -0
- data/lib/haveapi/validators/custom.rb +2 -0
- data/lib/haveapi/validators/exclusion.rb +2 -0
- data/lib/haveapi/validators/format.rb +2 -0
- data/lib/haveapi/validators/inclusion.rb +2 -0
- data/lib/haveapi/validators/length.rb +2 -0
- data/lib/haveapi/validators/numericality.rb +7 -5
- data/lib/haveapi/validators/presence.rb +2 -0
- data/lib/haveapi/version.rb +1 -1
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cdbc87bdee886f2d6c0063cb3cb4ce06bb42431508ba88ae9de7375432c642bb
|
4
|
+
data.tar.gz: ca75c1bbaed366ef28c8605d6c1f80c204066cdb7dc09a2e44e4e0a08bb0b52b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c41b7a81996a2cde3e59b813e9d59d0494338f17f1a67319da23c9aa801c4e79459af8d62c5ac1f76796efd9168c4640e397cc25385e2bcd673ef53a931593c2
|
7
|
+
data.tar.gz: b8a884d61fbe5a2b683f557de99d1c0c6514e16db2666c010e0e9a110a1c41ed0787cee151dbdf1ab939c94d19cde183c390bc4a98aa6fd5d781880fb82c910d
|
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
data/haveapi.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'haveapi/version'
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = 'haveapi'
|
7
7
|
s.version = HaveAPI::VERSION
|
8
|
-
s.date = '
|
8
|
+
s.date = '2017-11-27'
|
9
9
|
s.summary =
|
10
10
|
s.description = 'Framework for creating self-describing APIs'
|
11
11
|
s.authors = 'Jakub Skokan'
|
@@ -15,15 +15,15 @@ Gem::Specification.new do |s|
|
|
15
15
|
|
16
16
|
s.required_ruby_version = '>= 2.0.0'
|
17
17
|
|
18
|
-
s.add_runtime_dependency 'require_all', '~>
|
18
|
+
s.add_runtime_dependency 'require_all', '~> 2.0.0'
|
19
19
|
s.add_runtime_dependency 'json'
|
20
20
|
s.add_runtime_dependency 'activesupport', '>= 4.0'
|
21
|
-
s.add_runtime_dependency 'sinatra', '~>
|
22
|
-
s.add_runtime_dependency 'tilt', '~>
|
21
|
+
s.add_runtime_dependency 'sinatra', '~> 2.0.5'
|
22
|
+
s.add_runtime_dependency 'tilt', '~> 2.0.9'
|
23
23
|
s.add_runtime_dependency 'redcarpet', '~> 3.4'
|
24
24
|
s.add_runtime_dependency 'rake'
|
25
25
|
s.add_runtime_dependency 'github-markdown'
|
26
26
|
s.add_runtime_dependency 'nesty', '~> 1.0'
|
27
|
-
s.add_runtime_dependency 'haveapi-client', '~> 0.
|
27
|
+
s.add_runtime_dependency 'haveapi-client', '~> 0.12.0'
|
28
28
|
s.add_runtime_dependency 'mail'
|
29
29
|
end
|
data/lib/haveapi/action.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'haveapi/common'
|
2
|
+
require 'haveapi/hooks'
|
3
|
+
require 'haveapi/metadata'
|
4
|
+
|
1
5
|
module HaveAPI
|
2
6
|
class Action < Common
|
3
7
|
obj_type :action
|
@@ -167,7 +171,7 @@ module HaveAPI
|
|
167
171
|
end
|
168
172
|
|
169
173
|
def example(title = '', &block)
|
170
|
-
@examples ||=
|
174
|
+
@examples ||= ExampleList.new
|
171
175
|
e = Example.new(title)
|
172
176
|
e.instance_eval(&block)
|
173
177
|
@examples << e
|
@@ -209,7 +213,7 @@ module HaveAPI
|
|
209
213
|
input: @input ? @input.describe(context) : {parameters: {}},
|
210
214
|
output: @output ? @output.describe(context) : {parameters: {}},
|
211
215
|
meta: @meta ? @meta.merge(@meta) { |_, v| v && v.describe(context) } : nil,
|
212
|
-
examples: @examples ? @examples.
|
216
|
+
examples: @examples ? @examples.describe(context) : [],
|
213
217
|
url: context.resolved_url,
|
214
218
|
method: route_method,
|
215
219
|
help: "#{context.url}?method=#{route_method}"
|
@@ -547,9 +551,6 @@ module HaveAPI
|
|
547
551
|
self.class.model_adapter(self.class.input.layout).input(@safe_params[input.namespace]))
|
548
552
|
end
|
549
553
|
|
550
|
-
# Remove duplicit key
|
551
|
-
@safe_params.delete(input.namespace.to_s)
|
552
|
-
|
553
554
|
# Now check required params, convert types and set defaults
|
554
555
|
input.validate(@safe_params)
|
555
556
|
end
|
data/lib/haveapi/example.rb
CHANGED
@@ -4,6 +4,10 @@ module HaveAPI
|
|
4
4
|
@title = title
|
5
5
|
end
|
6
6
|
|
7
|
+
def authorize(&block)
|
8
|
+
@authorization = block
|
9
|
+
end
|
10
|
+
|
7
11
|
def url_params(*params)
|
8
12
|
@url_params = params
|
9
13
|
end
|
@@ -36,20 +40,29 @@ module HaveAPI
|
|
36
40
|
@comment = str
|
37
41
|
end
|
38
42
|
|
43
|
+
def authorized?(context)
|
44
|
+
if (context.endpoint || context.current_user) \
|
45
|
+
&& @authorization && !@authorization.call(context.current_user)
|
46
|
+
false
|
47
|
+
else
|
48
|
+
true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
39
52
|
def provided?
|
40
53
|
instance_variables.detect do |v|
|
41
54
|
instance_variable_get(v)
|
42
55
|
end ? true : false
|
43
56
|
end
|
44
57
|
|
45
|
-
def describe
|
58
|
+
def describe(context)
|
46
59
|
if provided?
|
47
60
|
{
|
48
61
|
title: @title,
|
49
62
|
comment: @comment,
|
50
63
|
url_params: @url_params,
|
51
|
-
request: @request,
|
52
|
-
response: @response,
|
64
|
+
request: filter_input_params(context, @request),
|
65
|
+
response: filter_output_params(context, @response),
|
53
66
|
status: @status.nil? ? true : @status,
|
54
67
|
message: @message,
|
55
68
|
errors: @errors,
|
@@ -59,5 +72,45 @@ module HaveAPI
|
|
59
72
|
{}
|
60
73
|
end
|
61
74
|
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
def filter_input_params(context, input)
|
78
|
+
case context.action.input.layout
|
79
|
+
when :object, :hash
|
80
|
+
context.authorization.filter_input(
|
81
|
+
context.action.input.params,
|
82
|
+
ModelAdapters::Hash.output(context, input),
|
83
|
+
)
|
84
|
+
|
85
|
+
when :object_list, :hash_list
|
86
|
+
input.map do |obj|
|
87
|
+
context.authorization.filter_input(
|
88
|
+
context.action.input.params,
|
89
|
+
ModelAdapters::Hash.output(context, obj),
|
90
|
+
true
|
91
|
+
)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def filter_output_params(context, output)
|
97
|
+
case context.action.output.layout
|
98
|
+
when :object, :hash
|
99
|
+
context.authorization.filter_output(
|
100
|
+
context.action.output.params,
|
101
|
+
ModelAdapters::Hash.output(context, output),
|
102
|
+
true
|
103
|
+
)
|
104
|
+
|
105
|
+
when :object_list, :hash_list
|
106
|
+
output.map do |obj|
|
107
|
+
context.authorization.filter_output(
|
108
|
+
context.action.output.params,
|
109
|
+
ModelAdapters::Hash.output(context, obj),
|
110
|
+
true
|
111
|
+
)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
62
115
|
end
|
63
116
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module HaveAPI
|
2
|
+
class ExampleList
|
3
|
+
def initialize
|
4
|
+
@examples = []
|
5
|
+
end
|
6
|
+
|
7
|
+
# @param example [Example]
|
8
|
+
def <<(example)
|
9
|
+
@examples << example
|
10
|
+
end
|
11
|
+
|
12
|
+
def describe(context)
|
13
|
+
ret = []
|
14
|
+
|
15
|
+
@examples.each do |e|
|
16
|
+
ret << e.describe(context) if e.authorized?(context)
|
17
|
+
end
|
18
|
+
|
19
|
+
ret
|
20
|
+
end
|
21
|
+
|
22
|
+
def each(&block)
|
23
|
+
@examples.each(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
include Enumerable
|
27
|
+
end
|
28
|
+
end
|
data/lib/haveapi/resource.rb
CHANGED
data/lib/haveapi/server.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'erb'
|
2
2
|
require 'redcarpet'
|
3
|
+
require 'tilt'
|
3
4
|
require 'cgi'
|
5
|
+
require 'haveapi/hooks'
|
4
6
|
|
5
7
|
module HaveAPI
|
6
8
|
class Server
|
@@ -159,6 +161,10 @@ module HaveAPI
|
|
159
161
|
@root = prefix
|
160
162
|
|
161
163
|
@sinatra = Sinatra.new do
|
164
|
+
# Preload template engine for .md -- without this, tilt will not search
|
165
|
+
# for markdown files with extension .md, only .markdown
|
166
|
+
Tilt[:md]
|
167
|
+
|
162
168
|
set :views, settings.root + '/views'
|
163
169
|
set :public_folder, settings.root + '/public'
|
164
170
|
set :bind, '0.0.0.0'
|
@@ -269,7 +275,7 @@ module HaveAPI
|
|
269
275
|
end
|
270
276
|
end
|
271
277
|
|
272
|
-
@sinatra.get %r{#{@root}doc/([^\.]+)
|
278
|
+
@sinatra.get %r{#{@root}doc/([^\.]+)(\.md)?} do |f, _|
|
273
279
|
content_type 'text/html'
|
274
280
|
erb :doc_layout, layout: :main_layout do
|
275
281
|
begin
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'haveapi/validator'
|
2
|
+
|
1
3
|
module HaveAPI
|
2
4
|
# Checks the value is a number or a string containing only digits.
|
3
5
|
#
|
@@ -8,7 +10,7 @@ module HaveAPI
|
|
8
10
|
# step: 2,
|
9
11
|
# message: 'the error message'
|
10
12
|
# }
|
11
|
-
#
|
13
|
+
#
|
12
14
|
# Will allow values +3+, +5+, +7+ and +9+.
|
13
15
|
#
|
14
16
|
# string :param, number: {
|
@@ -16,7 +18,7 @@ module HaveAPI
|
|
16
18
|
# max: 10,
|
17
19
|
# mod: 2,
|
18
20
|
# }
|
19
|
-
#
|
21
|
+
#
|
20
22
|
# Will allow values +4+, +6+, +8+ and +10+.
|
21
23
|
class Validators::Numericality < Validator
|
22
24
|
name :number
|
@@ -47,17 +49,17 @@ module HaveAPI
|
|
47
49
|
msg += '; ' unless msg.empty?
|
48
50
|
msg += "in steps of #{@step}"
|
49
51
|
end
|
50
|
-
|
52
|
+
|
51
53
|
if @mod
|
52
54
|
msg += '; ' unless msg.empty?
|
53
55
|
msg += "mod #{@step} must equal zero"
|
54
56
|
end
|
55
|
-
|
57
|
+
|
56
58
|
if @odd
|
57
59
|
msg += '; ' unless msg.empty?
|
58
60
|
msg += "odd"
|
59
61
|
end
|
60
|
-
|
62
|
+
|
61
63
|
if @even
|
62
64
|
msg += '; ' unless msg.empty?
|
63
65
|
msg += "even"
|
data/lib/haveapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: haveapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakub Skokan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: require_all
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: json
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,28 +58,28 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 2.0.5
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 2.0.5
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: tilt
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 2.0.9
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 2.0.9
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: redcarpet
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,14 +142,14 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.12.0
|
146
146
|
type: :runtime
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
152
|
+
version: 0.12.0
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: mail
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -207,6 +207,7 @@ files:
|
|
207
207
|
- lib/haveapi/common.rb
|
208
208
|
- lib/haveapi/context.rb
|
209
209
|
- lib/haveapi/example.rb
|
210
|
+
- lib/haveapi/example_list.rb
|
210
211
|
- lib/haveapi/exceptions.rb
|
211
212
|
- lib/haveapi/extensions/action_exceptions.rb
|
212
213
|
- lib/haveapi/extensions/base.rb
|
@@ -303,7 +304,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
303
304
|
version: '0'
|
304
305
|
requirements: []
|
305
306
|
rubyforge_project:
|
306
|
-
rubygems_version: 2.
|
307
|
+
rubygems_version: 2.7.7
|
307
308
|
signing_key:
|
308
309
|
specification_version: 4
|
309
310
|
summary: Framework for creating self-describing APIs
|