Commits

Koen Van der Auwera committed 85386f0

keys, post, random, last and all posts methods + tests

  • Participants
  • Parent commits 8796b99

Comments (0)

Files changed (2)

File amanda/store.rb

 
   class Store
 
+    POST_KEY_PREFIX = "post:"
+    POSTS_KEY = "posts"
+    POSTS_LAST_KEY = "posts:last"
+    POSTS_ALL_KEY = "posts:all"
+
     attr_accessor :config_file
 
     def initialize(config_file) @config_file = config_file; end;
                              port: config.fetch("redis").fetch("port", 6379))
     end
 
+    def keys(pattern="*")
+      redis.keys(pattern)
+    end
+
+    def post(redis_post_id)
+      Post.from_json(redis.get(redis_post_id))
+    end
+
+    def posts
+      redis.lrange(POSTS_ALL_KEY, 0, -1).map{|p| post(p)}
+    end
+
+    def random
+      post(redis.srandmember(POSTS_KEY, 1))
+    end
+
+    def last
+      post(redis.get("posts:last"))
+    end
+
     def read_posts_from_disk
       posts = []
       Dir.glob(File.join(path, "*")).each do |post_file|
         if post_file =~ /\d{8}-\d{4}\.md$/
-          posts << Amanda::Post.parse(post_file)
+          posts << Post.parse(post_file)
         end
       end
       posts
 
     def store_posts_in_redis
       read_posts_from_disk.each do |post|
-        redis.set "post:#{post.id}", post.to_json
-        redis.sadd "posts", "post:#{post.id}"
-        redis.set "posts:last", "post:#{post.id}"
+        redis.set "#{POST_KEY_PREFIX}#{post.id}", post.to_json
+        redis.sadd POSTS_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+        redis.set POSTS_LAST_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+        redis.rpush POSTS_ALL_KEY, "#{POST_KEY_PREFIX}#{post.id}"
       end
     end
 

File test/store_test.rb

   it "should store posts in redis" do
     store = Amanda::Store.new("amanda_test.yml")
     store.store_posts_in_redis
-    assert first_key = store.redis.keys("post:*").first
+    assert first_key = store.keys("#{Amanda::Store::POST_KEY_PREFIX}*").first
     post = Amanda::Post.from_json(store.redis.get(first_key))
-    assert_equal first_key, "post:#{post.id}"
+    assert_equal first_key, "#{Amanda::Store::POST_KEY_PREFIX}#{post.id}"
   end
+
+  it "should return one post" do
+    store = Amanda::Store.new("amanda_test.yml")
+    store.store_posts_in_redis
+    assert first_key = store.keys("#{Amanda::Store::POST_KEY_PREFIX}*").first
+    assert store.post(first_key)
+  end
+
+  it "should return random post" do
+    store = Amanda::Store.new("amanda_test.yml")
+    store.store_posts_in_redis
+    assert first_key = store.keys("#{Amanda::Store::POST_KEY_PREFIX}*").first
+    assert store.random
+  end
+
+  it "should return all posts" do
+    store = Amanda::Store.new("amanda_test.yml")
+    store.store_posts_in_redis
+    assert store.posts.any?
+  end
+
+  it "should return last post" do
+    store = Amanda::Store.new("amanda_test.yml")
+    store.store_posts_in_redis
+    assert first_key = store.keys("#{Amanda::Store::POST_KEY_PREFIX}*").first
+    assert store.last
+  end
+
 end