Sha256: 76fcafd6147a782e0fad5fee66ab675e059b7f1b4d8c1f4763618bfd22ae7da7

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module Wukong
  module Elasticsearch

    # A convenient class for parsing Elasticsearch index and mapping URIs
    # like
    #
    #   - es://my_index
    #   - es://my_index/my_mapping
    #   - es://first_index,second_index,third_index
    #   - es://my_index/first_mapping,second_mapping,third_mapping
    class IndexAndMapping

      # A regular expression that matches URIs describing an
      # Elasticsearch index and/or mapping to read/write from/to.
      #
      # @param [Regexp]
      ES_SCHEME_REGEXP        = %r{^es://}
      
      # The Elasticsearch index.
      #
      # @param [String]
      attr_reader :index

      # The Elasticsearch mapping.
      #
      # @param [String]
      attr_reader :mapping

      # Does the given `string` look like a possible Elasticsearch
      # /index/mapping specification?
      #
      # @param [String] string
      # @return [true, false]
      def self.matches? string
        return false unless string
        string =~ ES_SCHEME_REGEXP
      end

      # Create a new index and mapping specification from the given
      # +uri..
      #
      # @param [String] uri
      def initialize uri
        self.uri = uri
      end

      # Set the URI of this index and mapping specification, parsing it
      # for an index and mapping.
      #
      # Will raise an error if the given URI is malformed.
      #
      # @param [String] uri
      def uri= uri
        raise Wukong::Error.new("'#{uri}' is not an ElasticSearch es://index/mapping specification") unless self.class.matches?(uri)
        parts = uri.gsub(ES_SCHEME_REGEXP, '').gsub(/^\/+/,'').gsub(/\/+$/,'').split('/')
        
        raise Wukong::Error.new("'#{uri}' is not an ElasticSearch es://index/mapping specification") unless parts.size.between?(1,2)

        @index   = parts[0]
        @mapping = parts[1]
      end
    end
  end
end

    
    

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wonderdog-0.2.0 lib/wonderdog/index_and_mapping.rb
wonderdog-0.1.1 lib/wonderdog/index_and_mapping.rb
wonderdog-0.1.0 lib/wonderdog/index_and_mapping.rb