Skip to content

Commit

Permalink
Add support for :database in ~/.itunesrc for "import" and "report"
Browse files Browse the repository at this point in the history
commands.
  • Loading branch information
Alex Vollmer committed Sep 16, 2009
1 parent 94188f4 commit 587cf56
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
17 changes: 13 additions & 4 deletions lib/appstore/commands/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

module AppStore::Commands
class Import # :nodoc:
def initialize(c)
c.req('b', 'db', :desc => 'Dump report to sqlite DB at the given path')
def initialize(c, rcfile=File.expand_path("~/.itunesrc"))
c.opt('b', 'db', :desc => 'Dump report to sqlite DB at the given path')
c.req('f', 'file', :desc => 'The file to import, - means standard in')
@rcfile = rcfile
end

def execute!(opts, args=[])
raise ArgumentError.new("Missing :db option") if opts.db.nil?
db = if opts.db
opts.db
elsif File.exist?(@rcfile)
rc = YAML.load_file(@rcfile)
File.expand_path(rc[:database])
else
nil
end
raise ArgumentError.new("Missing :db option") unless db
raise ArgumentError.new("Missing :file option") if opts.file.nil?
store = AppStore::Store.new(opts.db, opts.verbose?)
store = AppStore::Store.new(db, opts.verbose?)
input = opts.file == '-' ? $stdin : open(opts.file, 'r')
count = 0
AppStore::Report.new(input).each do |entry|
Expand Down
18 changes: 14 additions & 4 deletions lib/appstore/commands/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module AppStore::Commands
class Report # :nodoc:
def initialize(c)
c.req('b', 'db', :desc => 'Dump report to sqlite DB at the given path')
def initialize(c, rcfile=File.expand_path("~/.itunesrc"))
c.opt('b', 'db', :desc => 'Dump report to sqlite DB at the given path')
c.opt('c', 'country',
:desc => 'A two-letter country code to filter results with')
c.opt('f', 'from', :desc => 'The starting date, inclusive') do |f|
Expand All @@ -13,11 +13,21 @@ def initialize(c)
Date.parse(t)
end
c.flag('g', 'group', :desc => 'Group results by country code')
@rcfile = rcfile
end

def execute!(opts, args=[], out=$stdout)
raise ArgumentError.new("Missing :db option") if opts.db.nil?
store = AppStore::Store.new(opts.db)
db = if opts.db
opts.db
elsif @rcfile and File.exist?(@rcfile)
rc = YAML.load_file(@rcfile)
File.expand_path(rc[:database])
else
nil
end

raise ArgumentError.new("Missing :db option") if db.nil?
store = AppStore::Store.new(db)
params = {
:to => opts.to,
:from => opts.from,
Expand Down
4 changes: 2 additions & 2 deletions lib/appstore/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class AppStore::Report
# <tt>:install</tt>.
attr_reader :data

# Give me an +IO+-like object (one that responds to the +lines+
# Give me an +IO+-like object (one that responds to the +each+
# method) and I'll parse that sucker for you.
def initialize(input)
@data = Hash.new { |h,k| h[k] = { }}
input.lines.each do |line|
input.each.each do |line|
line.chomp!
next if line =~ /^(Provider|$)/
tokens = line.split(/\s+/)
Expand Down
7 changes: 4 additions & 3 deletions spec/commands/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe AppStore::Commands::Import do

before(:each) do
@cmd = AppStore::Commands::Import.new(mock(:null_object => true))
@cmd = AppStore::Commands::Import.new(mock(:null_object => true),
'/tmp/fake-itunesrc')
end

describe 'with valid execution arguments' do
Expand Down Expand Up @@ -40,7 +41,7 @@

it 'should reject missing :db option' do
lambda do
@cmd.execute!(stub(:file => '/tmp/report.txt', :db => nil))
@cmd.execute!(stub(:db => nil, :file => '/tmp/report.txt', :verbose? => false))
end.should raise_error(ArgumentError)
end
end
Expand All @@ -49,7 +50,7 @@

it 'should add appropriate options to a given Clip' do
clip = mock('Clip')
clip.should_receive(:req).
clip.should_receive(:opt).
with('b', 'db',
:desc => 'Dump report to sqlite DB at the given path')
clip.should_receive(:req).
Expand Down
2 changes: 1 addition & 1 deletion spec/commands/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
describe 'command-line option parsing' do
it 'should add appropriate options to a given Clip' do
clip = mock("Clip")
clip.should_receive(:req).
clip.should_receive(:opt).
with('b', 'db',
:desc => 'Dump report to sqlite DB at the given path')
clip.should_receive(:opt).
Expand Down

0 comments on commit 587cf56

Please sign in to comment.