<commit_before>require 'sinatra/base'
require 'blankslate'
require 'nokogiri'

module Sinatra
  module Wechat
    module EndpointActions
      class WechatDispatcher < ::BlankSlate
        attr_reader :wechat_token, :verfiy_message
        def initialize
          super
          @message_handlers = {}
        end

        def method_missing(sym, *args, &block)
          @message_handlers[sym] ||= []
          matchers = args.collect do |v|
            if v.respond_to?(:call) then lambda { |values| v.call(values) }
            # for named parameters
            elsif v.respond_to?(:all?) then lambda { |values| v.all? { |k,v| v === values[k]} }
            else raise TypeError, "\"#{v} (#{v.class})\" is not an acceptable condition"
            end 
          end
          matcher = lambda do |values| 
            matchers.all? {|m| m.call(values)}
          end
          @message_handlers[sym] << [ matcher, block ]
        end

        def route!(values)
          type = values[:msg_type].to_sym
          handlers = @message_handlers[type] || []
          _, handler = handlers.find { |m, _| m.call(values) }
          handler
        end
      end

      def wechat(endpoint = '/', wechat_token: '', message_validation: true, &block)
        dispatcher = WechatDispatcher.new
        dispatcher.instance_eval &block

        get endpoint do
          halt 403 unless validate_messages(wechat_token) if message_validation
          content_type 'text/plain'
          params[:echostr]
        end

        post endpoint do
          halt 403 unless validate_messages(wechat_token) if message_validation

          body = request.body.read || ""
          halt 501 if body.empty?

          doc = Nokogiri::XML(body).root
          values = doc.element_children.each_with_object(Hash.new) do |e, v|
            name = e.name.gsub(/(.)([A-Z])/,'\1_\2').downcase<commit_msg>refine http response status code<commit_after>require 'sinatra/base'
require 'blankslate'
require 'nokogiri'

module Sinatra
  module Wechat
    module EndpointActions
      class WechatDispatcher < ::BlankSlate
        attr_reader :wechat_token, :verfiy_message
        def initialize
          super
          @message_handlers = {}
        end

        def method_missing(sym, *args, &block)
          @message_handlers[sym] ||= []
          matchers = args.collect do |v|
            if v.respond_to?(:call) then lambda { |values| v.call(values) }
            # for named parameters
            elsif v.respond_to?(:all?) then lambda { |values| v.all? { |k,v| v === values[k]} }
            else raise TypeError, "\"#{v} (#{v.class})\" is not an acceptable condition"
            end 
          end
          matcher = lambda do |values| 
            matchers.all? {|m| m.call(values)}
          end
          @message_handlers[sym] << [ matcher, block ]
        end

        def route!(values)
          type = values[:msg_type].to_sym
          handlers = @message_handlers[type] || []
          _, handler = handlers.find { |m, _| m.call(values) }
          handler
        end
      end

      def wechat(endpoint = '/', wechat_token: '', message_validation: true, &block)
        dispatcher = WechatDispatcher.new
        dispatcher.instance_eval &block

        before endpoint do
          halt 403 unless validate_messages(wechat_token) if message_validation
        end

        get endpoint do
          content_type 'text/plain'
          params[:echostr]
        end

        post endpoint do
          body = request.body.read || ""
          halt 400 if body.empty?  # bad request

          doc = Nokogiri::XML(body).root
          values = doc.element_children.each_with_object(Hash.new) do |e, v|
            name = e.name.gsub(/(.)([A-Z])/,'\1_\2').downcase
{'fraction_non_alphanumeric': 0.11075697211155379, 'fraction_numerical': 0.005046480743691899, 'mean_word_length': 2.0200481154771452, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3788395', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1093, 'n_words': 271}