Class: PostBin::Storage
- Inherits:
-
Object
- Object
- PostBin::Storage
- Defined in:
- lib/postbin/storage.rb
Overview
Storage backend for PostBin, uses PStore under the hood.
Instance Method Summary (collapse)
-
- (Object) hits
Returns hash, key being url and value being number of posts received.
-
- (Storage) initialize(pstore_file)
constructor
A new instance of Storage.
-
- (Object) posts(url)
Returns array of posts for the given url.
-
- (Object) store(url, post)
Store a post in the database.
-
- (Object) urls
Returns array of urls that have been posted to.
Constructor Details
- (Storage) initialize(pstore_file)
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
- (Object) hits
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 |
- (Object) posts(url)
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 |
- (Object) store(url, post)
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 |
- (Object) urls
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 |