Class: Brief::Server::Gateway
- Inherits:
-
Object
- Object
- Brief::Server::Gateway
- Defined in:
- lib/brief/server/gateway.rb
Instance Attribute Summary collapse
-
#briefcases ⇒ Object
readonly
Returns the value of attribute briefcases.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #briefcase_options ⇒ Object
- #call(env) ⇒ Object
-
#initialize(options = {}) ⇒ Gateway
constructor
A new instance of Gateway.
- #load_briefcases ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Gateway
Returns a new instance of Gateway.
4 5 6 7 8 9 |
# File 'lib/brief/server/gateway.rb', line 4 def initialize(={}) @root = .fetch(:root) @briefcases = {}.to_mash @briefcase_options = .fetch(:briefcase_options, {}) load_briefcases end |
Instance Attribute Details
#briefcases ⇒ Object (readonly)
Returns the value of attribute briefcases.
2 3 4 |
# File 'lib/brief/server/gateway.rb', line 2 def briefcases @briefcases end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
2 3 4 |
# File 'lib/brief/server/gateway.rb', line 2 def root @root end |
Instance Method Details
#briefcase_options ⇒ Object
11 12 13 |
# File 'lib/brief/server/gateway.rb', line 11 def (@briefcase_options || {}) end |
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/brief/server/gateway.rb', line 26 def call(env) request = Rack::Request.new(env) params = request.params.symbolize_keys if request.path.match(/\/all$/) presenter = params.fetch(:presenter, 'default') return [200, {}, [ @briefcases.values.map do |bc| bc.present(presenter, params) end.to_json ]] end name = request.path.match(/\/\w+\/(\w+)/)[1] rescue nil if name && @briefcases[name] @briefcases[name].server.call(env) else [404, {}, ["Not found: #{ name } -- #{ request.path }"]] end end |
#load_briefcases ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/brief/server/gateway.rb', line 15 def load_briefcases config_path = .fetch(:config_path, "brief.rb") root.children.select(&:directory?).each do |dir| if dir.join(config_path).exist? slug = dir.basename.to_s.parameterize @briefcases[slug] ||= Brief::Briefcase.new(.merge(root: dir)) end end end |