Class: PostBin::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/postbin/storage.rb

Overview

Storage backend for PostBin, uses PStore under the hood.

Instance Method Summary collapse

Constructor Details

#initialize(pstore_file) ⇒ Storage

Returns a new instance of Storage.



6
7
8
9
10
# 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

Instance Method Details

#hitsObject

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



28
29
30
31
32
# File 'lib/postbin/storage.rb', line 28

def hits
  @db.transaction(true) do
    @db['urls'] || {}
  end
end

#posts(url) ⇒ Object

Returns array of posts for the given url.



42
43
44
45
46
# File 'lib/postbin/storage.rb', line 42

def posts(url)
  @db.transaction(true) do
    @db[url] || []
  end
end

#store(url, post) ⇒ Object

Store a post in the database.



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

#urlsObject

Returns array of urls that have been posted to.



35
36
37
38
39
# File 'lib/postbin/storage.rb', line 35

def urls
  @db.transaction(true) do
    (@db['urls'] || {}).keys
  end
end