Class: SpsBill::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/sps_bill/shell.rb

Constant Summary collapse

OPTIONS =

command line options definition

%w(help verbose csv+ raw+ data=s)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Shell

new



28
29
30
31
# File 'lib/sps_bill/shell.rb', line 28

def initialize(options)
  @fileset = ARGV
  @options = (options||{}).each{|k,v| {k => v} }
end

Instance Attribute Details

#filesetObject

Returns the value of attribute fileset.



2
3
4
# File 'lib/sps_bill/shell.rb', line 2

def fileset
  @fileset
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/sps_bill/shell.rb', line 2

def options
  @options
end

Class Method Details

.usageObject

Usage message



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sps_bill/shell.rb', line 8

def self.usage
  puts <<-EOS

SP Services Bill Scanner v#{SpsBill::Version::STRING}
===================================

Usage:
sps_bill [options] file-spec

Command Options
-r  | --raw    raw data format (without headers)
-c  | --csv    output in CSV format (default)
-d= | --data=[charges,electricity,gas,water,all]

file-spec is a path to the PDF bill(s) to read.

  EOS
end

Instance Method Details

#billsObject



53
54
55
# File 'lib/sps_bill/shell.rb', line 53

def bills
  @bills ||= SpsBill::BillCollection.load(fileset)
end

#export(dataset_selector) ⇒ Object



57
58
59
60
# File 'lib/sps_bill/shell.rb', line 57

def export(dataset_selector)
  format_header bills.headers(dataset_selector)
  format_rows bills.send(dataset_selector)
end

#format_header(data) ⇒ Object



68
69
70
71
# File 'lib/sps_bill/shell.rb', line 68

def format_header(data)
  return if options[:raw]
  puts data.join(',')
end

#format_rows(data) ⇒ Object



62
63
64
65
66
# File 'lib/sps_bill/shell.rb', line 62

def format_rows(data)
  data.each do |row|
    puts row.join(',')
  end
end

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sps_bill/shell.rb', line 33

def run
  if options[:help] or fileset.empty?
    self.class.usage
    return
  end
  case options[:data]
  when /^c/
    export(:total_amounts)
  when /^e/
    export(:electricity_usages)
  when /^g/
    export(:gas_usages)
  when /^w/
    export(:water_usages)
  # when /^a/
  else
    export(:all_data)
  end
end