haveapi 0.28.3 → 0.29.0

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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +139 -0
  3. data/Rakefile +1 -0
  4. data/haveapi.gemspec +2 -1
  5. data/lib/haveapi/action.rb +26 -9
  6. data/lib/haveapi/actions/default.rb +5 -2
  7. data/lib/haveapi/actions/paginable.rb +8 -4
  8. data/lib/haveapi/authentication/oauth2/provider.rb +1 -1
  9. data/lib/haveapi/authentication/token/config.rb +10 -3
  10. data/lib/haveapi/authentication/token/provider.rb +22 -21
  11. data/lib/haveapi/context.rb +4 -1
  12. data/lib/haveapi/extensions/exception_mailer.rb +136 -17
  13. data/lib/haveapi/i18n.rb +125 -0
  14. data/lib/haveapi/locales/cs.yml +167 -0
  15. data/lib/haveapi/locales/en.yml +168 -0
  16. data/lib/haveapi/metadata.rb +25 -3
  17. data/lib/haveapi/model_adapters/active_record.rb +40 -26
  18. data/lib/haveapi/output_formatter.rb +2 -2
  19. data/lib/haveapi/parameters/metadata_i18n.rb +179 -0
  20. data/lib/haveapi/parameters/resource.rb +18 -7
  21. data/lib/haveapi/parameters/typed.rb +27 -20
  22. data/lib/haveapi/params.rb +76 -7
  23. data/lib/haveapi/resource.rb +1 -1
  24. data/lib/haveapi/resources/action_state.rb +47 -27
  25. data/lib/haveapi/server.rb +287 -94
  26. data/lib/haveapi/spec/api_builder.rb +25 -0
  27. data/lib/haveapi/spec/spec_methods.rb +10 -0
  28. data/lib/haveapi/tasks/i18n.rb +198 -0
  29. data/lib/haveapi/validator_chain.rb +1 -1
  30. data/lib/haveapi/validators/acceptance.rb +5 -2
  31. data/lib/haveapi/validators/confirmation.rb +5 -2
  32. data/lib/haveapi/validators/exclusion.rb +2 -2
  33. data/lib/haveapi/validators/format.rb +5 -2
  34. data/lib/haveapi/validators/inclusion.rb +2 -2
  35. data/lib/haveapi/validators/length.rb +5 -5
  36. data/lib/haveapi/validators/numericality.rb +20 -22
  37. data/lib/haveapi/validators/presence.rb +4 -2
  38. data/lib/haveapi/version.rb +1 -1
  39. data/lib/haveapi.rb +1 -0
  40. data/spec/authentication/oauth2_spec.rb +10 -0
  41. data/spec/extensions/exception_mailer_spec.rb +195 -0
  42. data/spec/i18n_spec.rb +520 -0
  43. data/spec/params_spec.rb +183 -0
  44. data/spec/server/integration_spec.rb +34 -0
  45. metadata +30 -7
  46. data/doc/create-client.md +0 -107
  47. data/doc/json-schema.html +0 -1182
  48. data/doc/protocol.md +0 -535
  49. data/doc/protocol.png +0 -0
@@ -37,6 +37,19 @@ describe HaveAPI::Server do
37
37
  { msg: params.dig(:test, :msg) }
38
38
  end
39
39
  end
40
+
41
+ define_action(:AuthorizeError) do
42
+ route 'authorize_error'
43
+ http_method :get
44
+
45
+ authorize do
46
+ raise 'authorize boom'
47
+ end
48
+
49
+ def exec
50
+ ok!
51
+ end
52
+ end
40
53
  end
41
54
 
42
55
  define_resource(:Transfer) do
@@ -143,6 +156,27 @@ describe HaveAPI::Server do
143
156
  expect(api_response).not_to be_ok
144
157
  end
145
158
 
159
+ it 'routes request-level exceptions through hook' do
160
+ calls = []
161
+
162
+ app.settings.api_server.connect_hook(:request_exception) do |ret, context, exception|
163
+ calls << [context, exception]
164
+ ret
165
+ end
166
+
167
+ header 'Accept', 'application/json'
168
+ get '/v1/tests/authorize_error'
169
+
170
+ expect(last_response.status).to eq(500)
171
+ expect(api_response).not_to be_ok
172
+ expect(api_response.message).to eq('Server error occurred')
173
+
174
+ expect(calls.size).to eq(1)
175
+ context, exception = calls.first
176
+ expect(context.action.to_s).to include('AuthorizeError')
177
+ expect(exception.message).to eq('authorize boom')
178
+ end
179
+
146
180
  it 'handles CORS preflight OPTIONS' do
147
181
  header 'Accept', 'application/json'
148
182
  header 'Origin', 'https://example.com'
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.28.3
4
+ version: 0.29.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
@@ -29,14 +29,34 @@ dependencies:
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.28.3
32
+ version: 0.29.0
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.28.3
39
+ version: 0.29.0
40
+ - !ruby/object:Gem::Dependency
41
+ name: i18n
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '1.6'
47
+ - - "<"
48
+ - !ruby/object:Gem::Version
49
+ version: '2'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '1.6'
57
+ - - "<"
58
+ - !ruby/object:Gem::Version
59
+ version: '2'
40
60
  - !ruby/object:Gem::Dependency
41
61
  name: json
42
62
  requirement: !ruby/object:Gem::Requirement
@@ -190,12 +210,8 @@ files:
190
210
  - LICENSE.txt
191
211
  - README.md
192
212
  - Rakefile
193
- - doc/create-client.md
194
213
  - doc/hooks.erb
195
214
  - doc/index.md
196
- - doc/json-schema.html
197
- - doc/protocol.md
198
- - doc/protocol.png
199
215
  - haveapi.gemspec
200
216
  - lib/haveapi.rb
201
217
  - lib/haveapi/action.rb
@@ -234,6 +250,9 @@ files:
234
250
  - lib/haveapi/extensions/base.rb
235
251
  - lib/haveapi/extensions/exception_mailer.rb
236
252
  - lib/haveapi/hooks.rb
253
+ - lib/haveapi/i18n.rb
254
+ - lib/haveapi/locales/cs.yml
255
+ - lib/haveapi/locales/en.yml
237
256
  - lib/haveapi/metadata.rb
238
257
  - lib/haveapi/model_adapter.rb
239
258
  - lib/haveapi/model_adapters/active_record.rb
@@ -241,6 +260,7 @@ files:
241
260
  - lib/haveapi/output_formatter.rb
242
261
  - lib/haveapi/output_formatters/base.rb
243
262
  - lib/haveapi/output_formatters/json.rb
263
+ - lib/haveapi/parameters/metadata_i18n.rb
244
264
  - lib/haveapi/parameters/resource.rb
245
265
  - lib/haveapi/parameters/typed.rb
246
266
  - lib/haveapi/params.rb
@@ -264,6 +284,7 @@ files:
264
284
  - lib/haveapi/spec/mock_action.rb
265
285
  - lib/haveapi/spec/spec_methods.rb
266
286
  - lib/haveapi/tasks/hooks.rb
287
+ - lib/haveapi/tasks/i18n.rb
267
288
  - lib/haveapi/tasks/yard.rb
268
289
  - lib/haveapi/types.rb
269
290
  - lib/haveapi/validator.rb
@@ -312,7 +333,9 @@ files:
312
333
  - spec/documentation_spec.rb
313
334
  - spec/envelope_spec.rb
314
335
  - spec/extensions/action_exceptions_spec.rb
336
+ - spec/extensions/exception_mailer_spec.rb
315
337
  - spec/hooks_spec.rb
338
+ - spec/i18n_spec.rb
316
339
  - spec/model_adapters/active_record_spec.rb
317
340
  - spec/parameters/typed_spec.rb
318
341
  - spec/params_spec.rb
data/doc/create-client.md DELETED
@@ -1,107 +0,0 @@
1
- # Client definition
2
- It is necessary to differentiate between clients for HaveAPI based APIs
3
- and clients to work with your API instance. This document describes
4
- the former. If you only want to use your API, you should check a list
5
- of available clients and pick the one in the right language. Only when
6
- there isn't a client already available in the language you want, then
7
- continue reading.
8
-
9
- # Design rules
10
- The client must completely depend on the API description:
11
-
12
- - it has no assumptions and API-specific code,
13
- - it does not know any resources, actions and parameters,
14
- - everything the client knows must be found out from the API description.
15
-
16
- All clients should use a similar paradigm, so that it is possible to use
17
- clients in different languages in the same way, as far as the language syntax
18
- allows. Clients bundled with HaveAPI may serve as a model. A client should
19
- use all the advantages and coding styles of the language it is written
20
- in (e.g. use objects in object oriented languages).
21
-
22
- A model paradigm (in no particular language):
23
-
24
- // Create client instance
25
- api = new HaveAPI.Client("https://your.api.tld")
26
-
27
- // Authenticate
28
- api.authenticate("basic", {"user": "yourname", "password": "yourpassword"})
29
-
30
- // Access resources and actions
31
- api.<resource>.<action>( { <parameters> } )
32
- api.user.new({"name": "Very Name", "password": "donottellanyone"})
33
- api.user.list()
34
- api.nested.resource.deep.list()
35
-
36
- // Pass IDs to resources
37
- api.nested(1).resource(2).deep.list()
38
-
39
- // Object-like access
40
- user = api.user.show(1)
41
- user.id
42
- user.name
43
- user.destroy()
44
-
45
- // Logout
46
- api.logout()
47
-
48
- # Necessary features to implement
49
- A client should implement all of the listed features in order to be useful.
50
-
51
- ## Resource tree
52
- The client gives access to all listed resources and their actions.
53
-
54
- In scripting languages, resources and actions are usually defined as dynamic
55
- properties/objects/methods, depending on the language.
56
-
57
- ## Input/output parameters
58
- Allow sending input parameters and accessing the response.
59
-
60
- ## Input/output formats
61
- A client must send appropriate HTTP header `Accept`. As only JSON is by now built-in
62
- in HaveAPI, it should send `application/json`.
63
-
64
- ## Authentication
65
- All authentication methods that are built-in the HaveAPI should be supported
66
- if possible. The client should choose suitable authentication method
67
- for its purpose and allow the developer to select the authentication method
68
- if it makes sense to do so.
69
-
70
- It is a good practise to implement authentication methods as plugins,
71
- so that developers may add custom authentication providers easily.
72
-
73
- ## Object-like access
74
- A client must interpret the API response according to action output layout.
75
- Layouts `object` and `object_list` should be handled as object instances,
76
- if the language allows it.
77
-
78
- Object instances represent the object fetched from the database. Received
79
- parameters are accessed via object attributes/properties. Actions are defined
80
- as instance methods. Objects may have associations to other resources which
81
- must be made available and be easy to access.
82
-
83
- # Supplemental features
84
- Following features are supplemental. It is good to support them,
85
- but is not necessary.
86
-
87
- ## Client-side validations
88
- Client may make use of described validators and validate the input before
89
- sending it to the API, to lessen the API load and make it more user-friendly.
90
-
91
- However, as the input is validated on the server anyway, it does not have
92
- to be implemented.
93
-
94
- ## Metadata channel
95
- Metadata channel is currently used to specify what associated resources should
96
- be prefetched and whether an object list should contain total number of items.
97
-
98
- Metadata is nothing more than a hash in input parameters under key `_meta`.
99
-
100
- ## Blocking actions
101
- Useful for APIs with long-running actions. Clients can check state of such actions
102
- using resource `ActionState`. Because this resource is automatically present in all
103
- APIs that use blocking actions, client libraries expose this resource to the developer
104
- just as any other resource in the API.
105
-
106
- However, you may wish to integrate it in your client and provide ways for the action
107
- call to block/accept callbacks.