Sha256: 0011d9932a400e351208fd5c9824469fb7cad7b2dc4db33969552d51284eb9f1

Contents?: true

Size: 1.71 KB

Versions: 11

Compression:

Stored size: 1.71 KB

Contents

module Sources
  
  # Raised when a Couch source is instantiated without a file.
  #
  # Example:
  #   Sources::Couch.new(:column1, :column2) # without file option
  #
  class NoCouchDBGiven < StandardError; end
  
  # A Couch database source.
  #
  # Options:
  # * url
  # and all the options of a <tt>RestClient::Resource</tt>.
  # See http://github.com/archiloque/rest-client.
  #
  # Examples:
  #  Sources::Couch.new(:title, :author, :isbn, url:'localhost:5984')
  #  Sources::Couch.new(:title, :author, :isbn, url:'localhost:5984', user:'someuser', password:'somepassword')
  #
  class Couch < Base
    
    #
    #
    def initialize *category_names, options
      check_gem
      
      Hash === options && options[:url] || raise_no_db_given(category_names)
      @db = RestClient::Resource.new options.delete(:url), options
    end
    
    # Default key format method for couch DB is to_sym. 
    #
    def key_format
      :to_sym
    end
    
    # Tries to require the rest_client gem.
    #
    def check_gem # :nodoc:
      require 'rest_client'
    rescue LoadError
      puts_gem_missing 'rest-client', 'the CouchDB source'
      exit 1
    end

    # Harvests the data to index.
    #
    # See important note, above.
    #
    @@id_key = '_id'
    def harvest type, category
      category_name = category.from.to_s
      get_data do |doc|
        yield doc[@@id_key], doc[category_name] || next
      end
    end
    
    def get_data &block # :nodoc:
      resp = @db['_all_docs?include_docs=true'].get
      JSON.parse(resp)['rows'].
        map{|row| row['doc']}.
        each &block
    end
    
    def raise_no_db_given category_names # :nodoc:
      raise NoCouchDBGiven.new(category_names.join(', '))
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
picky-1.5.2 lib/picky/sources/couch.rb
picky-1.5.1 lib/picky/sources/couch.rb
picky-1.5.0 lib/picky/sources/couch.rb
picky-1.4.3 lib/picky/sources/couch.rb
picky-1.4.2 lib/picky/sources/couch.rb
picky-1.4.1 lib/picky/sources/couch.rb
picky-1.4.0 lib/picky/sources/couch.rb
picky-1.3.4 lib/picky/sources/couch.rb
picky-1.3.3 lib/picky/sources/couch.rb
picky-1.3.2 lib/picky/sources/couch.rb
picky-1.3.1 lib/picky/sources/couch.rb