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
# File 'lib/postbin/storage.rb', line 6

def initialize(pstore_file)
  # setup file database for storage.
  @db = PStore.new(pstore_file)
end

Instance Method Details

#hitsObject

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



26
27
28
29
30
# File 'lib/postbin/storage.rb', line 26

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

#posts(url) ⇒ Object

Returns array of posts for the given url.



40
41
42
43
44
# File 'lib/postbin/storage.rb', line 40

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

#store(url, post) ⇒ Object

Store a post in the database.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/postbin/storage.rb', line 12

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
end

#urlsObject

Returns array of urls that have been posted to.



33
34
35
36
37
# File 'lib/postbin/storage.rb', line 33

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