Module: Roda::RodaPlugins::Wedge::RequestMethods

Defined in:
lib/roda/plugins/wedge.rb

Instance Method Summary collapse

Instance Method Details

#wedge_assetsObject



44
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
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/roda/plugins/wedge.rb', line 44

def wedge_assets
  on self.class.wedge_route_regex do |component, ext|
    case ext
    when 'map'
      ::Wedge.source_map component
    when 'rb'
      if component =~ /^wedge/
        path = ::Wedge.opts.file_path.gsub(/\/wedge.rb$/, '')
        File.read("#{path}/#{component}.rb")
      else
        File.read("#{ROOT_PATH}/#{component}.rb")
      end
    when 'call'
      body = scope.request.body.read
      data = scope.request.params

      begin
        data.merge!(body ? JSON.parse(body) : {})
      rescue
        # can't be parsed by json
      end

      data          = data.indifferent
      name          = data.delete(:name)
      method_called = data.delete(:method_called)
      method_args   = data.delete(:method_args)

      res = scope.wedge(name, data).send(method_called, *method_args) || ''

      scope.response.headers["BIO-CSRF-TOKEN"] = scope.csrf_token if scope.methods.include? :csrf_token

      if res.is_a? Hash
        scope.response.headers["Content-Type"] = 'application/json; charset=UTF-8'
        res = res.to_json
      else
        res = res.to_s
      end

      res
    else
      "#{::Wedge.javascript(component)}\n//# sourceMappingURL=/assets/wedge/#{component}.map"
    end
  end
end