apia 3.0.3 → 3.2.0

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
  SHA256:
3
- metadata.gz: 33cb0099fcbfe56a47b0c3f6253252a7fbe64963627695d8580936f72731ee37
4
- data.tar.gz: 9744e82b89172cad5f5eac2870484f43dfe0cf23e793e2db6265b7dcbdd20f12
3
+ metadata.gz: f9eccaade7623f6fc8ecae88bb8c87da23d4a436a2d76801ac6dbd2d57db9efe
4
+ data.tar.gz: c77009793790064a1f6219b6a362128359d2e98a0ccc7c8215d184f7afa36b5b
5
5
  SHA512:
6
- metadata.gz: 195b2874b6deeeb9d52f8fd568cf553da7fd09f43a10e04a4302736eae62ea3ecd4272077e8b7d446cfb46a74d9023ba6af69c6d6d5796ecf3036fcb95bde9d2
7
- data.tar.gz: 8f37aca1ff128cbe3d3fe6b0341a64b68df34ba273f6eb7503c8c3b8ec6e672fb14efe24f589e40f81c1d3fb325e6f0649dc5ccdb0a55925bcf68f9818d995c7
6
+ metadata.gz: 3dc391ac5e8ad44636978220ceeb55bef47b10ba03f5826e371ed4041e428873ed59d351447d3345817e3bbb8b25bfafc6ecd01f4f86745ddc1d0851c2bc19a2
7
+ data.tar.gz: 2f92e720ce247b4408c21bc5a4096137ef75433b10c8712c4d2d115f8d4a33def15509bd5a7af05fd1bfffed9e465e57dac6cd4f5422e3aa2278e12aa0727664
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.3
1
+ 3.2.0
@@ -122,6 +122,13 @@ module Apia
122
122
  @source.key?(key.to_sym)
123
123
  end
124
124
 
125
+ # Return whether the argument set has no arguments within?
126
+ #
127
+ # @return [Boolean]
128
+ def empty?
129
+ @source.empty?
130
+ end
131
+
125
132
  # Validate an argument set and return any errors as appropriate
126
133
  #
127
134
  # @param argument [Apia::Argument]
@@ -36,6 +36,19 @@ module Apia
36
36
  @potential_errors ||= Apia::ErrorSet.new
37
37
  end
38
38
 
39
+ def fields=(fieldset)
40
+ unless @fields.empty?
41
+ raise Apia::StandardError, 'Cannot set the fieldset on an endpoint that already has fields defined'
42
+ end
43
+
44
+ @fields = fieldset
45
+ @fields_overriden = true
46
+ end
47
+
48
+ def fields_overriden?
49
+ @fields_overriden == true
50
+ end
51
+
39
52
  def dsl
40
53
  @dsl ||= DSLs::Endpoint.new(self)
41
54
  end
@@ -41,6 +41,10 @@ module Apia
41
41
  end
42
42
 
43
43
  def field(name, *args, type: nil, **options, &block)
44
+ if @definition.fields_overriden?
45
+ raise Apia::StandardError, 'Cannot add fields to an endpoint that has a separate fieldset'
46
+ end
47
+
44
48
  if pagination_options = options.delete(:paginate)
45
49
 
46
50
  unless @definition.paginated_field.nil?
@@ -64,6 +68,10 @@ module Apia
64
68
  super(name, *args, type: type, **options, &block)
65
69
  end
66
70
 
71
+ def fields(fieldset)
72
+ @definition.fields = fieldset
73
+ end
74
+
67
75
  def scopes(*names)
68
76
  names.each { |name| scope(name) }
69
77
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apia
4
+ class Notifications
5
+
6
+ class << self
7
+
8
+ def handlers
9
+ @handlers ||= []
10
+ end
11
+
12
+ def notify(event, args = {})
13
+ handlers.each do |handler|
14
+ handler.call(event, args)
15
+ end
16
+ end
17
+
18
+ def add_handler(&block)
19
+ handlers.push(block)
20
+ end
21
+
22
+ def clear_handlers
23
+ @handlers = nil
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
data/lib/apia/rack.rb CHANGED
@@ -4,6 +4,7 @@ require 'json'
4
4
  require 'apia/rack_error'
5
5
  require 'apia/request'
6
6
  require 'apia/response'
7
+ require 'apia/notifications'
7
8
 
8
9
  module Apia
9
10
  class Rack
@@ -91,19 +92,28 @@ module Apia
91
92
  request.endpoint = route.endpoint
92
93
  request.route = route
93
94
 
95
+ start_time = Time.now
94
96
  response = request.endpoint.execute(request)
97
+ end_time = Time.now
98
+
99
+ Apia::Notifications.notify(:request, { request: request, response: response, time: (end_time - start_time).to_f })
100
+
95
101
  response.rack_triplet
96
102
  rescue ::StandardError => e
97
103
  if e.is_a?(RackError) || e.is_a?(Apia::ManifestError)
98
104
  return e.triplet
99
105
  end
100
106
 
107
+ request_or_nil = defined?(request) ? request : nil
108
+
101
109
  api.definition.exception_handlers.call(e, {
102
110
  env: env,
103
111
  api: api,
104
- request: defined?(request) ? request : nil
112
+ request: request_or_nil
105
113
  })
106
114
 
115
+ Apia::Notifications.notify(:request_error, { exception: e, request: request_or_nil, api: api, env: env })
116
+
107
117
  if development?
108
118
  return triplet_for_exception(e)
109
119
  end
data/lib/apia/request.rb CHANGED
@@ -50,7 +50,9 @@ module Apia
50
50
  def parse_json_from_string(body)
51
51
  return {} if body.empty?
52
52
 
53
- JSON.parse(body)
53
+ response = JSON.parse(body)
54
+ response = {} unless response.is_a?(Hash)
55
+ response
54
56
  rescue JSON::ParserError => e
55
57
  raise InvalidJSONError, e.message
56
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apia
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-25 00:00:00.000000000 Z
11
+ date: 2022-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -116,6 +116,7 @@ files:
116
116
  - lib/apia/lookup_environment.rb
117
117
  - lib/apia/manifest_errors.rb
118
118
  - lib/apia/mock_request.rb
119
+ - lib/apia/notifications.rb
119
120
  - lib/apia/object.rb
120
121
  - lib/apia/object_set.rb
121
122
  - lib/apia/pagination_object.rb