class ConcertoConfigServer
Constants
- ADDRESSING_METHODS
… and available layer-3 addressing methods.
- CONNECTION_METHODS
push these over to netconfig.rb? Our list of available physical-layer connection methods…
- LOCALHOSTS
Hosts we allow to access configuration without authenticating.
Public Instance Methods
# File lib/bandshell/application/app.rb, line 43 def active_page?(path='') request.path_info == '/' + path end
Get our base URL from wherever it may be stored.
# File lib/bandshell/application/app.rb, line 122 def concerto_url Bandshell::ConfigStore.read_config('concerto_url', '') end
Set the arguments on an instance of a given configuration class. This uses the safe_assign method that should be present in all configuration classes to determine which values are allowed to be passed via form fields (i.e. which ones are subject to validation)
# File lib/bandshell/application/app.rb, line 333 def do_assign(params, instance) safe = instance.safe_assign params.each do |param, value| if safe.include? param.intern instance.send((param + '=').intern, value) end end end
Extract arguments from a set of form data that are intended to go to a specific network configuration class. These fields have names of the form ‘ClassName/field_name’; this function returns a hash in the form { ‘field_name’ => ‘value } containing the configuration arguments.
# File lib/bandshell/application/app.rb, line 317 def extract_class_args(params, target_class) result = { } params.each do |key, value| klass, arg = key.split('/', 2) if klass == target_class result[arg] = value end end result end
Try to figure out what our current IPv4 address is and return it as a string.
# File lib/bandshell/application/app.rb, line 128 def my_ip iface = Bandshell.configured_interface if iface iface.ip else "Network setup failed or bad configuration" end end
Try to figure out what our current port is and return it as a string.
# File lib/bandshell/application/app.rb, line 139 def my_port settings.port end
Check if we have something resembling a network connection. This means we found a usable interface and it has an IPv4 address.
# File lib/bandshell/application/app.rb, line 145 def network_ok return true if settings.no_netconfig iface = Bandshell.configured_interface if iface if iface.ip != "0.0.0.0" true else false end else false end end
Given the name of a class, pick a class out of a list of allowed classes. This is used for parsing the form input for network configuration.
# File lib/bandshell/application/app.rb, line 309 def pick_class(name, list) list.find { |klass| klass.basename == name } end
# File lib/bandshell/application/app.rb, line 47 def player_info # Note: probably not thread-safe. @@player_info ||= Bandshell::PlayerInfo.new end
Enforce authentication on actions. Calling from within an action will check authentication and return 401 if unauthorized.
# File lib/bandshell/application/app.rb, line 89 def protected! unless authorized? response['WWW-Authenticate'] = \ %(Basic realm="Concerto Configuration") throw(:halt, [401, "Not authorized\n"]) end end
# File lib/bandshell/application/app.rb, line 97 def request_is_local? ip = IPAddress.parse(request.env['REMOTE_ADDR']) LOCALHOSTS.include? ip end
Check if we can retrieve a URL and get a 200 status code.
# File lib/bandshell/application/app.rb, line 160 def validate_url(url) begin # this will fail with Errno::something if server # can't be reached uri = URI(url) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| request = Net::HTTP::Get.new uri.request_uri response = http.request request # also bomb out if we don't get an OK response # maybe demanding 200 is too strict here? if response.code != "200" fail end end # if we get here we have a somewhat valid URL to go to true rescue # our request bombed out for some reason false end end
Get the return value of the method on obj if obj supports the method. Otherwise return the empty string. This is useful in views where arguments may be of diverse types.
# File lib/bandshell/application/app.rb, line 78 def value_from(obj, method) if obj.respond_to? method obj.send method else "" end end