Class: NEC::MockServer::Application
- Inherits:
-
Object
- Object
- NEC::MockServer::Application
- Defined in:
- lib/nec_mock_server/mock_server.rb
Overview
Class represent trivial application to handle requests by defined router.
Constant Summary collapse
- MATCHERS =
{ uaf: /:(?<port>\d+)(\/(?<product>[\-\w]+)){0,1}(\/(?<tid>\w+)\-(?<awid>\w+)){0,1}(?<resource>(\/\w+)+)(\?(?<parameters>.*)){0,1}/, uu8: /:(?<port>\d+)\/{0,1}(?<territory>[0-9A-z\-]*)\/(?<product>[\w\-]+)\/(?<resource>\w+\/\w+)\/exec\?(?<parameters>.*)/ }
- REGISTER_AWID_RESOURCE =
'/sys/initAppWorkspace'
Instance Attribute Summary collapse
-
#asids ⇒ Object
readonly
Returns the value of attribute asids.
-
#awids ⇒ Object
readonly
Returns the value of attribute awids.
Instance Method Summary collapse
-
#call(env) ⇒ Object
The method catch request and process it.
-
#get_identity(authorization) ⇒ Object
The method helps to get identity information from Bearer token.
-
#initialize(router, opts = {}) ⇒ Application
constructor
The method create new instance of Application.
-
#serve_request(request, identity = nil) ⇒ Object
The method handle received request.
Constructor Details
#initialize(router, opts = {}) ⇒ Application
The method create new instance of Application
60 61 62 63 64 65 66 |
# File 'lib/nec_mock_server/mock_server.rb', line 60 def initialize(router, opts = {}) @router = router @only_registered_awid = opts[:only_registered_awid] @url_parts_matcher = opts.fetch(:matcher) {MATCHERS[:uaf]} @awids = opts.fetch(:awids) {{}} @asids = opts.fetch(:asids) {{}} end |
Instance Attribute Details
#asids ⇒ Object (readonly)
Returns the value of attribute asids.
49 50 51 |
# File 'lib/nec_mock_server/mock_server.rb', line 49 def asids @asids end |
#awids ⇒ Object (readonly)
Returns the value of attribute awids.
49 50 51 |
# File 'lib/nec_mock_server/mock_server.rb', line 49 def awids @awids end |
Instance Method Details
#call(env) ⇒ Object
The method catch request and process it
72 73 74 75 76 |
# File 'lib/nec_mock_server/mock_server.rb', line 72 def call(env) request = Rack::Request.new(env) identity = get_identity(env['HTTP_AUTHORIZATION']) serve_request(request, identity) end |
#get_identity(authorization) ⇒ Object
The method helps to get identity information from Bearer token
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/nec_mock_server/mock_server.rb', line 82 def get_identity() if begin token = .split('Bearer ')[1] parts = token.split('.') JSON.parse(Base64.decode64(parts[1]), symbolize_names: true) rescue => e warn e. end end end |
#serve_request(request, identity = nil) ⇒ Object
The method handle received request.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/nec_mock_server/mock_server.rb', line 98 def serve_request(request, identity = nil) parts = get_url_parts(request.url) begin io = request.body data = io.read ensure io.close if io && io.respond_to?(:close) && io.respond_to?(:closed?) && !io.closed? end if parts[:resource] && parts[:resource] == REGISTER_AWID_RESOURCE process_sys_init_app_workspace(parts, data) end if @only_registered_awid return @router.no_registered_awid(parts[:tid], parts[:awid]) unless is_registered_resource?(parts[:tid], parts[:awid]) end @router.route(parts, data, identity) end |