Sha256: b83fe6c784f839c92351427be73ef6f41f35582cb033b79727a7513c526e39a7

Contents?: true

Size: 984 Bytes

Versions: 1

Compression:

Stored size: 984 Bytes

Contents

require 'restclient'
require 'json'

module CouchDesignDocs
  class Store
    attr_accessor :url

    def initialize(url)
      @url = url
    end

    def load(h)
      h.each_pair do |document_name, doc|
        Store.put!("#{url}/_design/#{document_name}", doc)
      end
    end

    # Create or replace the document located at "path" with the
    # Hash document "doc"
    def self.put!(path, doc)
      self.put(path, doc)
    rescue RestClient::RequestFailed
      self.delete_and_put(path, doc)
    end

    def self.delete_and_put(path, doc)
      self.delete(path)
      self.put(path, doc)
    end

    def self.put(path, doc)
      RestClient.put path,
        doc.to_json,
        :content_type => 'application/json'
    end

    def self.delete(path)
      # retrieve existing to obtain the revision
      old = self.get(path)
      RestClient.delete(path + "?rev=#{old['_rev']}")
    end

    def self.get(path)
      JSON.parse(RestClient.get(path))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eee-c-couch_design_docs-1.0.1 lib/couch_design_docs/store.rb