haveapi 0.29.7 → 0.29.9
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/.gitignore +1 -0
- data/Rakefile +19 -0
- data/doc/create-client.md +113 -0
- data/doc/json-schema.html +1182 -0
- data/doc/protocol.md +570 -0
- data/doc/protocol.png +0 -0
- data/doc/typed-input-validation.md +113 -0
- data/haveapi.gemspec +1 -1
- data/lib/haveapi/client_examples/php_client.rb +14 -8
- data/lib/haveapi/server.rb +6 -1
- data/lib/haveapi/version.rb +1 -1
- data/spec/client_examples/syntax_spec.rb +16 -4
- data/spec/documentation_spec.rb +14 -0
- data/spec/extensions/exception_mailer_spec.rb +33 -0
- data/spec/packaging_spec.rb +124 -0
- metadata +9 -3
|
@@ -148,20 +148,26 @@ module HaveAPI::ClientExamples
|
|
|
148
148
|
|
|
149
149
|
def format_parameters(dir, params, prefix = '')
|
|
150
150
|
ret = params.map do |k, v|
|
|
151
|
-
|
|
152
|
-
"#{prefix} \"#{k}\" => custom type}"
|
|
153
|
-
else
|
|
154
|
-
"#{prefix} \"#{k}\" => #{value(v)}"
|
|
155
|
-
end
|
|
151
|
+
"#{prefix} #{value(k.to_s)} => #{value(v)}"
|
|
156
152
|
end
|
|
157
153
|
|
|
158
154
|
"#{prefix}[\n#{ret.join(",\n")}\n#{prefix}]"
|
|
159
155
|
end
|
|
160
156
|
|
|
161
157
|
def value(v)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
158
|
+
case v
|
|
159
|
+
when ::NilClass
|
|
160
|
+
'null'
|
|
161
|
+
when ::Numeric, true, false
|
|
162
|
+
v.to_s
|
|
163
|
+
when ::Array
|
|
164
|
+
"[#{v.map { |item| value(item) }.join(', ')}]"
|
|
165
|
+
when ::Hash
|
|
166
|
+
"[#{v.map { |key, item| "#{value(key.to_s)} => #{value(item)}" }.join(', ')}]"
|
|
167
|
+
else
|
|
168
|
+
escaped = v.to_s.gsub(/[\\']/) { |char| "\\#{char}" }
|
|
169
|
+
"'#{escaped}'"
|
|
170
|
+
end
|
|
165
171
|
end
|
|
166
172
|
end
|
|
167
173
|
end
|
data/lib/haveapi/server.rb
CHANGED
|
@@ -533,7 +533,12 @@ module HaveAPI
|
|
|
533
533
|
@sinatra.get "#{@root}doc/json-schema" do
|
|
534
534
|
content_type 'text/html'
|
|
535
535
|
erb :doc_layout, layout: :main_layout do
|
|
536
|
-
|
|
536
|
+
begin
|
|
537
|
+
@content = File.read(File.join(settings.root, '../../doc/json-schema.html'))
|
|
538
|
+
rescue Errno::ENOENT
|
|
539
|
+
halt 404
|
|
540
|
+
end
|
|
541
|
+
|
|
537
542
|
@sidebar = erb :'doc_sidebars/json-schema'
|
|
538
543
|
end
|
|
539
544
|
end
|
data/lib/haveapi/version.rb
CHANGED
|
@@ -20,7 +20,8 @@ describe HaveAPI::ClientExamples do
|
|
|
20
20
|
namespace: :widget,
|
|
21
21
|
parameters: {
|
|
22
22
|
name: { type: 'String' },
|
|
23
|
-
enabled: { type: 'Boolean' }
|
|
23
|
+
enabled: { type: 'Boolean' },
|
|
24
|
+
group_by: { type: 'Custom' }
|
|
24
25
|
}
|
|
25
26
|
},
|
|
26
27
|
output: {
|
|
@@ -36,7 +37,11 @@ describe HaveAPI::ClientExamples do
|
|
|
36
37
|
let(:sample) do
|
|
37
38
|
{
|
|
38
39
|
path_params: [101],
|
|
39
|
-
request: {
|
|
40
|
+
request: {
|
|
41
|
+
name: "documented \"widget\" with $value and an apostrophe's backslash \\",
|
|
42
|
+
enabled: true,
|
|
43
|
+
group_by: ['vps_id', "owner's $group\\path"]
|
|
44
|
+
},
|
|
40
45
|
response: { id: 101, name: 'documented widget' },
|
|
41
46
|
status: true,
|
|
42
47
|
message: nil,
|
|
@@ -77,6 +82,12 @@ describe HaveAPI::ClientExamples do
|
|
|
77
82
|
status, output = syntax_check(%w[php -l], "<?php\n#{source}", '.php')
|
|
78
83
|
|
|
79
84
|
expect(status).to be_success, output
|
|
85
|
+
expect(source).to include(
|
|
86
|
+
"'group_by' => ['vps_id', 'owner\\'s $group\\\\path']"
|
|
87
|
+
)
|
|
88
|
+
expect(source).to include(
|
|
89
|
+
"'name' => 'documented \"widget\" with $value and an apostrophe\\'s backslash \\\\'"
|
|
90
|
+
)
|
|
80
91
|
end
|
|
81
92
|
|
|
82
93
|
it 'generates valid Ruby' do
|
|
@@ -102,8 +113,9 @@ describe HaveAPI::ClientExamples do
|
|
|
102
113
|
expect(request_lines).to include('Host: api.example.test', 'Content-Type: application/json')
|
|
103
114
|
expect(JSON.parse(request_body)).to eq(
|
|
104
115
|
'widget' => {
|
|
105
|
-
'name' =>
|
|
106
|
-
'enabled' => true
|
|
116
|
+
'name' => "documented \"widget\" with $value and an apostrophe's backslash \\",
|
|
117
|
+
'enabled' => true,
|
|
118
|
+
'group_by' => ['vps_id', "owner's $group\\path"]
|
|
107
119
|
}
|
|
108
120
|
)
|
|
109
121
|
|
data/spec/documentation_spec.rb
CHANGED
|
@@ -195,4 +195,18 @@ describe 'Documentation' do
|
|
|
195
195
|
expect(last_response.status).to eq(200)
|
|
196
196
|
expect(last_response.headers['Content-Type']).to include('text/html')
|
|
197
197
|
end
|
|
198
|
+
|
|
199
|
+
{
|
|
200
|
+
'/doc/json-schema' => 'Envelope',
|
|
201
|
+
'/doc/protocol.md' => 'Protocol definition',
|
|
202
|
+
'/doc/create-client.md' => 'Client definition'
|
|
203
|
+
}.each do |path, heading|
|
|
204
|
+
it "serves shared documentation at #{path}" do
|
|
205
|
+
get path
|
|
206
|
+
|
|
207
|
+
expect(last_response.status).to eq(200)
|
|
208
|
+
expect(last_response.headers['Content-Type']).to include('text/html')
|
|
209
|
+
expect(last_response.body).to include(heading)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
198
212
|
end
|
|
@@ -122,6 +122,39 @@ describe HaveAPI::Extensions::ExceptionMailer do
|
|
|
122
122
|
expect(body).to include('request boom')
|
|
123
123
|
end
|
|
124
124
|
|
|
125
|
+
it 'does not mail a missing JSON schema documentation file' do
|
|
126
|
+
mailer = new_mailer
|
|
127
|
+
calls = collect_mail_calls(mailer)
|
|
128
|
+
doc_path = File.join(app.settings.root, '../../doc/json-schema.html')
|
|
129
|
+
allow(File).to receive(:read).and_call_original
|
|
130
|
+
allow(File).to receive(:read).with(doc_path).and_raise(Errno::ENOENT, doc_path)
|
|
131
|
+
|
|
132
|
+
with_exception_mailer(mailer) do
|
|
133
|
+
get '/doc/json-schema'
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
expect(last_response.status).to eq(404)
|
|
137
|
+
expect(calls).to be_empty
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it 'still mails other JSON schema file errors' do
|
|
141
|
+
mailer = new_mailer
|
|
142
|
+
calls = collect_mail_calls(mailer)
|
|
143
|
+
app.set :raise_errors, false
|
|
144
|
+
app.set :show_exceptions, false
|
|
145
|
+
doc_path = File.join(app.settings.root, '../../doc/json-schema.html')
|
|
146
|
+
allow(File).to receive(:read).and_call_original
|
|
147
|
+
allow(File).to receive(:read).with(doc_path).and_raise(Errno::EACCES, doc_path)
|
|
148
|
+
|
|
149
|
+
with_exception_mailer(mailer) do
|
|
150
|
+
get '/doc/json-schema'
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
expect(last_response.status).to eq(500)
|
|
154
|
+
expect(calls.size).to eq(1)
|
|
155
|
+
expect(calls.first[1]).to be_a(Errno::EACCES)
|
|
156
|
+
end
|
|
157
|
+
|
|
125
158
|
it 'redacts secrets from request-level exception emails' do
|
|
126
159
|
mailer = new_mailer
|
|
127
160
|
calls = collect_mail_calls(mailer)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'open3'
|
|
5
|
+
require 'rake'
|
|
6
|
+
require 'rbconfig'
|
|
7
|
+
require 'rubygems/package'
|
|
8
|
+
require 'tmpdir'
|
|
9
|
+
|
|
10
|
+
describe HaveAPI::Server do
|
|
11
|
+
def required_docs
|
|
12
|
+
%w[json-schema.html protocol.md create-client.md typed-input-validation.md protocol.png]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
before(:context) do
|
|
16
|
+
load File.expand_path('../Rakefile', __dir__)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def invoke_build
|
|
20
|
+
Rake::Task[:check_doc].reenable
|
|
21
|
+
Rake::Task[:build].reenable
|
|
22
|
+
Rake::Task[:build].invoke
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
%i[missing empty].each do |condition|
|
|
26
|
+
it "rejects a direct build with #{condition} shared documentation" do
|
|
27
|
+
Dir.mktmpdir('haveapi-doc-check') do |dir|
|
|
28
|
+
FileUtils.mkdir_p(File.join(dir, 'doc'))
|
|
29
|
+
required_docs.each do |name|
|
|
30
|
+
next if name == 'json-schema.html' && condition == :missing
|
|
31
|
+
|
|
32
|
+
content = name == 'json-schema.html' && condition == :empty ? '' : 'document'
|
|
33
|
+
File.write(File.join(dir, 'doc', name), content)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Dir.chdir(dir) do
|
|
37
|
+
expect do
|
|
38
|
+
expect { invoke_build }.to raise_error(SystemExit) do |error|
|
|
39
|
+
expect(error.status).to eq(1)
|
|
40
|
+
end
|
|
41
|
+
end.to output(/json-schema.html.*Run `make doc`/).to_stderr
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'ships and serves shared documentation from the built gem' do
|
|
48
|
+
invoke_build
|
|
49
|
+
gem_path = File.expand_path("../pkg/haveapi-#{HaveAPI::VERSION}.gem", __dir__)
|
|
50
|
+
|
|
51
|
+
Dir.mktmpdir('haveapi-package') do |dir|
|
|
52
|
+
Gem::Package.new(gem_path).extract_files(dir)
|
|
53
|
+
required_docs.each do |name|
|
|
54
|
+
expect(File.read(File.join(dir, 'doc', name))).not_to be_empty
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
script = <<~RUBY
|
|
58
|
+
require 'haveapi'
|
|
59
|
+
require 'rack/mock'
|
|
60
|
+
|
|
61
|
+
module PackagedAPI
|
|
62
|
+
class Test < HaveAPI::Resource
|
|
63
|
+
version 1
|
|
64
|
+
auth false
|
|
65
|
+
|
|
66
|
+
class Index < HaveAPI::Actions::Default::Index
|
|
67
|
+
authorize { allow }
|
|
68
|
+
def exec
|
|
69
|
+
[]
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
server = HaveAPI::Server.new(PackagedAPI)
|
|
76
|
+
server.use_version(1)
|
|
77
|
+
server.mount('/')
|
|
78
|
+
request = Rack::MockRequest.new(server.app)
|
|
79
|
+
responses = %w[/doc/json-schema /doc/protocol.md /doc/create-client.md].to_h do |path|
|
|
80
|
+
response = request.get(path, 'HTTP_HOST' => 'localhost')
|
|
81
|
+
[path, { status: response.status, type: response.content_type, body: response.body }]
|
|
82
|
+
end
|
|
83
|
+
puts JSON.generate({
|
|
84
|
+
framework: HaveAPI::Server.instance_method(:mount).source_location.first,
|
|
85
|
+
responses: responses
|
|
86
|
+
})
|
|
87
|
+
RUBY
|
|
88
|
+
|
|
89
|
+
empty_gem_home = File.join(dir, 'empty-gems')
|
|
90
|
+
original_env = Bundler.original_env.merge('GEM_HOME' => empty_gem_home, 'GEM_PATH' => empty_gem_home)
|
|
91
|
+
allow(Bundler).to receive(:original_env).and_return(original_env)
|
|
92
|
+
|
|
93
|
+
# Retain Bundler's resolved gem paths for installations such as vendor/bundle.
|
|
94
|
+
child_env = {
|
|
95
|
+
'GEM_HOME' => Gem.dir,
|
|
96
|
+
'GEM_PATH' => Gem.path.join(File::PATH_SEPARATOR),
|
|
97
|
+
'RUBYLIB' => nil,
|
|
98
|
+
'RUBYOPT' => nil
|
|
99
|
+
}
|
|
100
|
+
stdout, stderr, status = Bundler.with_unbundled_env do
|
|
101
|
+
Open3.capture3(
|
|
102
|
+
child_env,
|
|
103
|
+
RbConfig.ruby, '-I', File.join(dir, 'lib'), '-e', script,
|
|
104
|
+
'-I', File.join(Gem.loaded_specs.fetch('haveapi-client').full_gem_path, 'lib'),
|
|
105
|
+
chdir: dir
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
expect(status.success?).to be(true), "Packaged server failed: #{stderr}"
|
|
109
|
+
result = JSON.parse(stdout)
|
|
110
|
+
expect(File.realpath(result.fetch('framework'))).to eq(File.join(dir, 'lib/haveapi/server.rb'))
|
|
111
|
+
|
|
112
|
+
{
|
|
113
|
+
'/doc/json-schema' => 'Envelope',
|
|
114
|
+
'/doc/protocol.md' => 'Protocol definition',
|
|
115
|
+
'/doc/create-client.md' => 'Client definition'
|
|
116
|
+
}.each do |path, heading|
|
|
117
|
+
response = result.fetch('responses').fetch(path)
|
|
118
|
+
expect(response.fetch('status')).to eq(200), "Packaged route failed: #{path}"
|
|
119
|
+
expect(response.fetch('type')).to include('text/html')
|
|
120
|
+
expect(response.fetch('body')).to include(heading)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: haveapi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.29.
|
|
4
|
+
version: 0.29.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jakub Skokan
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - "~>"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 0.29.
|
|
32
|
+
version: 0.29.9
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 0.29.
|
|
39
|
+
version: 0.29.9
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: i18n
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -210,8 +210,13 @@ files:
|
|
|
210
210
|
- LICENSE.txt
|
|
211
211
|
- README.md
|
|
212
212
|
- Rakefile
|
|
213
|
+
- doc/create-client.md
|
|
213
214
|
- doc/hooks.erb
|
|
214
215
|
- doc/index.md
|
|
216
|
+
- doc/json-schema.html
|
|
217
|
+
- doc/protocol.md
|
|
218
|
+
- doc/protocol.png
|
|
219
|
+
- doc/typed-input-validation.md
|
|
215
220
|
- haveapi.gemspec
|
|
216
221
|
- lib/haveapi.rb
|
|
217
222
|
- lib/haveapi/action.rb
|
|
@@ -338,6 +343,7 @@ files:
|
|
|
338
343
|
- spec/hooks_spec.rb
|
|
339
344
|
- spec/i18n_spec.rb
|
|
340
345
|
- spec/model_adapters/active_record_spec.rb
|
|
346
|
+
- spec/packaging_spec.rb
|
|
341
347
|
- spec/parameters/typed_spec.rb
|
|
342
348
|
- spec/params_spec.rb
|
|
343
349
|
- spec/resource_spec.rb
|