Class: ThreeScale::Backend::Service
- Inherits:
-
Object
- Object
- ThreeScale::Backend::Service
- Extended by:
- Memoizer::Decorator
- Includes:
- Storable
- Defined in:
- lib/3scale/backend/service.rb
Instance Attribute Summary collapse
-
#backend_version ⇒ Object
Returns the value of attribute backend_version.
-
#default_service ⇒ Object
writeonly
Sets the attribute default_service.
-
#id ⇒ Object
Returns the value of attribute id.
-
#provider_key ⇒ Object
Returns the value of attribute provider_key.
-
#referrer_filters_required ⇒ Object
writeonly
Sets the attribute referrer_filters_required.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- .attribute_names ⇒ Object
-
.authenticate_service_id(service_id, provider_key) ⇒ Object
Returns true if a given service belongs to the provider with that key without loading the whole object.
- .clear_cache(provider_key, id) ⇒ Object
- .default_id(provider_key) ⇒ Object
- .default_id!(provider_key) ⇒ Object
- .delete_by_id(service_id) ⇒ Object
- .exists?(service_id) ⇒ Boolean
- .get_service(id) ⇒ Object
- .list(provider_key) ⇒ Object
- .load_by_id(service_id) ⇒ Object
- .load_by_id!(service_id) ⇒ Object
- .load_with_provider_key!(id, provider_key) ⇒ Object
-
.provider_key_for(service_id) ⇒ Object
Gets the provider key without loading the whole service.
- .save!(attributes = {}) ⇒ Object
- .storage_key(id, attribute) ⇒ Object
- .storage_key_by_provider(provider_key, attribute) ⇒ Object
Instance Method Summary collapse
- #active=(value) ⇒ Object
- #active? ⇒ Boolean (also: #active)
- #clear_cache ⇒ Object
- #default_service? ⇒ Boolean
- #delete_data ⇒ Object
-
#initialize(attributes = {}) ⇒ Service
constructor
A new instance of Service.
- #referrer_filters_required? ⇒ Boolean
- #save! ⇒ Object
- #storage_key(attribute) ⇒ Object
- #to_hash ⇒ Object
Methods included from Memoizer::Decorator
Methods included from Storable
Methods included from StorageKeyHelpers
Constructor Details
#initialize(attributes = {}) ⇒ Service
Returns a new instance of Service.
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/3scale/backend/service.rb', line 155 def initialize(attributes = {}) # :state is set as active in this method when: # - The state key is not present in the attributes hash # - The state key is present in the attributes hash but it has # the nil value # This is done in order to not break compatibility for existing # Services saved in the database, that do not contain the state # key. attributes[:state] ||= :active super(attributes) end |
Instance Attribute Details
#backend_version ⇒ Object
Returns the value of attribute backend_version.
11 12 13 |
# File 'lib/3scale/backend/service.rb', line 11 def backend_version @backend_version end |
#default_service=(value) ⇒ Object (writeonly)
Sets the attribute default_service
12 13 14 |
# File 'lib/3scale/backend/service.rb', line 12 def default_service=(value) @default_service = value end |
#id ⇒ Object
Returns the value of attribute id.
11 12 13 |
# File 'lib/3scale/backend/service.rb', line 11 def id @id end |
#provider_key ⇒ Object
Returns the value of attribute provider_key.
11 12 13 |
# File 'lib/3scale/backend/service.rb', line 11 def provider_key @provider_key end |
#referrer_filters_required=(value) ⇒ Object (writeonly)
Sets the attribute referrer_filters_required
12 13 14 |
# File 'lib/3scale/backend/service.rb', line 12 def referrer_filters_required=(value) @referrer_filters_required = value end |
#state ⇒ Object
Returns the value of attribute state.
10 11 12 |
# File 'lib/3scale/backend/service.rb', line 10 def state @state end |
Class Method Details
.attribute_names ⇒ Object
17 18 19 |
# File 'lib/3scale/backend/service.rb', line 17 def attribute_names (ATTRIBUTES + %i[id default_service].freeze).freeze end |
.authenticate_service_id(service_id, provider_key) ⇒ Object
Returns true if a given service belongs to the provider with that key without loading the whole object.
24 25 26 |
# File 'lib/3scale/backend/service.rb', line 24 def authenticate_service_id(service_id, provider_key) provider_key == provider_key_for(service_id) end |
.clear_cache(provider_key, id) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/3scale/backend/service.rb', line 114 def clear_cache(provider_key, id) provider_key_arg = [provider_key] keys = Memoizer.build_keys_for_class(self, authenticate_service_id: [id, provider_key], default_id: provider_key_arg, load_by_id: [id], list: provider_key_arg, provider_key_for: [id]) Memoizer.clear keys end |
.default_id(provider_key) ⇒ Object
29 30 31 |
# File 'lib/3scale/backend/service.rb', line 29 def default_id(provider_key) storage.get(storage_key_by_provider(provider_key, :id)) end |
.default_id!(provider_key) ⇒ Object
34 35 36 |
# File 'lib/3scale/backend/service.rb', line 34 def default_id!(provider_key) default_id(provider_key) or raise ProviderKeyInvalid, provider_key end |
.delete_by_id(service_id) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/3scale/backend/service.rb', line 71 def delete_by_id(service_id) service = load_by_id!(service_id) if service.default_service? and not service_is_the_only_one_for_provider(service_id) raise ServiceIsDefaultService, service.id end service.delete_data service.clear_cache end |
.exists?(service_id) ⇒ Boolean
82 83 84 |
# File 'lib/3scale/backend/service.rb', line 82 def exists?(service_id) storage.exists(storage_key(service_id, 'provider_key')) end |
.get_service(id) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/3scale/backend/service.rb', line 86 def get_service(id) keys = ATTRIBUTES.map { |attr| storage_key(id, attr) } values = storage.mget(keys) result = {} ATTRIBUTES.each_with_index do |key, idx| result[key] = values[idx] end result end |
.list(provider_key) ⇒ Object
97 98 99 |
# File 'lib/3scale/backend/service.rb', line 97 def list(provider_key) storage.smembers(storage_key_by_provider(provider_key, :ids)) || [] end |
.load_by_id(service_id) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/3scale/backend/service.rb', line 38 def load_by_id(service_id) return if service_id.nil? service_attrs = get_service(id = service_id.to_s) massage_service_attrs service_attrs return if service_attrs[:provider_key].nil? new(service_attrs.merge(id: id, default_service: default_service?(service_attrs[:provider_key], id) )) end |
.load_by_id!(service_id) ⇒ Object
52 53 54 |
# File 'lib/3scale/backend/service.rb', line 52 def load_by_id!(service_id) load_by_id(service_id) or raise ServiceIdInvalid, service_id end |
.load_with_provider_key!(id, provider_key) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/3scale/backend/service.rb', line 56 def load_with_provider_key!(id, provider_key) id = Service.default_id(provider_key) if id.nil? || id.empty? raise ProviderKeyInvalidOrServiceMissing, provider_key if id.nil? || id.empty? service = Service.load_by_id(id.split('-').last) || Service.load_by_id!(id) if service.provider_key != provider_key # this is an error; let's raise in default_id! or raise invalid service Service.default_id!(provider_key) raise ServiceIdInvalid, id end service end |
.provider_key_for(service_id) ⇒ Object
Gets the provider key without loading the whole service
126 127 128 |
# File 'lib/3scale/backend/service.rb', line 126 def provider_key_for(service_id) storage.get(storage_key(service_id, 'provider_key'.freeze)) end |
.save!(attributes = {}) ⇒ Object
102 103 104 |
# File 'lib/3scale/backend/service.rb', line 102 def save!(attributes = {}) new(attributes).save! end |
.storage_key(id, attribute) ⇒ Object
106 107 108 |
# File 'lib/3scale/backend/service.rb', line 106 def storage_key(id, attribute) encode_key("service/id:#{id}/#{attribute}") end |
.storage_key_by_provider(provider_key, attribute) ⇒ Object
110 111 112 |
# File 'lib/3scale/backend/service.rb', line 110 def storage_key_by_provider(provider_key, attribute) encode_key("service/provider_key:#{provider_key}/#{attribute}") end |
Instance Method Details
#active=(value) ⇒ Object
213 214 215 |
# File 'lib/3scale/backend/service.rb', line 213 def active=(value) self.state = value ? :active : :suspended end |
#active? ⇒ Boolean Also known as: active
208 209 210 |
# File 'lib/3scale/backend/service.rb', line 208 def active? state == :active end |
#clear_cache ⇒ Object
183 184 185 |
# File 'lib/3scale/backend/service.rb', line 183 def clear_cache self.class.clear_cache(provider_key, id) end |
#default_service? ⇒ Boolean
168 169 170 |
# File 'lib/3scale/backend/service.rb', line 168 def default_service? @default_service end |
#delete_data ⇒ Object
191 192 193 194 195 |
# File 'lib/3scale/backend/service.rb', line 191 def delete_data delete_from_lists delete_attributes ErrorStorage.delete_all(id) end |
#referrer_filters_required? ⇒ Boolean
172 173 174 |
# File 'lib/3scale/backend/service.rb', line 172 def referrer_filters_required? @referrer_filters_required end |
#save! ⇒ Object
176 177 178 179 180 181 |
# File 'lib/3scale/backend/service.rb', line 176 def save! set_as_default_if_needed persist clear_cache self end |
#storage_key(attribute) ⇒ Object
187 188 189 |
# File 'lib/3scale/backend/service.rb', line 187 def storage_key(attribute) self.class.storage_key id, attribute end |
#to_hash ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/3scale/backend/service.rb', line 197 def to_hash { id: id, state: state, provider_key: provider_key, backend_version: backend_version, referrer_filters_required: referrer_filters_required?, default_service: default_service? } end |