class PostBin::Storage

Storage backend for PostBin, uses PStore under the hood.

Public Class Methods

new(pstore_file) click to toggle source
# File lib/postbin/storage.rb, line 6
def initialize(pstore_file)
  # setup file database for storage.
  @db = PStore.new(pstore_file)
  @db.ultra_safe = true if @db.respond_to?(:ultra_safe)
end

Public Instance Methods

hits() click to toggle source

Returns hash, key being url and value being number of posts received.

# File lib/postbin/storage.rb, line 28
def hits
  @db.transaction(true) do
    @db['urls'] || {}
  end
end
posts(url) click to toggle source

Returns array of posts for the given url.

# File lib/postbin/storage.rb, line 42
def posts(url)
  @db.transaction(true) do
    @db[url] || []
  end
end
store(url, post) click to toggle source

Store a post in the database.

# File lib/postbin/storage.rb, line 13
def store(url, post)
  @db.transaction do
    # init if no data exists.
    @db['urls'] ||= {}
    @db['urls'][url] ||= 0
    @db[url] ||= []
    # incr hit count.
    @db['urls'][url] += 1
    # store post.
    @db[url] << post
  end
  true
end
urls() click to toggle source

Returns array of urls that have been posted to.

# File lib/postbin/storage.rb, line 35
def urls
  @db.transaction(true) do
    (@db['urls'] || {}).keys
  end
end