require 'helix_versioning_engine/util' module HelixVersioningEngine # Generic CRUD behavior for most of our spec types. class App < Sinatra::Base set(:is_spec) do |_x| condition do path_info = env['PATH_INFO'] matches = %r{^/p4/v\d+/(?\w+)}.match(path_info) if matches spec_type = matches[:spec_type] return (spec_type == 'branches' || spec_type == 'clients' || spec_type == 'depots' || spec_type == 'groups' || spec_type == 'jobs' || spec_type == 'labels' || spec_type == 'servers' ) end false end end # Provide a generic collection accessor for each of the specs. get '/p4/:api/:spec_type', is_spec: true do |_, spec_type| require_p4 results = env['p4'].run(spec_type) results.to_json end # Provide a generic output accessor for each spec get '/p4/:api/:spec_type/:id', is_spec: true do |api, spec_type, id| require_p4 results = env['p4'].run(Util.singular(spec_type), '-o', id) results.first.to_json end # This is our generic "add" mechanism for each type. # # It's assumed that the client understands the requirements of each spec # type. post '/p4/:api/:spec_type', is_spec: true do |api, spec_type| require_p4 method_name = "save_#{Util.singular(spec_type)}".to_sym results = env['p4'].send(method_name, params) results.to_json end # An 'update' mechanism for each spec type. patch '/p4/:api/:spec_type/:id', is_spec: true do |_, spec_type, id| require_p4 p4 = env['p4'] singular = Util.singular(spec_type) spec = p4.run(singular, '-o', id)[0] spec = spec.merge(filter_params(params)) method_name = "save_#{singular}".to_sym p4.send(method_name, spec) spec.to_json end delete '/p4/:api/:spec_type/:id', is_spec: true do |_, spec_type, id| require_p4 method_name = "delete_#{Util.singular(spec_type)}".to_sym env['p4'].send(method_name, id) '' end end end