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.
Class Method Summary collapse
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.
13 14 15 16 17 18 |
# File 'lib/brief/server/gateway.rb', line 13 def initialize(={}) @root = .fetch(:root) @briefcases = {}.to_mash = .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 |
Class Method Details
.start(options = {}) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/brief/server/gateway.rb', line 4 def self.start(={}) app = new(root: Pathname([:root])) port = .fetch(:port, 9094) host = .fetch(:host, '0.0.0.0') Rack::Handler::Thin.run(app, Port: port, Host: host) end |
Instance Method Details
#briefcase_options ⇒ Object
20 21 22 |
# File 'lib/brief/server/gateway.rb', line 20 def ( || {}) end |
#call(env) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/brief/server/gateway.rb', line 35 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
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/brief/server/gateway.rb', line 24 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 |