<commit_before>require 'rakuten_web_service/client'
require 'rakuten_web_service/search_result'

module RakutenWebService
  class Resource
    class << self
      def attribute(*attributes)
        attributes.each do |attribute|
          method_name = attribute.to_s.gsub(/([a-z]+)([A-Z]{1})/) do |matched|
            "#{$1}_#{$2.downcase}"
          end
          method_name = method_name.sub(/^#{resource_name}_(\w+)$/) { $1 }
          instance_eval do
            define_method method_name do
              (instance_variable_get(:@params))[attribute.to_s]
            end
          end
          if method_name =~ /(.+)_flag$/
            instance_eval do
              define_method "#{$1}?" do
                instance_variable_get(:@params)[attribute.to_s] == 1
              end
            end
          end
        end
      end

      def search(options)
        SearchResult.new(options, self, client)
      end

      def resource_name
        @resource_name ||= self.name.split('::').last.downcase
      end

      def set_resource_name(name)
        @resource_name = name
      end

      def endpoint(url=nil)
        @endpoint = url || @endpoint 
      end

      def client
        @client ||= RakutenWebService::Client.new(endpoint)
      end

      def set_parser(&block)
        @parse_proc = block
      end

      def parse_response(response)
        @parse_proc.call(response)
      end
    end

    def initialize(params)
      @params = params.dup
      params.each { |k, v| @params[k.to_s] = v }
    end

    def [](key)
      camel_key = key.<KEY> { "#{$1}#{$2.capitalize}" }
      @params[key] || @params[camel_key]
    end
  end
end<commit_msg>Refactor Resource so that parse_response are allowed to be inherited<commit_after>require 'rakuten_web_service/client'
require 'rakuten_web_service/search_result'

module RakutenWebService
  class Resource
    class << self
      def attribute(*attributes)
        attributes.each do |attribute|
          method_name = attribute.to_s.gsub(/([a-z]+)([A-Z]{1})/) do |matched|
            "#{$1}_#{$2.downcase}"
          end
          method_name = method_name.sub(/^#{resource_name}_(\w+)$/) { $1 }
          instance_eval do
            define_method method_name do
              (instance_variable_get(:@params))[attribute.to_s]
            end
          end
          if method_name =~ /(.+)_flag$/
            instance_eval do
              define_method "#{$1}?" do
                instance_variable_get(:@params)[attribute.to_s] == 1
              end
            end
          end
        end
      end

      def search(options)
        SearchResult.new(options, self, client)
      end

      def resource_name
        @resource_name ||= self.name.split('::').last.downcase
      end

      def set_resource_name(name)
        @resource_name = name
      end

      def endpoint(url=nil)
        @endpoint = url || @endpoint 
      end

      def client
        @client ||= RakutenWebService::Client.new(endpoint)
      end

      def set_parser(&block)
        instance_eval do
          define_singleton_method :parse_response, block
        end
      end
    end

    def initialize(params)
      @params = params.dup
      params.each { |k, v| @params[k.to_s] = v }
    end

    def [](key)
      camel_key = key.gsub(/([<KEY> { "#{$1}#{$2.capitalize}" }
      @params[key] || @params[camel_key]
    end
  end
end
{'fraction_non_alphanumeric': 0.11692126909518213, 'fraction_numerical': 0.004700352526439483, 'mean_word_length': 1.9506065857885615, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '6797056', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1030, 'n_words': 216}