Class: Pickpocket::Articles::Library
- Inherits:
-
Object
- Object
- Pickpocket::Articles::Library
- Defined in:
- lib/pickpocket/articles/library.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #guarantee_inventory ⇒ Object
-
#initialize ⇒ Library
constructor
A new instance of Library.
-
#pick(quantity = 1) ⇒ Object
Select an unread article, put it to the read collection and return this article.
-
#renew ⇒ Object
Replace unread store with content from pocket.
-
#stats ⇒ Object
Show stats from your local articles.
Constructor Details
#initialize ⇒ Library
Returns a new instance of Library.
15 16 17 18 19 |
# File 'lib/pickpocket/articles/library.rb', line 15 def initialize @api = API.new @logger = Pickpocket::Logger @store = yaml_store end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
8 9 10 |
# File 'lib/pickpocket/articles/library.rb', line 8 def api @api end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/pickpocket/articles/library.rb', line 7 def logger @logger end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
8 9 10 |
# File 'lib/pickpocket/articles/library.rb', line 8 def store @store end |
Instance Method Details
#guarantee_inventory ⇒ Object
21 22 23 24 25 26 |
# File 'lib/pickpocket/articles/library.rb', line 21 def guarantee_inventory store.transaction do store[:read] = {} if store[:read].nil? store[:unread] = {} if store[:unread].nil? end end |
#pick(quantity = 1) ⇒ Object
Select an unread article, put it to the read collection and return this article
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pickpocket/articles/library.rb', line 30 def pick(quantity = 1) guarantee_inventory store.transaction do quantity.times do unread = store[:unread] random_key = random_hash_key(unread) if (random_article = unread.delete(random_key)) store[:read].update({ random_key => random_article }) Launchy.open(random_article['given_url']) else logger.info 'You have read all articles!' end end end end |
#renew ⇒ Object
Replace unread store with content from pocket
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pickpocket/articles/library.rb', line 48 def renew guarantee_inventory store.transaction do already_read = store[:read] api.delete(already_read.keys) new_unread = api.retrieve['list'] store[:unread] = new_unread store[:read] = {} end end |
#stats ⇒ Object
Show stats from your local articles
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pickpocket/articles/library.rb', line 61 def stats guarantee_inventory store.transaction do unread_count = store[:unread].keys.size read_count = store[:read].keys.size logger.info %Q{You have #{read_count} read articles} logger.info %Q{You have #{unread_count} unread articles} end end |