Class: KindleUtil::CLI

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/kindle_util/cli.rb

Constant Summary collapse

ACTIONS =
{
  "list" => "Display the selected items",
  "reset_lpr" => "Reset the last page read marker"
}

Instance Method Summary collapse

Instance Method Details

#executeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kindle_util/cli.rb', line 41

def execute
  Logging.logger.root.appenders = Logging.appenders.stdout
  Logging.logger.root.level = debug? ? :debug : :info
  
  self.username ||= ask("Enter amazon username: ")
  self.password ||= ask("Enter amazon password: ") { |q| q.echo = false }
  
  crawler = AmazonCrawler.new(username, password, cache?)
  
  items = crawler.owned_items
  filter_list.each do |name, pattern|
    items = items.select {|item| item[name] =~ pattern }
  end
  
  case action
    when "list"
      items.each  do |item|
        puts debug? ? item.pretty_inspect : format_item(item)
      end
    when "reset_lpr"
      items.each do |item|
        logger.info "Resetting last page read for: #{format_item(item)}"
        crawler.reset_lpr(item)
      end
    else
      logger.error "Unknown action: #{action}"
  end
end

#filter_list=(filters) ⇒ Object



70
71
72
# File 'lib/kindle_util/cli.rb', line 70

def filter_list=(filters)
  @filter_list = filters.collect {|f| f.split('=') }.collect {|k,v| [k, /#{v}/]}
end

#format_item(item) ⇒ Object



74
75
76
# File 'lib/kindle_util/cli.rb', line 74

def format_item(item)
  "[#{item['asin']}] #{item['title']} (#{item['author']})"
end