Sha256: 131d918b07715850555d48aa8e9b3c4d8b5217101445c564c0888eb213ff6f1a

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 KB

Contents

require 'rid/actions/base'
require 'rid/design_document'

require 'pathname'
require "rest_client"

module Rid
  module Actions
    class Push < Base
      def initialize(*args)
        super
        @doc = DesignDocument.new
      end

      def create_database_unless_exists
        RestClient.put @doc.database, nil
        say "Created database %s" % @doc.database
      rescue RestClient::PreconditionFailed
      end

      def push
        root = Pathname.new(destination_root)
        filenames = Dir[root.join "**/*"]
        filenames.map! { |file| Pathname.new file }
        filenames.map! { |path| path.relative_path_from root }
        filenames.delete_if { |path| !path.file? }
        filenames.map!(&:to_s)
        @doc.read(filenames) do |filename|
          File.read File.join(destination_root, filename)
        end

        say "Pushing to %s" % @doc.url

        resp = RestClient.put @doc.url, @doc.json
        response = JSON.parse(resp.body)

        if response["ok"]
          @doc.rev = response["rev"]
          File.open File.join(destination_root, "_rev"), "w" do |file|
           file << @doc.rev
          end

          say "Pushed %s" % @doc.rev
        else
          say "Error occured: %s" % response.inspect
        end

      rescue RestClient::Conflict
        say "Conflict! Try to pull first or delete ./_rev."
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rid-0.5.2 lib/rid/actions/push.rb
rid-0.5.1 lib/rid/actions/push.rb
rid-0.5.0 lib/rid/actions/push.rb
rid-0.4.1 lib/rid/actions/push.rb
rid-0.4.0 lib/rid/actions/push.rb
rid-0.3.1 lib/rid/actions/push.rb
rid-0.3.0 lib/rid/actions/push.rb