Commits

Koen Van der Auwera committed 475534c

intial commit

  • Participants

Comments (0)

Files changed (5)

+**.yml
+require_relative 'amanda/post'
+require_relative 'amanda/store'
+require 'json'
+
+module Amanda
+
+  class Post
+
+    attr_accessor :id, :title, :published, :date, :tags, :content
+
+    def initialize(id=nil)
+      @id = id
+    end
+
+    def self.from_json(data)
+      data = JSON.parse(data)
+      post = Post.new
+      %w(id title published date tags content).each do |attr|
+        post.send("#{attr}=", data[attr])
+      end
+      post
+    end
+
+    def to_json
+      {id: id, title: title, published: published, date: date, tags: tags, content: content}.to_json
+    end
+
+    def tags_to_arr
+      @tags.split(",").map(&:strip) if @tags
+    end
+
+    def self.parse(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)
+        unless head = match_header(post, line)
+          post.content << line rescue post.content = line
+        end
+      end
+      post
+    rescue => e
+      puts "Problem parsing #{filename}"
+      raise e
+    end
+
+    def self.match_header(obj, value)
+      case value
+      when /^title:/i then obj.title = value[6..-1].strip; true
+      when /^published:/i then obj.published = value[10..-1].strip; true
+      when /^date:/i then obj.date = value[5..-1].strip; true
+      when /^tags:/i then obj.tags = value[5..-1].strip; true
+      else
+        false
+      end
+    end
+
+  end
+end
+require 'yaml'
+require 'json'
+require 'redis'
+
+module Amanda
+
+  class Store
+
+    attr_accessor :config_file
+
+    def initialize(config_file) @config_file = config_file; end;
+
+    def config
+      @config ||= YAML.load_file(config_file)
+    end
+
+    def path
+      @path ||= config.fetch("posts_path")
+    end
+
+    def redis
+      @redis ||= ::Redis.new(host: config.fetch("redis").fetch("host", "localhost"),
+                             port: config.fetch("redis").fetch("port", 6379))
+    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)
+        end
+      end
+      posts
+    end
+
+    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 "post:last", "post:#{post.id}"
+      end
+    end
+
+  end
+
+end

test/store_test.rb

+require "minitest/autorun"
+require File.join(File.dirname(__FILE__), "..", "amanda")
+
+describe Amanda::Store do
+
+  it "should have a configured path" do
+    assert Amanda::Store.new("amanda_test.yml").path
+  end
+
+  it "should read all posts" do
+    store = Amanda::Store.new("amanda_test.yml")
+    posts = store.read_posts_from_disk
+    assert posts.size > 0
+    assert Amanda::Post === posts.first
+  end
+
+  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
+    post = Amanda::Post.from_json(store.redis.get(first_key))
+    assert_equal first_key, "post:#{post.id}"
+  end
+end