module Sensu::API::Routes::Stashes

Constants

STASHES_URI
STASH_URI

Public Instance Methods

delete_stash() click to toggle source

DELETE /stash/:path or /stashes/:path

# File lib/sensu/api/routes/stashes.rb, line 35
def delete_stash
  path = parse_uri(STASH_URI).first
  @redis.exists("stash:#{path}") do |stash_exists|
    if stash_exists
      @redis.srem("stashes", path) do
        @redis.del("stash:#{path}") do
          no_content!
        end
      end
    else
      not_found!
    end
  end
end
get_stash() click to toggle source

GET /stash/:path or /stashes/:path

# File lib/sensu/api/routes/stashes.rb, line 22
def get_stash
  path = parse_uri(STASH_URI).first
  @redis.get("stash:#{path}") do |stash_json|
    unless stash_json.nil?
      @response_content = Sensu::JSON.load(stash_json)
      respond
    else
      not_found!
    end
  end
end
get_stashes() click to toggle source

GET /stashes

# File lib/sensu/api/routes/stashes.rb, line 51
def get_stashes
  @response_content = []
  @redis.smembers("stashes") do |stashes|
    unless stashes.empty?
      stashes = pagination(stashes)
      stashes.each_with_index do |path, index|
        @redis.get("stash:#{path}") do |stash_json|
          @redis.ttl("stash:#{path}") do |ttl|
            unless stash_json.nil?
              item = {
                :path => path,
                :content => Sensu::JSON.load(stash_json),
                :expire => ttl
              }
              @response_content << item
            else
              @redis.srem("stashes", path)
            end
            if index == stashes.length - 1
              respond
            end
          end
        end
      end
    else
      respond
    end
  end
end
post_stash() click to toggle source

POST /stash/:path or /stashes/:path

# File lib/sensu/api/routes/stashes.rb, line 9
def post_stash
  path = parse_uri(STASH_URI).first
  read_data do |data|
    @redis.set("stash:#{path}", Sensu::JSON.dump(data)) do
      @redis.sadd("stashes", path) do
        @response_content = {:path => path}
        created!
      end
    end
  end
end
post_stashes() click to toggle source

POST /stashes

# File lib/sensu/api/routes/stashes.rb, line 82
def post_stashes
  rules = {
    :path => {:type => String, :nil_ok => false},
    :content => {:type => Hash, :nil_ok => false},
    :expire => {:type => Integer, :nil_ok => true}
  }
  read_data(rules) do |data|
    stash_key = "stash:#{data[:path]}"
    @redis.set(stash_key, Sensu::JSON.dump(data[:content])) do
      @redis.sadd("stashes", data[:path]) do
        @response_content = {:path => data[:path]}
        if data[:expire]
          @redis.expire(stash_key, data[:expire]) do
            created!
          end
        else
          created!
        end
      end
    end
  end
end