Commits

Koen Van der Auwera  committed 6f9d752

from dropbox to redis

  • Participants
  • Parent commits 587a716

Comments (0)

Files changed (4)

     end
   end
 
+  class Refresh < R '/refresh'
+    def get
+      STORE.refresh_from_dropbox
+      redirect "/"
+    end
+  end
+
   class Archive < R '/archive'
     def get
       render :archive

File amanda/post.rb

       @tags.split(",").map(&:strip) if @tags
     end
 
-    def self.parse(filename)
+    def self.parse(filename, contents="")
+      post = Post.new(File.basename(filename, ".md"))
+      contents = contents.force_encoding("ISO-8859-1").encode("utf-8", replace: nil)
+      contents.split("\n").each do |line|
+        unless head = match_header(post, line)
+          post.content << line rescue post.content = line
+        end
+      end
+      post
+    end
+
+    def self.parse_from_file(filename)
       post = Post.new(File.basename(filename, ".md"))
       IO.readlines(filename).each do |line|
         line = line.force_encoding("ISO-8859-1").encode("utf-8", replace: nil)

File amanda/store.rb

       post(redis.get("posts:last"))
     end
 
+    def read_posts_from_dropbox
+      posts = []
+      client = DropboxClient.new(dropbox_session, :app_folder)
+      client.metadata('/')["contents"].map{|f| f["path"]}.select{|f| f =~ /\d{8}-\d{4}\.md$/}.each do |path|
+        contents, meta = client.get_file_and_metadata(path)
+        posts << Post.parse(path, contents)
+      end
+      posts
+    end
+
+    def refresh_from_dropbox
+      store_posts_in_redis(read_posts_from_dropbox)
+    end
+
+    def refresh_from_disk
+      store_posts_in_redis(read_posts_from_disk)
+    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 << Post.parse(post_file)
+          posts << Post.parse_from_file(post_file)
         end
       end
       posts
     end
 
-    def store_posts_in_redis
-      rposts = read_posts_from_disk
+    def store_posts_in_redis(rposts)
       if rposts.any?
-        redis.flushdb
+        redis.del(POSTS_ALL_KEY)
         rposts.each do |post|
           redis.set "#{POST_KEY_PREFIX}#{post.id}", post.to_json
           redis.sadd POSTS_KEY, "#{POST_KEY_PREFIX}#{post.id}"

File test/store_test.rb

 
   it "should store posts in redis" do
     store = Amanda::Store.new("amanda_test.yml")
-    store.store_posts_in_redis
+    store.refresh_from_disk
     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, "#{Amanda::Store::POST_KEY_PREFIX}#{post.id}"
 
   it "should return one post" do
     store = Amanda::Store.new("amanda_test.yml")
-    store.store_posts_in_redis
+    store.refresh_from_disk
     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
+    store.refresh_from_disk
     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
+    store.refresh_from_disk
     assert store.posts.any?
   end
 
   it "should return last post" do
     store = Amanda::Store.new("amanda_test.yml")
-    store.store_posts_in_redis
+    store.refresh_from_disk
     assert first_key = store.keys("#{Amanda::Store::POST_KEY_PREFIX}*").first
     assert store.last
   end