Class: Bijou::Console::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/bijou/console/adapter.rb

Overview

This adapter sets up an approximation of a web server environment, so that Bijou can be run from the command line. This permits simple testing and experimentation. It also allows for the use of certain debugging tools and techniques that would otherwise not be available.

Class Method Summary collapse

Class Method Details

.diagnostic(message, diagnostic) ⇒ Object



147
148
149
150
# File 'lib/bijou/console/adapter.rb', line 147

def self.diagnostic(message, diagnostic)
  puts "#{message}:\n#{diagnostic}"
  return nil
end

.error(message) ⇒ Object



142
143
144
145
# File 'lib/bijou/console/adapter.rb', line 142

def self.error(message)
  puts "error: #{message}"
  return nil
end

.handleObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bijou/console/adapter.rb', line 20

def self.handle
  opts = GetoptLong.new(
    [ "--config" , "-c",            GetoptLong::OPTIONAL_ARGUMENT ],
    [ "--query",   "-q",            GetoptLong::REQUIRED_ARGUMENT ],
    [ "--help",    "-h", "-?",      GetoptLong::NO_ARGUMENT ]
  )

  debug = false
  t0 = Time.now

  document_root = ENV['DOCUMENT_ROOT']

  path_info = ENV['PATH_INFO']
  bijou_cache = ENV['BIJOU_CACHE']
  bijou_config = ENV['BIJOU_CONFIG']

  query_string = ''

  # process the parsed options
  opts.each do |opt, arg|
    case opt
    when '--config'
      bijou_config = arg
    when '--query'
      query_string = arg
    when '--help'
      return usage()
    end
  end

  if ARGV.length == 1
    path_info = ARGV[0]
  elsif !document_root
    puts "Missing document argument (try --help)"
    return 0
  end

  if bijou_config
    begin
      config = Bijou::Config.load_file(bijou_config)
    rescue SyntaxError
      error = $!.to_s
      error << "\nStack: " + $@.join("\n")

      return self::diagnostic("Error loading configuration.", error)
    end

    if (!config)
      return self::error("The configuration file path is invalid.")
    end
  else
    config = Bijou::Config.new
  end

  # The config file overrides the environment settings.
  if !config.document_root || config.document_root.empty?
    if document_root
      config.document_root = document_root
    else
      config.document_root = Dir.getwd
    end
  end

  if !config.cache_root && bijou_cache
    config.cache_root = bijou_cache
  end

  # The config file overrides the environment cache root value
  if !config.cache_root && bijou_cache
    config.cache_root = bijou_cache
  end

  # No need to defer trace render when outside of an HTTP environment.
  config.trace_buffer = false

  config.includes.each do |inc|
    $:.push inc
  end

  processor = Bijou::Processor.new

  # Build the page from the request.
  begin
    context = processor.load(path_info, config)
  rescue Exception
    msg = Bijou::ErrorFormatter.format_error :text, 4, context
    return self::diagnostic("Error loading page", msg)
  end

  #
  # Prepare the request data.
  #

  context.request = Bijou::Console::Request.new(query_string)
  context.response = Bijou::HttpResponse.new

  args = {}
  args.replace(context.request.params);

  # Render the result of executing the page.
  begin
    context.render(args)
  rescue Exception
    msg = Bijou::ErrorFormatter.format_error :text, 4, context
    return self::diagnostic("Syntax error loading page", msg)
  end

  puts context.output;

  t3 = Time.now

  if debug 
    puts context.get_log

    if config.trace_level > 0
      # print_environment
      puts context.get_trace
    end
  end

end


152
153
154
155
156
# File 'lib/bijou/console/adapter.rb', line 152

def self.print_environment()
  print ENV.collect() do |key, value|
    key + " --> " + value + "\n"
  end.join("")
end

.usageObject



158
159
160
161
162
163
164
# File 'lib/bijou/console/adapter.rb', line 158

def self.usage()
  puts "Renders a Bijou console to stdout.\n\n"
  puts "#{File.basename($0)} filename\n\n"
  puts "  -h, --help         This help message"
  puts "  -c, --config file  Specify a config file"
  puts '  -q, --query args   Pass query string arguments (e.g, "a=1&b=2")'
end