class Pickpocket::Articles::Library

Attributes

api[R]
logger[RW]
store[R]

Public Class Methods

new() click to toggle source
# File lib/pickpocket/articles/library.rb, line 15
def initialize
  @api    = API.new
  @logger = Pickpocket::Logger
  @store  = yaml_store
end

Public Instance Methods

guarantee_inventory() click to toggle source
# 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) click to toggle source

Select an unread article, put it to the read collection and return this article @param [Integer] quantity. Quantity of articles to open.

# 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() click to toggle source

Replace unread store with content from pocket

# 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
stats() click to toggle source

Show stats from your local articles

# 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

Private Instance Methods

random_hash_key(unread_articles) click to toggle source

@param [Hash] unread_articles @return [String, NilClass]

# File lib/pickpocket/articles/library.rb, line 76
def random_hash_key(unread_articles)
  unread_articles.keys.sample
end
yaml_store() click to toggle source
# File lib/pickpocket/articles/library.rb, line 10
        def yaml_store
  FileUtils.mkdir_p(File.dirname(Pickpocket.config.library_file))
  YAML::Store.new(Pickpocket.config.library_file)
end