Class: SuperSettings::Storage::HttpStorage

Inherits:
StorageAttributes show all
Includes:
SuperSettings::Storage, Transaction
Defined in:
lib/super_settings/storage/http_storage.rb

Overview

SuperSettings::Storage model that reads from a remote service running the SuperSettings REST API. This storage engine is read only. It is intended to allow microservices to read settings from a central application that exposes the SuperSettings::RestAPI.

You must the the base_url class attribute to the base URL of a SuperSettings REST API endpoint. You can also set the timeout, headers, and query_params used in reqeusts to the API.

Defined Under Namespace

Classes: HistoryStorage

Constant Summary collapse

DEFAULT_HEADERS =
{"Accept" => "application/json"}.freeze
DEFAULT_TIMEOUT =
5.0

Class Attribute Summary collapse

Attributes inherited from StorageAttributes

#created_at, #description, #key, #raw_value, #updated_at, #value_type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transaction

included, #persisted=, #persisted?, #save!

Methods included from SuperSettings::Storage

#==, #create_history, #created_at, #created_at=, #deleted=, #deleted?, #description, #description=, included, #key, #key=, #persisted?, #raw_value, #raw_value=, #save!, #updated_at, #updated_at=, #value_type, #value_type=

Methods inherited from StorageAttributes

#deleted=, #deleted?, #initialize, #persisted=, #persisted?

Constructor Details

This class inherits a constructor from SuperSettings::Storage::StorageAttributes

Class Attribute Details

.base_urlObject

Set the base URL for the SuperSettings REST API.



30
31
32
# File 'lib/super_settings/storage/http_storage.rb', line 30

def base_url
  @base_url
end

.headersObject (readonly)

Add headers to this hash to add them to all requests to the SuperSettings REST API.

Examples:

SuperSettings::HttpStorage.headers["Authorization"] = "Bearer 12345"


39
40
41
# File 'lib/super_settings/storage/http_storage.rb', line 39

def headers
  @headers
end

.query_paramsObject (readonly)

Add query parameters to this hash to add them to all requests to the SuperSettings REST API.

Examples:

SuperSettings::HttpStorage.query_params["access_token"] = "12345"


45
46
47
# File 'lib/super_settings/storage/http_storage.rb', line 45

def query_params
  @query_params
end

.timeoutObject

Set the timeout for requests to the SuperSettings REST API.



33
34
35
# File 'lib/super_settings/storage/http_storage.rb', line 33

def timeout
  @timeout
end

Class Method Details

.allObject



47
48
49
50
51
# File 'lib/super_settings/storage/http_storage.rb', line 47

def all
  call_api(:get, "/settings")["settings"].collect do |attributes|
    new(attributes)
  end
end

.create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object



72
73
74
# File 'lib/super_settings/storage/http_storage.rb', line 72

def create_history(key:, changed_by:, created_at:, value: nil, deleted: false)
  # No-op since history is maintained by the source system.
end

.find_by_key(key) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/super_settings/storage/http_storage.rb', line 59

def find_by_key(key)
  record = new(call_api(:get, "/setting", key: key))
  record.persisted = true
  record
rescue HttpClient::NotFoundError
  nil
end

.last_updated_atObject



67
68
69
70
# File 'lib/super_settings/storage/http_storage.rb', line 67

def last_updated_at
  value = call_api(:get, "/settings/last_updated_at")["last_updated_at"]
  SuperSettings::Coerce.time(value)
end

.save_all(changes) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/super_settings/storage/http_storage.rb', line 76

def save_all(changes)
  payload = []
  changes.each do |setting|
    setting_payload = {key: setting.key}

    if setting.deleted?
      setting_payload[:deleted] = true
    else
      setting_payload[:value] = setting.value
      setting_payload[:value_type] = setting.value_type
      setting_payload[:description] = setting.description
    end

    payload << setting_payload
  end

  begin
    call_api(:post, "/settings", settings: payload)
  rescue HttpClient::InvalidRecordError
    return false
  end

  true
end

.updated_since(time) ⇒ Object



53
54
55
56
57
# File 'lib/super_settings/storage/http_storage.rb', line 53

def updated_since(time)
  call_api(:get, "/settings/updated_since", time: time)["settings"].collect do |attributes|
    new(attributes)
  end
end

Instance Method Details

#history(limit: nil, offset: 0) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/super_settings/storage/http_storage.rb', line 127

def history(limit: nil, offset: 0)
  params = {key: key}
  params[:offset] = offset if offset > 0
  params[:limit] = limit if limit
  history = call_api(:get, "/setting/history", params)
  history["histories"].collect do |attributes|
    HistoryItem.new(key: key, value: attributes["value"], changed_by: attributes["changed_by"], created_at: attributes["created_at"], deleted: attributes["deleted"])
  end
end

#reloadObject



137
138
139
140
141
# File 'lib/super_settings/storage/http_storage.rb', line 137

def reload
  self.class.find_by_key(key)
  self.attributes = self.class.find_by_key(key).attributes
  self
end