Class: RightDevelop::Testing::Server::MightApi::App::Admin

Inherits:
Base
  • Object
show all
Defined in:
lib/right_develop/testing/servers/might_api/app/admin.rb

Defined Under Namespace

Classes: MightAdminError

Constant Summary collapse

CONFIG_CLASS =

convenince

::RightDevelop::Testing::Recording::Config

Constants inherited from Base

Base::DEFAULT_PROXY_SETTINGS, Base::MAX_REDIRECTS, Base::MUTEX

Instance Attribute Summary

Attributes inherited from Base

#config, #logger, #state_file_path

Instance Method Summary collapse

Methods inherited from Base

app_threads, #call, interrupted=, interrupted?

Constructor Details

#initialize(options = {}) ⇒ Admin

Returns a new instance of Admin.

See Also:



35
36
37
38
39
40
41
42
# File 'lib/right_develop/testing/servers/might_api/app/admin.rb', line 35

def initialize(options = {})
  super
  fail "Unexpected mode: #{config.mode}" unless config.mode == :admin

  # admin has no state to preserve and no fixtures of its own so it can
  # immediately cleanup after reading the configuration file.
  cleanup
end

Instance Method Details

#cleanupObject

See Also:



78
79
80
81
82
# File 'lib/right_develop/testing/servers/might_api/app/admin.rb', line 78

def cleanup
  # cleanup handlers, if any.
  (@route_handlers || []).each { |handler| handler.cleanup }
  super
end

#handle_request(env, verb, uri, headers, body) ⇒ Object



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
# File 'lib/right_develop/testing/servers/might_api/app/admin.rb', line 45

def handle_request(env, verb, uri, headers, body)
  # check routes.
  unless admin_route = find_admin_route(uri)
    raise MissingRoute, "No route configured for #{uri.path.inspect}"
  end
  route_path, route_data = admin_route
  case route_data
  when Base
    return route_data.handle_request(env, verb, uri, headers, body)
  when :configure
    case verb
    when 'GET'
      if @route_handlers
        return [
          200,
          { 'connection' => 'close', 'content-type' => 'application/x-yaml' },
          (@route_handlers || []).map { |h| ::YAML.dump(h.config.to_hash) }
        ]
      else
        return [204, { 'connection' => 'close' }, ['']]
      end
    when 'POST', 'PUT'
      return configure_known_routes(CONFIG_CLASS.new(::YAML.load(body)))
    else
      raise MightAdminError, "Wrong verb: #{verb}"
    end
  else
    raise MissingRoute,
          "No handler for configured administrator route: #{route_path}"
  end
end