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

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

Instance Method Summary collapse

Instance Method Details

#wedge_assetsObject



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/roda/plugins/wedge.rb', line 52

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.config.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
        # try json
        data.merge!(body ? JSON.parse(body) : {})
      rescue
        begin
          # try form data
          data.merge!(body ? Rack::Utils.parse_query(body) : {})
        rescue
          # no data
        end
      end

      data          = data.indifferent
      name          = data.delete(:__wedge_name__)
      method_called = data.delete(:__wedge_method__)
      method_args   = data.delete(:__wedge_args__)

      if method_args == '__wedge_data__' && data
        method_args   = [data]
        res = scope.wedge(name).send(method_called, *method_args) || ''
      else
        # This used to send things like init, we need a better way to
        # send client config data to the server
        # res = scope.wedge(name, data).send(method_called, *method_args) || ''
        res = scope.wedge(name).send(method_called, *method_args) || ''
      end

      scope.response.headers["WEDGE-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
      scope.response.headers['Content-Type'] = 'application/javascript; charset=UTF-8'

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