Class: Rack::MonetaRest
- Inherits:
-
Object
- Object
- Rack::MonetaRest
- Defined in:
- lib/rack/moneta_rest.rb
Overview
A Rack application which provides a REST interface to a Moneta store.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(store = nil, options = {}, &block) ⇒ MonetaRest
constructor
A new instance of MonetaRest.
Constructor Details
#initialize(store = nil, options = {}, &block) ⇒ MonetaRest
Returns a new instance of MonetaRest.
20 21 22 23 24 25 26 27 28 |
# File 'lib/rack/moneta_rest.rb', line 20 def initialize(store = nil, = {}, &block) if block raise ArgumentError, 'Use either block or options' unless .empty? @store = ::Moneta.build(&block) else raise ArgumentError, 'Block or argument store is required' unless @store = store @store = ::Moneta.new(@store, ) if Symbol === @store end end |
Instance Method Details
#call(env) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rack/moneta_rest.rb', line 30 def call(env) key = env['PATH_INFO'][1..-1].to_s case env['REQUEST_METHOD'] when 'HEAD' if key.empty? respond(400, 'Empty key') elsif @store.key?(key) empty(200) else empty(404) end when 'GET' if key.empty? respond(400, 'Empty key') elsif value = @store[key] respond(200, value) else empty(404) end when 'POST', 'PUT' if key.empty? respond(400, 'Empty key') else respond(200, @store[key] = env['rack.input'].read) end when 'DELETE' if key.empty? @store.clear empty(200) else respond(200, @store.delete(key)) end else respond(400, 'Bad method') end rescue => ex respond(500, "Exception: #{ex.}") end |