Sha256: 78008c7ec97cd57e7aa44858551096365b5174d0bc7924281aae4f5e1d01ae0e

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 KB

Contents

module SafeRubyWithSinatra
  PARSER_REGEX = /["'](?<path>\/.*)["']\.(?<method>[A-z]+)/
  QUERY_REGEX  = /query?.*\{(?<query>.*)\}/
  INPUT_REGEX  = /input?.*\{(?<input>.*)\}/
end

Toycol::Protocol.define(:safe_ruby_with_sinatra) do |message|
  using Module.new {
    refine String do
      def get(options = {})
        Toycol::Protocol.request.query { options[:query] } if options[:query]
        Toycol::Protocol.request.http_method { "GET" }
      end

      def post(options = {})
        Toycol::Protocol.request.input { options[:input] } if options[:input]
        Toycol::Protocol.request.http_method { "POST" }
      end

      def parse_as_queries
        split(",").map { |str| str.scan(/\w+/).join("=") }
      end

      def parse_as_inputs
        split(",").map { |str| str.split(":").map { |s| s.strip! && s.gsub(/['"]/, "") }.join("=") }
      end
    end
  }

  path, method = SafeRubyWithSinatra::PARSER_REGEX.match(message) { |m| [m[:path], m[:method]] }
  query = SafeRubyWithSinatra::QUERY_REGEX.match(message) { |m| m[:query].parse_as_queries }&.join("&")
  input = SafeRubyWithSinatra::INPUT_REGEX.match(message) { |m| m[:input].parse_as_inputs }&.join("&")
  args  = {}

  request.path { path }

  %i[query input].each do |k|
    if v = binding.local_variable_get(k)
      args[*k] = v
    end
  end

  request_path.public_send(method, args)
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
toycol-1.0.0 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra
toycol-0.3.1 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra
toycol-0.3.0 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra
toycol-0.2.2 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra
toycol-0.2.1 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra
toycol-0.2.0 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra
toycol-0.1.0 examples/safe_ruby_with_sinatra/Protocolfile.safe_ruby_with_sinatra