Sha256: 9a51868eba618c7841cc12233e0e69d8742c4d361dbf1e58aa08a60232d20269

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require 'couch/actions/base'

module Couch
  module Actions
    class Pull < Base
      add_runtime_options!

      def pull
        id = mapper.doc["_id"]
        url = url_for(id)

        say "Pulling from %s..." % url

        resp = RestClient.get url + "?attachments=true"
        response = JSON.parse(resp.body)
        map_doc(response)
        rev = response["_rev"]

        say "Checked out %s" % rev
      rescue RestClient::ResourceNotFound
        say "Error: Document %s does not exist!" % id
      end

      private

      def map_doc(doc, dirname = destination_root)
        doc.each do |key, value|
          next if key == "_attachments"
          filename = File.join(dirname, key)
          if value.is_a?(String)
            create_file "#{filename}.js", "#{value}\n"
          else
            Dir.mkdir(filename) unless File.directory?(filename)
            map_doc value, File.join(dirname, key)
          end
        end
        map_doc_attachments(doc["_attachments"], dirname) if doc["_attachments"]
      end

      def map_doc_attachments(doc, dirname)
        doc.each do |key, value|
          filename = File.join(dirname, "_attachments", key)
          create_file filename, value["data"].unpack("m")
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couch-0.0.4 lib/couch/actions/pull.rb
couch-0.0.3 lib/couch/actions/pull.rb