Sha256: b94bb79f475c4f80ac163b8683e646de1dcc6e5bcdb1876dc4a00308004509cb

Contents?: true

Size: 1.88 KB

Versions: 17

Compression:

Stored size: 1.88 KB

Contents

module Picky

  module Sources

    # A Couch database source.
    #
    # Options:
    # * url
    # and all the options of a <tt>RestClient::Resource</tt>.
    # See http://github.com/archiloque/rest-client.
    #
    # Examples:
    #  Picky::Sources::Couch.new(:title, :author, :isbn, url:'localhost:5984')
    #  Picky::Sources::Couch.new(:title, :author, :isbn, url:'localhost:5984', user:'someuser', password:'somepassword')
    #
    class Couch < Base

      # Raised when a Couch source is instantiated without a file.
      #
      # Example:
      #   Picky::Sources::Couch.new(:column1, :column2) # without file option
      #
      class NoDBGiven < StandardError; end

      #
      #
      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

        key_format   = options.delete :key_format
        @key_format  = key_format && key_format.intern || :to_s
      end

      def to_s
        self.class.name
      end

      # Tries to require the rest_client gem.
      #
      def check_gem # :nodoc:
        require 'rest_client'
      rescue LoadError
        warn_gem_missing 'rest-client', 'the CouchDB source'
        exit 1
      end

      # Harvests the data to index.
      #
      # See important note, above.
      #
      @@id_key = '_id'
      def harvest 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 NoDBGiven.new(category_names.join(', '))
      end
    end
  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
picky-4.0.0pre1 lib/picky/sources/couch.rb
picky-3.6.16 lib/picky/sources/couch.rb
picky-3.6.15 lib/picky/sources/couch.rb
picky-3.6.14 lib/picky/sources/couch.rb
picky-3.6.13 lib/picky/sources/couch.rb
picky-3.6.12 lib/picky/sources/couch.rb
picky-3.6.11 lib/picky/sources/couch.rb
picky-3.6.10 lib/picky/sources/couch.rb
picky-3.6.9 lib/picky/sources/couch.rb
picky-3.6.8 lib/picky/sources/couch.rb
picky-3.6.7 lib/picky/sources/couch.rb
picky-3.6.6 lib/picky/sources/couch.rb
picky-3.6.4 lib/picky/sources/couch.rb
picky-3.6.3 lib/picky/sources/couch.rb
picky-3.6.2 lib/picky/sources/couch.rb
picky-3.6.1 lib/picky/sources/couch.rb
picky-3.6.0 lib/picky/sources/couch.rb