Class: JsonApiServer::Configuration

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

Overview

Description

Configurations for the gem.

  • Be sure to configure :base_url which defaults to nil.

  • Logger defaults to Logger.new(STDOUT). If using Rails, configure to Rails.logger.

  • Custom builders can be added to :filter_builders.

  • Default builders can be substituted.

Example
# config/initializers/json_api_server.rb

# example of custom filter builder
module JsonApiServer
  class MyCustomFilter < FilterBuilder
    def to_query(model)
      model.where("#{column_name} LIKE :val", val: "%#{value}%")
    end
  end
end

JsonApiServer.configure do |c|
  c.base_url = 'http://localhost:3001' # or ENV['HOSTNAME']
  c.filter_builders = c.filter_builders
    .merge({my_custom_builder: JsonApiServer::MyCustomFilter})
  c.logger = Rails.logger
end

Constant Summary collapse

DEFAULT_SERIALIZER_OPTIONS =

Serializer options for the OJ gem.

{
  escape_mode: :xss_safe,
  time: :xmlschema,
  mode: :compat
}.freeze
DEFAULT_FILTER_BUILDERS =

Default filter builders. For generating queries based on on requested filters.

{
  sql_eql: JsonApiServer::SqlEql,
  sql_comparison: JsonApiServer::SqlComp,
  sql_in: JsonApiServer::SqlIn,
  sql_like: JsonApiServer::SqlLike,
  pg_ilike: JsonApiServer::PgIlike,
  pg_jsonb_array: JsonApiServer::PgJsonbArray,
  pg_jsonb_ilike_array: JsonApiServer::PgJsonbIlikeArray,
  model_query: JsonApiServer::ModelQuery
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/json_api_server/configuration.rb', line 86

def initialize
  @base_url = nil
  @default_max_per_page = 100
  @default_per_page = 20
  @default_like_builder = :sql_like
  @default_in_builder = :sql_in
  @default_comparison_builder = :sql_comparison
  @default_builder = :sql_eql
  @serializer_options = DEFAULT_SERIALIZER_OPTIONS
  @filter_builders = DEFAULT_FILTER_BUILDERS
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#base_urlObject

Root url i.e., www.example.com. Used in pagination links.



33
34
35
# File 'lib/json_api_server/configuration.rb', line 33

def base_url
  @base_url
end

#default_builderObject

Defaults to sql_eql: JsonApiServer::SqlEql.



58
59
60
# File 'lib/json_api_server/configuration.rb', line 58

def default_builder
  @default_builder
end

#default_comparison_builderObject

Defaults to sql_comparison: JsonApiServer::SqlComp. For <,>, <=, >=, etc. queries.



55
56
57
# File 'lib/json_api_server/configuration.rb', line 55

def default_comparison_builder
  @default_comparison_builder
end

#default_in_builderObject

Defaults to sql_in: JsonApiServer::SqlIn. For IN (x,y,z) queries.



51
52
53
# File 'lib/json_api_server/configuration.rb', line 51

def default_in_builder
  @default_in_builder
end

#default_like_builderObject

Defaults to sql_like: JsonApiServer::SqlLike. If using Postgres, it can be replaced with pg_ilike: JsonApiServer::PgIlike.



48
49
50
# File 'lib/json_api_server/configuration.rb', line 48

def default_like_builder
  @default_like_builder
end

#default_max_per_pageObject

Pagination option. Default maximum number of records to show per page. Defaults to 100.



37
38
39
# File 'lib/json_api_server/configuration.rb', line 37

def default_max_per_page
  @default_max_per_page
end

#default_per_pageObject

Pagination option. Default number of records to show per page. Defaults to 20.



41
42
43
# File 'lib/json_api_server/configuration.rb', line 41

def default_per_page
  @default_per_page
end

#filter_buildersObject

Defaults to DEFAULT_FILTER_BUILDERS.



61
62
63
# File 'lib/json_api_server/configuration.rb', line 61

def filter_builders
  @filter_builders
end

#loggerObject

Defaults to Logger.new(STDOUT)



64
65
66
# File 'lib/json_api_server/configuration.rb', line 64

def logger
  @logger
end

#serializer_optionsObject

JSON is serialized with OJ gem. Options are defined in DEFAULT_SERIALIZER_OPTIONS.



44
45
46
# File 'lib/json_api_server/configuration.rb', line 44

def serializer_options
  @serializer_options
end