rest_framework 0.0.10 → 0.0.12

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: a3ed68df38881608ec5cc6c66c286624a600398a745ab730e6cef89513f0138f
4
- data.tar.gz: 7f2654b29027aa24357b14082e0e0236d9024521fe47e3ae90be7fdeda535b5f
3
+ metadata.gz: 8c3691667e86ee77e9d15a1732d0013687a2e63a062a4ff01d1c6d8cc66f45fc
4
+ data.tar.gz: 6bcab926c283feb9a0643a06eab750f941882e5b56b294c59794f64a966883bb
5
5
  SHA512:
6
- metadata.gz: 460fd09cc0b375e5509a83ff64bf5ffb87f864cb53e5cb51aa1027bce3a39bbd12a8f692a3c621ad93ac200f98bb095b369610b2953eedb9a47a181568fea948
7
- data.tar.gz: 03c6ec7c3d910dac783e2ddb927ea53212e066246dcf1b2673d95e703754a9295a5e792d9daeb3c40fd5d334491e489e076b4740f766ab7880eaf3d731344bda
6
+ metadata.gz: 204e0b5ea7b46710968a6274791f9e0f676db34f4eaefa1cf831fe8f9e4ab1f076a24b0375ef0554e1c4b57db3d1787adeebc31c4201da40b504aded51ca097b
7
+ data.tar.gz: 7e46ed5bbe61832045207664d679ecf394aead2928cdd78a1b29873087b78130546fd80595fe443680d5c26eebfa8bf5f23246b56a9a4a83e801c7763c024c76
@@ -46,7 +46,7 @@
46
46
  <div class="tab-content w-100 pt-3">
47
47
  <div class="tab-pane fade show active" id="tab-json" role="tab">
48
48
  <% if @json_payload %>
49
- <div><pre><code class="language-json"><%= JSON.pretty_generate(JSON.parse(@json_payload)) %></code></pre></div>
49
+ <div><pre><code class="language-json"><%= JSON.pretty_generate(JSON.parse(@json_payload)) unless @json_payload == '' %></code></pre></div>
50
50
  <% end %>
51
51
  </div>
52
52
  <div class="tab-pane fade" id="tab-xml" role="tab">
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.12
@@ -74,35 +74,42 @@ module RESTFramework
74
74
  }.select { |r| r[:path].start_with?(request.path) }
75
75
  end
76
76
 
77
- # Helper alias for `respond_to`/`render`, and replace nil responses with blank ones. `payload`
78
- # should be already serialized to Ruby primitives.
79
- def api_response(payload, html_kwargs: nil, json_kwargs: nil, xml_kwargs: nil, **kwargs)
77
+ # Helper alias for `respond_to`/`render`. `payload` should be already serialized to Ruby
78
+ # primitives.
79
+ def api_response(payload=nil, html_kwargs: nil, json_kwargs: nil, xml_kwargs: nil, **kwargs)
80
80
  html_kwargs ||= {}
81
81
  json_kwargs ||= {}
82
82
  xml_kwargs ||= {}
83
83
 
84
- # make empty responses status 204 unless a status is already explicitly defined
85
- if (payload.nil? || payload == '') && !kwargs.key?(:status)
86
- kwargs[:status] = 204
87
- end
84
+ # allow blank (no-content) responses
85
+ @blank = kwargs[:blank]
88
86
 
89
87
  respond_to do |format|
90
- if payload.respond_to?(:to_json)
91
- format.json {
92
- kwargs = kwargs.merge(json_kwargs)
93
- render(json: payload || '', **kwargs)
94
- }
95
- end
96
- if payload.respond_to?(:to_xml)
97
- format.xml {
98
- kwargs = kwargs.merge(xml_kwargs)
99
- render(xml: payload || '', **kwargs)
100
- }
88
+ if @blank
89
+ format.json {head :no_content}
90
+ format.xml {head :no_content}
91
+ else
92
+ if payload.respond_to?(:to_json)
93
+ format.json {
94
+ kwargs = kwargs.merge(json_kwargs)
95
+ render(json: payload, layout: false, **kwargs)
96
+ }
97
+ end
98
+ if payload.respond_to?(:to_xml)
99
+ format.xml {
100
+ kwargs = kwargs.merge(xml_kwargs)
101
+ render(xml: payload, layout: false, **kwargs)
102
+ }
103
+ end
101
104
  end
102
105
  format.html {
103
106
  @payload = payload
104
- @json_payload = payload.to_json
105
- @xml_payload = payload.to_xml
107
+ @json_payload = ''
108
+ @xml_payload = ''
109
+ unless @blank
110
+ @json_payload = payload.to_json if payload.respond_to?(:to_json)
111
+ @xml_payload = payload.to_xml if payload.respond_to?(:to_xml)
112
+ end
106
113
  @template_logo_text ||= "Rails REST Framework"
107
114
  @title ||= self.controller_name.camelize
108
115
  @routes ||= self._get_routes
@@ -67,7 +67,7 @@ module RESTFramework
67
67
  # Filter the request body for keys in current action's allowed_parameters/fields config.
68
68
  def _get_parameter_values_from_request_body
69
69
  fields = self.get_allowed_parameters || self.get_fields
70
- return @_get_field_values_from_request_body ||= (request.request_parameters.select { |p|
70
+ return @_get_parameter_values_from_request_body ||= (request.request_parameters.select { |p|
71
71
  fields.include?(p.to_sym) || fields.include?(p.to_s)
72
72
  })
73
73
  end
@@ -77,7 +77,7 @@ module RESTFramework
77
77
  # Filter params for keys allowed by the current action's filterset_fields/fields config.
78
78
  def _get_filterset_values_from_params
79
79
  fields = self.filterset_fields || self.get_fields
80
- return @_get_field_values_from_params ||= request.query_parameters.select { |p|
80
+ return @_get_filterset_values_from_params ||= request.query_parameters.select { |p|
81
81
  fields.include?(p.to_sym) || fields.include?(p.to_s)
82
82
  }
83
83
  end
@@ -59,7 +59,7 @@ module RESTFramework
59
59
  return serializer_config if serializer_config
60
60
 
61
61
  # otherwise, build a serializer config from fields
62
- fields = @controller.send(:get_fields)
62
+ fields = @controller.send(:get_fields) if @controller
63
63
  unless fields.blank?
64
64
  columns, methods = fields.partition { |f| f.to_s.in?(@model.column_names) }
65
65
  return {only: columns, methods: methods}
@@ -68,54 +68,41 @@ module RESTFramework
68
68
  return {}
69
69
  end
70
70
 
71
- # Recursive method for traversing a config and evaluating nested serializers.
72
- def _resolve_serializer_config(node: nil)
73
- # First base case: found a serializer, so evaluate it and return it.
74
- if node.is_a?(Class) && (node < BaseSerializer)
75
- return node.new(controller: @controller).get_resolved_serializer_config
76
- end
77
-
78
- # Second base case: found a serializer instance, so evaluate it and return it.
79
- if node.is_a?(BaseSerializer)
80
- return node.get_resolved_serializer_config
71
+ # Convert the object(s) to Ruby primitives.
72
+ def serialize
73
+ if @object
74
+ @many = @object.respond_to?(:each) if @many.nil?
75
+ return @object.as_json(self.get_serializer_config)
81
76
  end
77
+ return nil
78
+ end
82
79
 
83
- # Third base case: node is not iterable, so return it.
84
- unless node.respond_to?(:each)
85
- return node
80
+ # Allow a serializer instance to be used as a hash directly in a nested serializer config.
81
+ def [](key)
82
+ unless instance_variable_defined?(:@_nested_config)
83
+ @_nested_config = self.get_serializer_config
86
84
  end
87
-
88
- # Recursive case: node is iterable, so iterate and recursively resolve serializers.
89
- if node.is_a? Hash
90
- node.each do |k,v|
91
- node[k] = self._resolve_serializer_config(node: v)
92
- end
93
- else
94
- node.map! do |v|
95
- self._resolve_serializer_config(node: v)
96
- end
85
+ return @_nested_config[key]
86
+ end
87
+ def []=(key, value)
88
+ unless instance_variable_defined?(:@_nested_config)
89
+ @_nested_config = self.get_serializer_config
97
90
  end
98
-
99
- return node
91
+ return @_nested_config[key] = value
100
92
  end
101
93
 
102
- # Get a serializer config and resolve any nested serializers into configs.
103
- def get_resolved_serializer_config
104
- config = self.get_serializer_config
105
-
106
- # traverse the config, resolving nested serializers
107
- return _resolve_serializer_config(node: config)
94
+ # Allow a serializer class to be used as a hash directly in a nested serializer config.
95
+ def self.[](key)
96
+ unless instance_variable_defined?(:@_nested_config)
97
+ @_nested_config = self.new.get_serializer_config
98
+ end
99
+ return @_nested_config[key]
108
100
  end
109
-
110
- # Convert the object(s) to Ruby primitives.
111
- def serialize
112
- if @object
113
- @many = @object.respond_to?(:each) if @many.nil?
114
- return @object.as_json(
115
- self.get_resolved_serializer_config
116
- )
101
+ def self.[]=(key, value)
102
+ unless instance_variable_defined?(:@_nested_config)
103
+ @_nested_config = self.new.get_serializer_config
117
104
  end
118
- return nil
105
+ return @_nested_config[key] = value
119
106
  end
120
107
  end
121
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory N. Schmit