Module: KRL_COMMON

Defined in:
lib/common.rb

Class Method Summary collapse

Class Method Details

.get_app(version = "development") ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/common.rb', line 36

def self.get_app(version="development")
  begin
    root_dir = Dir.pwd
    raise "Please re-checkout your ruleset or make sure you are in the root directory of a ruleset." unless File.exists?(File.join(root_dir, ".app"))
    config_file = File.join(root_dir, '.app')
    raise "Unable to get the app information. You need to recheckout your app." unless File.exists?(config_file)
    config = YAML::load_file(config_file)
    user = KRL_CMD::User.new
    app = user.find_application(:application_id => config[:ruleset_id], :version => version)
    return app
  rescue Exception => e
    raise "Unable to get ruleset information. #{e.message}"
  end

end

.long_post_form(url, params) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/common.rb', line 52

def self.long_post_form(url, params)
  req = Net::HTTP::Post.new(url.path)
  req.form_data = params
  req.basic_auth url.user, url.password if url.user
  Net::HTTP.new(url.host, url.port).start {|http|
    http.read_timeout = 120
    http.request(req)
  }
end

.pretty_table(collection, fields, limit = 0, limit_last = false) ⇒ Object



6
7
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
# File 'lib/common.rb', line 6

def self.pretty_table(collection, fields, limit=0, limit_last=false)
  raise "Nothing to present" if collection.empty?
  ptable = table do |t|
    
    t.headings = fields.collect{ |h| h[:field] }
    counter = 0
    collection.each do |c|
      show_row = true

      if limit_last && limit > 0
        # This will make it so that the last items in the table are
        # shown rather than the first when a limit is specified.
        show_row = (counter + 1) > (collection.length - limit)
      end

      if show_row
        row = []
        fields.each do |f|
          row << f[:value].call(c) if show_row
        end
        t << row
      end
      counter += 1
      break if limit > 0 && limit == counter && ! limit_last
    end
    
  end
  puts ptable
end