From 592d37d72f85037ce41ea58f9f930e608ec338f8 Mon Sep 17 00:00:00 2001 From: Heiko Bernloehr Date: Wed, 14 Aug 2013 13:50:56 +0200 Subject: New resource /sys/configs Every participant could GET and POST to this resource. A GET might show this: curl ... -H 'Content-Type: application/json' \ -X GET https://ecs.host.com/sys/configs { "participant_events": "true", "resource_events": { "/campusconnect/glossaries": true, "/campusconnect/course_urls": true, "/campusconnect/course_members": true, "/campusconnect/wikis": true, "/campusconnect/directory_trees": true, "/campusconnect/learningmodules": true, "/campusconnect/groups": true, "/campusconnect/categories": true, "/campusconnect/courselinks": true, "/campusconnect/organisation_units": true, "/campusconnect/files": true, "/campusconnect/terms": true, "/campusconnect/courses": true, "/sys/auths": false }, "selfrouting": "false" } Only if participant_events and the appropriate resource_events are true, you will get an event. It's also possible to set the two attributes participant_events and selfrouting. Just make a POST such as: curl ... -H 'Content-Type: application/json' -X -X POST \ -d '{"participant_events":true}' https://ecs.host.com/sys/configs or curl ... -H 'Content-Type: application/json' -X -X POST \ -d '{"selfrouting":false}' https://ecs.host.com/sys/configs or curl ... -H 'Content-Type: application/json' -X -X POST \ -d '{"selfrouting":false, "participant_events":true}' \ https://ecs.host.com/sys/configs As you see, just set the attribute you want to set and make a POST. You don't have to provide a receiver header, because the receiver is implicitely the ECS. --- app/controllers/configs_controller.rb | 64 ++++++++++++++++++++++++++++++ app/helpers/configs_helper.rb | 2 + app/views/configs/create.html.erb | 2 + app/views/configs/index.html.erb | 2 + config/routes.rb | 2 + test/functional/configs_controller_test.rb | 8 ++++ test/unit/helpers/configs_helper_test.rb | 4 ++ 7 files changed, 84 insertions(+) create mode 100644 app/controllers/configs_controller.rb create mode 100644 app/helpers/configs_helper.rb create mode 100644 app/views/configs/create.html.erb create mode 100644 app/views/configs/index.html.erb create mode 100644 test/functional/configs_controller_test.rb create mode 100644 test/unit/helpers/configs_helper_test.rb diff --git a/app/controllers/configs_controller.rb b/app/controllers/configs_controller.rb new file mode 100644 index 0000000..a6a12cb --- /dev/null +++ b/app/controllers/configs_controller.rb @@ -0,0 +1,64 @@ +class ConfigsController < ApplicationController + + require 'json/add/rails' + + before_filter :authentication + before_filter :add_cookie_header # only for anonymous participants + + def initialize + super + end + + def index + config_render + end + + def create + unless Mime::Type.lookup(request.headers["CONTENT_TYPE"]).to_sym == :json + raise Ecs::InvalidMimetypeException, "Please provide \"Content-Type:\" header. Data format has to be in JSON (application/json)" + end + + config= ActiveSupport::JSON.decode request.raw_post + + if config["participant_events"] == true + @participant.events_= true + else + @participant.events_= false + end unless config["participant_events"].nil? + + if config["selfrouting"] == true + @participant.community_selfrouting= true + else + @participant.community_selfrouting= false + end unless config["selfrouting"].nil? + + @participant.save! + config_render + #rescue ActiveSupport::OkJson::Error + rescue Ecs::InvalidMimetypeException + raise + rescue ActiveRecord::RecordInvalid + raise Ecs::InvalidMessageException, "Data could not be saved (ConfigsController#create)." + rescue StandardError + raise Ecs::InvalidMessageException, "You have provided invalid JSON data (ConfigsController#create)." + end + +private + + def config_render + config = nil + config_txt = "" + res_ev={};Ressource.all.each {|r| res_ev["/#{r.namespace}/#{r.ressource}"] = r.events} + config= \ + { + :participant_events => @participant.events?.to_s, + :resource_events => res_ev, + :selfrouting => @participant.community_selfrouting.to_s + } + respond_to do |format| + format.json { render :json => JSON.pretty_generate(config) + "\r\n" } + format.xml { render :xml => config } + end + end + +end diff --git a/app/helpers/configs_helper.rb b/app/helpers/configs_helper.rb new file mode 100644 index 0000000..5cac46f --- /dev/null +++ b/app/helpers/configs_helper.rb @@ -0,0 +1,2 @@ +module ConfigsHelper +end diff --git a/app/views/configs/create.html.erb b/app/views/configs/create.html.erb new file mode 100644 index 0000000..52a9380 --- /dev/null +++ b/app/views/configs/create.html.erb @@ -0,0 +1,2 @@ +

Configs#create

+

Find me in app/views/configs/create.html.erb

diff --git a/app/views/configs/index.html.erb b/app/views/configs/index.html.erb new file mode 100644 index 0000000..df50741 --- /dev/null +++ b/app/views/configs/index.html.erb @@ -0,0 +1,2 @@ +

Configs#index

+

Find me in app/views/configs/index.html.erb

diff --git a/config/routes.rb b/config/routes.rb index ff8114c..eeae969 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,6 +32,8 @@ ActionController::Routing::Routes.draw do |map| :only => [:index] map.resources :events, :path_prefix => '/sys', :name_prefix => 'sys_', :only => [:index], :collection => { :fifo => [:get, :post] } + map.resources :configs, :path_prefix => '/sys', :name_prefix => 'sys_', + :only => [:index, :create] begin Ressource.all.each do |r| diff --git a/test/functional/configs_controller_test.rb b/test/functional/configs_controller_test.rb new file mode 100644 index 0000000..049e561 --- /dev/null +++ b/test/functional/configs_controller_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class ConfigsControllerTest < ActionController::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/unit/helpers/configs_helper_test.rb b/test/unit/helpers/configs_helper_test.rb new file mode 100644 index 0000000..4a0c42a --- /dev/null +++ b/test/unit/helpers/configs_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class ConfigsHelperTest < ActionView::TestCase +end -- cgit v1.2.3