Class: JSONAPI::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/jsonapi/configuration.rb', line 46

def initialize
  #:underscored_key, :camelized_key, :dasherized_key, or custom
  self.json_key_format = :dasherized_key

  #:underscored_route, :camelized_route, :dasherized_route, or custom
  self.route_format = :dasherized_route

  #:integer, :uuid, :string, or custom (provide a proc)
  self.resource_key_type = :integer

  # optional request features
  self.default_allow_include_to_one = true
  self.default_allow_include_to_many = true
  self.allow_sort = true
  self.allow_filter = true

  self.raise_if_parameters_not_allowed = true

  self.warn_on_route_setup_issues = true
  self.warn_on_missing_routes = true
  self.warn_on_performance_issues = true

  # :none, :offset, :paged, or a custom paginator name
  self.default_paginator = :none

  # Output pagination links at top level
  self.top_level_links_include_pagination = true

  self.default_page_size = 10
  self.maximum_page_size = 20

  # Metadata
  # Output record count in top level meta for find operation
  self.top_level_meta_include_record_count = false
  self.top_level_meta_record_count_key = :record_count

  self.top_level_meta_include_page_count = false
  self.top_level_meta_page_count_key = :page_count

  self.use_text_errors = false

  # Whether or not to include exception backtraces in JSONAPI error
  # responses.  Defaults to `false` in anything other than development or test.
  self.include_backtraces_in_errors = (Rails.env.development? || Rails.env.test?)

  # Whether or not to include exception application backtraces in JSONAPI error
  # responses.  Defaults to `false` in anything other than development or test.
  self.include_application_backtraces_in_errors = (Rails.env.development? || Rails.env.test?)

  # List of classes that should not be rescued by the operations processor.
  # For example, if you use Pundit for authorization, you might
  # raise a Pundit::NotAuthorizedError at some point during operations
  # processing. If you want to use Rails' `rescue_from` macro to
  # catch this error and render a 403 status code, you should add
  # the `Pundit::NotAuthorizedError` to the `exception_class_allowlist`.
  self.exception_class_allowlist = []

  # If enabled, will override configuration option `exception_class_allowlist`
  # and allow all exceptions.
  self.allow_all_exceptions = false

  # Resource Linkage
  # Controls the serialization of resource linkage for non compound documents
  # NOTE: always_include_to_many_linkage_data is not currently implemented
  self.always_include_to_one_linkage_data = false
  self.always_include_to_many_linkage_data = false

  # The default Operation Processor to use if one is not defined specifically
  # for a Resource.
  self.default_processor_klass_name = 'JSONAPI::Processor'

  # Allows transactions for creating and updating records
  # Set this to false if your backend does not support transactions (e.g. Mongodb)
  self.allow_transactions = true

  # Formatter Caching
  # Set to false to disable caching of string operations on keys and links.
  # Note that unlike the resource cache, formatter caching is always done
  # internally in-memory and per-thread; no ActiveSupport::Cache is used.
  self.cache_formatters = true

  # Relationship reflection invokes the related resource when updates
  # are made to a has_many relationship. By default relationship_reflection
  # is turned off because it imposes a small performance penalty.
  self.use_relationship_reflection = false

  # Resource cache
  # An ActiveSupport::Cache::Store or similar, used by Resources with caching enabled.
  # Set to `nil` (the default) to disable caching, or to `Rails.cache` to use the
  # Rails cache store.
  self.resource_cache = nil

  # Cache resources by default
  # Cache resources by default. Individual resources can be excluded from caching by calling:
  # `caching false`
  self.default_caching = false

  # Default resource cache field
  # On Resources with caching enabled, this field will be used to check for out-of-date
  # cache entries, unless overridden on a specific Resource. Defaults to "updated_at".
  self.default_resource_cache_field = :updated_at

  # Resource cache digest function
  # Provide a callable that returns a unique value for string inputs with
  # low chance of collision. The default is SHA256 base64.
  self.resource_cache_digest_function = Digest::SHA2.new.method(:base64digest)

  # Resource cache usage reporting
  # Optionally provide a callable which JSONAPI will call with information about cache
  # performance. Should accept three arguments: resource name, hits count, misses count.
  self.resource_cache_usage_report_function = nil

  # Global configuration for links exclusion
  # Controls whether to generate links like `self`, `related` with all the resources
  # and relationships. Accepts either `:default`, `:none`, or array containing the
  # specific default links to exclude, which may be `:self` and `:related`.
  self.default_exclude_links = :none
end

Instance Attribute Details

#allow_all_exceptionsObject

Returns the value of attribute allow_all_exceptions.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def allow_all_exceptions
  @allow_all_exceptions
end

#allow_filterObject

Returns the value of attribute allow_filter.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def allow_filter
  @allow_filter
end

#allow_sortObject

Returns the value of attribute allow_sort.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def allow_sort
  @allow_sort
end

#allow_transactionsObject

Returns the value of attribute allow_transactions.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def allow_transactions
  @allow_transactions
end

#always_include_to_many_linkage_dataObject

Returns the value of attribute always_include_to_many_linkage_data.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def always_include_to_many_linkage_data
  @always_include_to_many_linkage_data
end

#always_include_to_one_linkage_dataObject

Returns the value of attribute always_include_to_one_linkage_data.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def always_include_to_one_linkage_data
  @always_include_to_one_linkage_data
end

#cache_formattersObject

Returns the value of attribute cache_formatters.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def cache_formatters
  @cache_formatters
end

#default_allow_include_to_manyObject

Returns the value of attribute default_allow_include_to_many.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_allow_include_to_many
  @default_allow_include_to_many
end

#default_allow_include_to_oneObject

Returns the value of attribute default_allow_include_to_one.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_allow_include_to_one
  @default_allow_include_to_one
end

#default_cachingObject

Returns the value of attribute default_caching.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_caching
  @default_caching
end

Returns the value of attribute default_exclude_links.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_exclude_links
  @default_exclude_links
end

#default_page_sizeObject

Returns the value of attribute default_page_size.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_page_size
  @default_page_size
end

#default_paginatorObject

Returns the value of attribute default_paginator.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_paginator
  @default_paginator
end

#default_processor_klass_nameObject

Returns the value of attribute default_processor_klass_name.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_processor_klass_name
  @default_processor_klass_name
end

#default_resource_cache_fieldObject

Returns the value of attribute default_resource_cache_field.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def default_resource_cache_field
  @default_resource_cache_field
end

#exception_class_allowlistObject

Returns the value of attribute exception_class_allowlist.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def exception_class_allowlist
  @exception_class_allowlist
end

#include_application_backtraces_in_errorsObject

Returns the value of attribute include_application_backtraces_in_errors.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def include_application_backtraces_in_errors
  @include_application_backtraces_in_errors
end

#include_backtraces_in_errorsObject

Returns the value of attribute include_backtraces_in_errors.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def include_backtraces_in_errors
  @include_backtraces_in_errors
end

#json_key_formatObject

Returns the value of attribute json_key_format.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def json_key_format
  @json_key_format
end

#maximum_page_sizeObject

Returns the value of attribute maximum_page_size.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def maximum_page_size
  @maximum_page_size
end

#raise_if_parameters_not_allowedObject

Returns the value of attribute raise_if_parameters_not_allowed.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def raise_if_parameters_not_allowed
  @raise_if_parameters_not_allowed
end

#resource_cacheObject

Returns the value of attribute resource_cache.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def resource_cache
  @resource_cache
end

#resource_cache_digest_functionObject

Returns the value of attribute resource_cache_digest_function.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def resource_cache_digest_function
  @resource_cache_digest_function
end

#resource_cache_usage_report_functionObject

Returns the value of attribute resource_cache_usage_report_function.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def resource_cache_usage_report_function
  @resource_cache_usage_report_function
end

#resource_key_typeObject

Returns the value of attribute resource_key_type.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def resource_key_type
  @resource_key_type
end

#route_formatObject

Returns the value of attribute route_format.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def route_format
  @route_format
end

Returns the value of attribute top_level_links_include_pagination.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def top_level_links_include_pagination
  @top_level_links_include_pagination
end

#top_level_meta_include_page_countObject

Returns the value of attribute top_level_meta_include_page_count.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def top_level_meta_include_page_count
  @top_level_meta_include_page_count
end

#top_level_meta_include_record_countObject

Returns the value of attribute top_level_meta_include_record_count.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def top_level_meta_include_record_count
  @top_level_meta_include_record_count
end

#top_level_meta_page_count_keyObject

Returns the value of attribute top_level_meta_page_count_key.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def top_level_meta_page_count_key
  @top_level_meta_page_count_key
end

#top_level_meta_record_count_keyObject

Returns the value of attribute top_level_meta_record_count_key.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def top_level_meta_record_count_key
  @top_level_meta_record_count_key
end

#use_relationship_reflectionObject

Returns the value of attribute use_relationship_reflection.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def use_relationship_reflection
  @use_relationship_reflection
end

#use_text_errorsObject

Returns the value of attribute use_text_errors.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def use_text_errors
  @use_text_errors
end

#warn_on_missing_routesObject

Returns the value of attribute warn_on_missing_routes.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def warn_on_missing_routes
  @warn_on_missing_routes
end

#warn_on_performance_issuesObject

Returns the value of attribute warn_on_performance_issues.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def warn_on_performance_issues
  @warn_on_performance_issues
end

#warn_on_route_setup_issuesObject

Returns the value of attribute warn_on_route_setup_issues.



9
10
11
# File 'lib/jsonapi/configuration.rb', line 9

def warn_on_route_setup_issues
  @warn_on_route_setup_issues
end

Instance Method Details

#allow_include=(allow_include) ⇒ Object



243
244
245
246
247
# File 'lib/jsonapi/configuration.rb', line 243

def allow_include=(allow_include)
  JSONAPI::CompatibilityHelper.deprecation_warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
  @default_allow_include_to_one = allow_include
  @default_allow_include_to_many = allow_include
end

#default_processor_klassObject



234
235
236
# File 'lib/jsonapi/configuration.rb', line 234

def default_processor_klass
  @default_processor_klass ||= default_processor_klass_name.safe_constantize
end

#default_processor_klass=(default_processor_klass) ⇒ Object



229
230
231
232
# File 'lib/jsonapi/configuration.rb', line 229

def default_processor_klass=(default_processor_klass)
  JSONAPI::CompatibilityHelper.deprecation_warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
  @default_processor_klass = default_processor_klass
end

#exception_class_allowed?(e) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
227
# File 'lib/jsonapi/configuration.rb', line 224

def exception_class_allowed?(e)
  @allow_all_exceptions ||
    @exception_class_allowlist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
end

#exception_class_whitelist=(exception_class_allowlist) ⇒ Object



254
255
256
257
# File 'lib/jsonapi/configuration.rb', line 254

def exception_class_whitelist=(exception_class_allowlist)
  JSONAPI::CompatibilityHelper.deprecation_warn('`exception_class_whitelist` has been replaced by `exception_class_allowlist`')
  @exception_class_allowlist = exception_class_allowlist
end

#key_formatterObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/jsonapi/configuration.rb', line 190

def key_formatter
  if self.cache_formatters
    formatter = @key_formatter_tlv.value
    return formatter if formatter
  end

  formatter = JSONAPI::Formatter.formatter_for(self.json_key_format)

  if self.cache_formatters
    formatter = @key_formatter_tlv.value = formatter.cached
  end

  return formatter
end

#route_formatterObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/jsonapi/configuration.rb', line 209

def route_formatter
  if self.cache_formatters
    formatter = @route_formatter_tlv.value
    return formatter if formatter
  end

  formatter = JSONAPI::Formatter.formatter_for(self.route_format)

  if self.cache_formatters
    formatter = @route_formatter_tlv.value = formatter.cached
  end

  return formatter
end

#whitelist_all_exceptions=(allow_all_exceptions) ⇒ Object



249
250
251
252
# File 'lib/jsonapi/configuration.rb', line 249

def whitelist_all_exceptions=(allow_all_exceptions)
  JSONAPI::CompatibilityHelper.deprecation_warn('`whitelist_all_exceptions` has been replaced by `allow_all_exceptions`')
  @allow_all_exceptions = allow_all_exceptions
end