aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/configs_controller.rb
blob: a6a12cb6c4468bb501f9bc5f5d8e1c4234a5ce09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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