<commit_before>module LookerSDK
  class Client

    module Dynamic

      def load_swagger
        @swagger ||= (
          without_authentication do
            begin
              get 'swagger.json'
            rescue
              # eat any errors
            end
          end
        )
      end

      def operations
        return nil unless @swagger
        @operations ||= (
          ops = {}
          paths = @swagger[:paths].to_h

          paths.each do |path_name, path_info|
            path_info.to_h.each do |method, route_info|
              route_info = route_info.to_h
              ops[route_info[:operationId]] = {:route => path_name, :method => method, :info => route_info }
            end
          end
          ops
        )
      end

      def method_link(entry)
        uri = URI.parse(api_endpoint)
        "#{uri.scheme}://#{uri.host}:#{uri.port}/api-docs/index.html#!/#{entry[:info][:tags].first}/#{entry[:info][:operationId]}" rescue "http://docs.looker.com/"
      end

      def respond_to?(method_name, include_private=false)
        (operations && !!operations[method_name.to_s]) || super
      end

      def method_missing(method_name, *args, &block)
        entry = operations && operations[method_name.to_s]
        return super unless entry

        args = (args || []).dup
        route = entry[:route].to_s
        params = (entry[:info][:parameters] || []).select {|param| param[:in] == 'path'}
        body_param = (entry[:info][:parameters] || []).select {|param| param[:in] == 'body'}.first

        params_passed = args.length
        params_required = params.length + (body_param && body_param[:required] ? 1 : 0)
        raise ArgumentError.new("wrong number of arguments (#{params_passed} for #{params_required}) in call to '#{method_name}'. See '#{method_link(entry)}'") unless params_passed >= params_required

        # substitute the actual params into the route template
        params.each {|param| route.sub!("{#{param[:name]}}", args.shift.to_s) }

        opts = args[0] || {}

        method = entry[:method].to_sym
        case method<commit_msg>simplify. dup the template before xforming it, for gawd's sake. support base_path.<commit_after>module LookerSDK
  class Client

    module Dynamic

      def load_swagger
        @swagger ||= without_authentication {get 'swagger.json' rescue nil}
      end

      def operations
        return nil unless @swagger
        @operations ||= Hash[
          @swagger[:paths].to_h.map do |path_name, path_info|
            path_info.to_h.map do |method, route_info|
              route = @swagger[:basePath].to_s + path_name.to_s
              [route_info[:operationId], {:route => route, :method => method, :info => route_info.to_h}]
            end
          end.reduce(:+)
        ]
      end

      def method_link(entry)
        uri = URI.parse(api_endpoint)
        "#{uri.scheme}://#{uri.host}:#{uri.port}/api-docs/index.html#!/#{entry[:info][:tags].first}/#{entry[:info][:operationId]}" rescue "http://docs.looker.com/"
      end

      def respond_to?(method_name, include_private=false)
        (operations && !!operations[method_name.to_s]) || super
      end

      def method_missing(method_name, *args, &block)
        entry = operations && operations[method_name.to_s]
        return super unless entry

        args = (args || []).dup
        route = entry[:route].to_s.dup
        params = (entry[:info][:parameters] || []).select {|param| param[:in] == 'path'}
        body_param = (entry[:info][:parameters] || []).select {|param| param[:in] == 'body'}.first

        params_passed = args.length
        params_required = params.length + (body_param && body_param[:required] ? 1 : 0)
        unless params_passed >= params_required
          raise ArgumentError.new("wrong number of arguments (#{params_passed} for #{params_required}) in call to '#{method_name}'. See '#{method_link(entry)}'")
        end

        # substitute the actual params into the route template
        params.each {|param| route.sub!("{#{param[:name]}}", args.shift.to_s) }

        opts = args[0] || {}

        method = entry[:method].to_sym
        case method
{'fraction_non_alphanumeric': 0.14093637454981991, 'fraction_numerical': 0.0014405762304921968, 'mean_word_length': 2.451532725766363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3606593', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1223, 'n_words': 279}