Class: SuperSettings::RestAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/super_settings/rest_api.rb

Class Method Summary collapse

Class Method Details

.history(key, limit: nil, offset: 0) ⇒ Hash?

Return the history of the setting.

Examples:

GET /setting/history

Query parameters

* key - setting key
* limit - number of history items to return
* offset - index to start fetching items from (most recent items are first)

The response format is:
{
  key: string,
  histories: [
    {
      value: object,
      changed_by: string,
      created_at: iso8601 string
    },
    ...
  ],
  previous_page_params: hash,
  next_page_params: hash
}

Parameters:

  • key (String)

    setting key

  • limit (Integer) (defaults to: nil)

    number of history items to return

  • offset (Integer) (defaults to: 0)

    index to start fetching items from (most recent items are first)

Returns:

  • (Hash, nil)

    history hash or nil if setting not found



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
164
165
166
167
168
# File 'lib/super_settings/rest_api.rb', line 138

def history(key, limit: nil, offset: 0)
  setting = Setting.find_by_key(key)
  return nil unless setting

  offset = [offset.to_i, 0].max
  limit = limit.to_i
  fetch_limit = ((limit > 0) ? limit + 1 : nil)
  histories = setting.history(limit: fetch_limit, offset: offset)

  payload = {key: setting.key}

  if limit > 0 && !histories.empty?
    if offset > 0
      previous_page_params = {key: setting.key, offset: [offset - limit, 0].max, limit: limit}
    end
    if histories.size > limit
      histories = histories.take(limit)
      next_page_params = {key: setting.key, offset: offset + limit, limit: limit}
    end
    payload[:previous_page_params] = previous_page_params if previous_page_params
    payload[:next_page_params] = next_page_params if next_page_params
  end

  payload[:histories] = histories.collect do |history|
    history_values = {value: history.value, changed_by: history.changed_by_display, created_at: history.created_at.utc.iso8601(6)}
    history_values[:deleted] = true if history.deleted?
    history_values
  end

  payload
end

.indexHash

Get all settings sorted by key. This endpoint may be called with a REST GET request.

Examples:

GET /

The response payload is:
[
  {
    key: string,
    value: object,
    value_type: string,
    description: string,
    created_at: iso8601 string,
    updated_at: iso8601 string
  },
  ...
]

Returns:

  • (Hash)

    hash with settings array



27
28
29
30
# File 'lib/super_settings/rest_api.rb', line 27

def index
  settings = Setting.active.reject(&:deleted?).sort_by(&:key)
  {settings: settings.collect(&:as_json)}
end

.last_updated_atHash

Return the timestamp of the most recently updated setting.

Examples:

GET /last_updated_at

The response payload is:
{
  last_updated_at: iso8601 string
}

Returns:

  • (Hash)

    hash with the last updated timestamp



181
182
183
# File 'lib/super_settings/rest_api.rb', line 181

def last_updated_at
  {last_updated_at: Setting.last_updated_at.utc.iso8601(6)}
end

.show(key) ⇒ Hash?

Get a setting by id.

Examples:

GET /setting

Query parameters

* key - setting key

The response payload is:
{
  key: string,
  value: object,
  value_type: string,
  description: string,
  created_at: iso8601 string,
  updated_at: iso8601 string
}

Parameters:

  • key (String)

    setting key

Returns:

  • (Hash, nil)

    setting hash or nil if not found



53
54
55
56
# File 'lib/super_settings/rest_api.rb', line 53

def show(key)
  setting = Setting.find_by_key(key)
  setting.as_json if setting && !setting.deleted?
end

.update(settings_params, changed_by = nil) ⇒ Hash

The update operation uses a transaction to atomically update all settings.

Examples:

POST /settings

The format of the parameters is an array of hashes with each setting identified by the key.
The settings should include either "value" and "value_type" (and optionally "description") to
insert or update a setting, or "deleted" to delete the setting.

{ settings: [
    {
      key: string,
      value: object,
      value_type: string,
      description: string,
    },
    {
      key: string,
      deleted: boolean,
    },
    ...
  ]
}

The response will be either

{success: true}

or

{success: false, errors: {key => [string], ...}}

Parameters:

  • settings_params (Array)

    array of setting parameter hashes

  • changed_by (String) (defaults to: nil)

    identifier for who made the changes

Returns:

  • (Hash)

    result hash with success status and any errors



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/super_settings/rest_api.rb', line 93

def update(settings_params, changed_by = nil)
  all_valid, settings = Setting.bulk_update(Array(settings_params), changed_by)
  if all_valid
    {success: true}
  else
    errors = {}
    settings.each do |setting|
      if setting.errors.any?
        errors[setting.key] = setting.errors.values.flatten
      end
    end
    {success: false, errors: errors}
  end
end

.updated_since(time) ⇒ Hash

Return settings that have been updated since a specified timestamp.

Examples:

GET /updated_since

Query parameters

* time - iso8601 string

The response payload is:
[
  {
    key: string,
    value: object,
    value_type: string,
    description: string,
    created_at: iso8601 string,
    updated_at: iso8601 string
  },
  ...
]

Parameters:

  • time (Time, String)

    timestamp to check for updates since

Returns:

  • (Hash)

    hash with settings array



209
210
211
212
213
# File 'lib/super_settings/rest_api.rb', line 209

def updated_since(time)
  time = Coerce.time(time)
  settings = Setting.updated_since(time).reject(&:deleted?)
  {settings: settings.collect(&:as_json)}
end