Class: Moneta::Adapters::RestClient
- Inherits:
-
Moneta::Adapter
- Object
- Moneta::Adapter
- Moneta::Adapters::RestClient
- Defined in:
- lib/moneta/adapters/restclient.rb
Overview
Moneta rest client backend which works together with Rack::MonetaRest
Instance Attribute Summary
Attributes inherited from Moneta::Adapter
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
- #initialize(options = {}) ⇒ Object constructor
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods inherited from Moneta::Adapter
backend, backend_block, backend_required?
Methods included from Config
Methods included from Defaults
#[], #[]=, #close, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Object
15 16 17 18 19 |
# File 'lib/moneta/adapters/restclient.rb', line 15 backend do |url:, adapter: nil, **| ::Faraday.new(url, ) do |faraday| faraday.adapter adapter if adapter end end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
46 47 48 49 |
# File 'lib/moneta/adapters/restclient.rb', line 46 def clear( = {}) backend.delete '' self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
40 41 42 43 |
# File 'lib/moneta/adapters/restclient.rb', line 40 def delete(key, = {}) response = backend.delete(key) response.status == 200 ? response.body : nil end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
22 23 24 |
# File 'lib/moneta/adapters/restclient.rb', line 22 def key?(key, = {}) backend.head(key).status == 200 end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
27 28 29 30 |
# File 'lib/moneta/adapters/restclient.rb', line 27 def load(key, = {}) response = backend.get(key) response.status == 200 ? response.body : nil end |
#store(key, value, options = {}) ⇒ Object
Store value with key
33 34 35 36 37 |
# File 'lib/moneta/adapters/restclient.rb', line 33 def store(key, value, = {}) response = backend.post(key, value) raise "HTTP error #{response.status}" unless response.status == 200 value end |