Class: Tendersync::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/tendersync/runner.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
69
70
71
72
73
74
# File 'lib/tendersync/runner.rb', line 8

def initialize argv
  @dry_run  = false
  @sections = []
  @groups = []
  settings['groups'] ||= []
  @parser = OptionParser.new do |op|
    op.banner += " command\n"
    op.on('-n',                                  "dry run" )       { @dry_run  = true }
    op.on('-s', '--section', '=SECTION', String, "section, specify multiple separately" ) { |s| @sections << s }
    op.on('-u', '--username','=EMAIL',   String, "* login e-mail" ) {|str| settings['username'] = str }
    op.on('-p', '--password','=PASS',    String, "* password" )     {|str| settings['password'] = str }
    op.on(      '--docurl',  '=URL',     String, "* tender site URL" ) { |dir| settings['docurl'] = dir }
    op.separator ""
    op.separator "Indexing Options:"
    op.on(       '--group',   '=TITLE;regex',  String, "*map of regex to group title for TOC groups") do | g |
      pair = g.split(';')
      settings['groups'] << [pair.first, pair.last]
    end
    op.on(       '--name',  '=PERMALINK', String, "override the default name of the index page") { |n| @index_name = n }
    op.on(       '--depth', '=DEPTH',     String, "*Number of levels to descend into a document being indexed") { | g | settings['depth'] = g.to_i }
          
      %Q{
  * saved in .tendersync file for subsequent default
 
  Commands:

      pull [URL, URL...]   -- download documents from tender; specify sections with -s, a page URL, or
                              nothing to download all documents
      index                -- create a master index of each section, writing to section/file; specify
                              the sections with -s options; you can organize the TOC into groups by
                              mapping document titles to groups via a regular expression with -g options
      ls                   -- list files in specified session
      post PATTERN         -- post the matching documents to tender; use /regexp/ or glob
      irb                  -- drops you into IRB with a tender session & related classes (for hacking/
                              one-time tasks).  Programmers only.
      create PERMALINK [ title ]
                           -- create a new tender document with the specified permalink in the section
                              specified by --section=... (must be only one.) 
                              
Version #{Tendersync::VERSION}

  }.split(/\n/).each {|line| op.separator line.chomp }
  end
  
  begin
    @command,*@args = *@parser.parse(argv)
  rescue OptionParser::InvalidOption => e
    raise Error, e.message
  end
  
  @username = settings['username']
  @password = settings['password']
  @dochome = settings['docurl'] && settings['docurl'] =~ /^(http.*?)\/?$/ && $1
  @root = settings['root']
  
  case
  when ! @username
    raise Error, "Please enter a username and password.  You only need to do this once."
  when ! @password
    raise Error, "Please enter a password.  You only need to do this once."
  when ! @dochome
    raise Error, "Please enter a --docurl indicating the home page URL of your Tender docs.\n" +
         "You only need to do this once."
  else
    settings.save!
  end
end

Class Method Details

.save!Object



212
213
214
215
216
# File 'lib/tendersync/runner.rb', line 212

def @settings.save!
  File.open(".tendersync","w") do |f|
    f.write(self.to_yaml)
  end
end

Instance Method Details

#runObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tendersync/runner.rb', line 76

def run
  $session = Tendersync::Session.new @dochome, @username, @password
  $dry_run = @dry_run
  case @command || 'help'
  when 'help'
    raise Error, @parser.to_s
  when *%w[pull post create irb ls index]
    send @command
  else
    raise Error, "Unknown command: #{@command}\n\n#{@parser}"
  end
end