Class: Pickpocket::Articles::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/pickpocket/articles/library.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLibrary

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

#apiObject (readonly)

Returns the value of attribute api.



8
9
10
# File 'lib/pickpocket/articles/library.rb', line 8

def api
  @api
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/pickpocket/articles/library.rb', line 7

def logger
  @logger
end

#storeObject (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_inventoryObject



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

Parameters:

  • quantity. (Integer)

    Quantity of articles to open.



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

#renewObject

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] = Hash[new_unread]
    store[:read]   = {}
  end
end

#statsObject

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