Sha256: 71df1289a494eef2de15c0576b8fe6d706abebd757ba064a30d6145fb8c67c99

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'couch/actions/base'

module Couch
  module Actions
    class Pull < Base
      add_runtime_options!

      def pull_me
        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, value
          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.2 lib/couch/actions/pull.rb
couch-0.0.1 lib/couch/actions/pull.rb