origami-importer 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
 - data/Gemfile +4 -0
 - data/README.md +11 -0
 - data/Rakefile +1 -0
 - data/bin/origami-importer +3 -0
 - data/lib/origami-importer/batch.rb +47 -0
 - data/lib/origami-importer/writer.rb +23 -0
 - data/lib/origami-importer.rb +52 -0
 - data/origami-importer.gemspec +23 -0
 - data/spec/spec_helper.rb +8 -0
 - data/spec/writer_spec.rb +23 -0
 - metadata +81 -0
 
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,11 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Origami Importer
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                Origami::Importer.import(:host => 'origami.dev', :realm => 'area51', :source => 'x-files', :reader => SomeAdapter.new)
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            The `reader` must have the following methods:
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            * `:in_batches` - yields sets of data that are acceptable to origami
         
     | 
| 
      
 10 
     | 
    
         
            +
            * `:relay=` - takes the data from the previous import.
         
     | 
| 
      
 11 
     | 
    
         
            +
            * `:relay` - custom data that will be returned to the next import. Can be nil.
         
     | 
    
        data/Rakefile
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "bundler/gem_tasks"
         
     | 
| 
         @@ -0,0 +1,47 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Origami
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Importer
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Batch
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                  attr_accessor :realm, :source, :endpoint, :session, :remote
         
     | 
| 
      
 6 
     | 
    
         
            +
                  def initialize(options)
         
     | 
| 
      
 7 
     | 
    
         
            +
                    self.endpoint = options[:host]
         
     | 
| 
      
 8 
     | 
    
         
            +
                    self.realm = options[:realm]
         
     | 
| 
      
 9 
     | 
    
         
            +
                    self.source = options[:source]
         
     | 
| 
      
 10 
     | 
    
         
            +
                    self.session = options[:session] # not in use yet
         
     | 
| 
      
 11 
     | 
    
         
            +
                  end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  def client
         
     | 
| 
      
 14 
     | 
    
         
            +
                    unless @client
         
     | 
| 
      
 15 
     | 
    
         
            +
                      origami_host = endpoint
         
     | 
| 
      
 16 
     | 
    
         
            +
                      Pebblebed.config { host origami_host }
         
     | 
| 
      
 17 
     | 
    
         
            +
                      @client = Pebblebed::Connector.new(session).origami
         
     | 
| 
      
 18 
     | 
    
         
            +
                    end
         
     | 
| 
      
 19 
     | 
    
         
            +
                    @client
         
     | 
| 
      
 20 
     | 
    
         
            +
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                  def open
         
     | 
| 
      
 23 
     | 
    
         
            +
                    result = client.post "/#{realm}/batches", {:batch => {:source => source}}
         
     | 
| 
      
 24 
     | 
    
         
            +
                    self.remote = result.batch
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  def relay
         
     | 
| 
      
 28 
     | 
    
         
            +
                    remote.relay || {:previous_import_at => Time.now.utc}
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                  # what about :delete => other_records?
         
     | 
| 
      
 32 
     | 
    
         
            +
                  def save(records)
         
     | 
| 
      
 33 
     | 
    
         
            +
                    client.post "/#{realm}/batches/#{remote.id}/items", :put => records
         
     | 
| 
      
 34 
     | 
    
         
            +
                  end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                  def close(data)
         
     | 
| 
      
 37 
     | 
    
         
            +
                    url = "/#{realm}/batches/#{remote.id}/close"
         
     | 
| 
      
 38 
     | 
    
         
            +
                    puts "posting to #{url}, :relay => #{data.inspect}"
         
     | 
| 
      
 39 
     | 
    
         
            +
                    client.post url, :relay => data
         
     | 
| 
      
 40 
     | 
    
         
            +
                  end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  def delete
         
     | 
| 
      
 43 
     | 
    
         
            +
                    client.delete "/#{realm}/batches/#{remote.id}"
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
              end
         
     | 
| 
      
 47 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,23 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Origami
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Importer
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
                class Writer
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                  attr_accessor :batch
         
     | 
| 
      
 7 
     | 
    
         
            +
                  def initialize(batch)
         
     | 
| 
      
 8 
     | 
    
         
            +
                    self.batch = batch
         
     | 
| 
      
 9 
     | 
    
         
            +
                  end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  def <<(records)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    save [records].flatten.compact
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  def save(records)
         
     | 
| 
      
 16 
     | 
    
         
            +
                    if records.size > 0
         
     | 
| 
      
 17 
     | 
    
         
            +
                      batch.save records
         
     | 
| 
      
 18 
     | 
    
         
            +
                    end
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,52 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'pebblebed'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'json'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            require 'origami-importer/batch'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'origami-importer/writer'
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            Pebblebed.config do
         
     | 
| 
      
 8 
     | 
    
         
            +
              service :origami
         
     | 
| 
      
 9 
     | 
    
         
            +
            end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            module Origami
         
     | 
| 
      
 12 
     | 
    
         
            +
              module Importer
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                class BatchFailed < RuntimeError; end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  def import(options = {})
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                    Origami::Importer.batch(options) do |reader, writer|
         
     | 
| 
      
 21 
     | 
    
         
            +
                      reader.in_batches { |records| writer << records }
         
     | 
| 
      
 22 
     | 
    
         
            +
                    end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                  def batch(options = {}, &block)
         
     | 
| 
      
 27 
     | 
    
         
            +
                    host = options[:host]
         
     | 
| 
      
 28 
     | 
    
         
            +
                    realm = options[:realm]
         
     | 
| 
      
 29 
     | 
    
         
            +
                    source = options[:source]
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                    batch = Batch.new(:host => host, :realm => realm, :source => source)
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                    reader = options[:reader]
         
     | 
| 
      
 34 
     | 
    
         
            +
                    writer = Writer.new(batch)
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 37 
     | 
    
         
            +
                      batch.open
         
     | 
| 
      
 38 
     | 
    
         
            +
                      reader.relay = batch.relay
         
     | 
| 
      
 39 
     | 
    
         
            +
                      yield reader, writer
         
     | 
| 
      
 40 
     | 
    
         
            +
                    rescue Pebblebed::HttpError => e
         
     | 
| 
      
 41 
     | 
    
         
            +
                      puts "Cannot continue. #{e.message}"
         
     | 
| 
      
 42 
     | 
    
         
            +
                    rescue BatchFailed => e
         
     | 
| 
      
 43 
     | 
    
         
            +
                      puts "Failed. Deleting"
         
     | 
| 
      
 44 
     | 
    
         
            +
                      batch.delete
         
     | 
| 
      
 45 
     | 
    
         
            +
                    else
         
     | 
| 
      
 46 
     | 
    
         
            +
                      puts "Succeeded. Closing."
         
     | 
| 
      
 47 
     | 
    
         
            +
                      batch.close(reader.relay)
         
     | 
| 
      
 48 
     | 
    
         
            +
                    end
         
     | 
| 
      
 49 
     | 
    
         
            +
                  end
         
     | 
| 
      
 50 
     | 
    
         
            +
                end
         
     | 
| 
      
 51 
     | 
    
         
            +
              end
         
     | 
| 
      
 52 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,23 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            $:.push File.expand_path("../lib", __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            Gem::Specification.new do |s|
         
     | 
| 
      
 5 
     | 
    
         
            +
              s.name        = "origami-importer"
         
     | 
| 
      
 6 
     | 
    
         
            +
              s.version     = "0.0.0"
         
     | 
| 
      
 7 
     | 
    
         
            +
              s.authors     = ["Katrina Owen"]
         
     | 
| 
      
 8 
     | 
    
         
            +
              s.email       = ["katrina.owen@gmail.com"]
         
     | 
| 
      
 9 
     | 
    
         
            +
              s.homepage    = ""
         
     | 
| 
      
 10 
     | 
    
         
            +
              s.summary     = %q{Superglue for origami imports}
         
     | 
| 
      
 11 
     | 
    
         
            +
              s.description = %q{Superglue for origami imports}
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              s.rubyforge_project = "origami-importer"
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
              s.files         = `git ls-files`.split("\n")
         
     | 
| 
      
 16 
     | 
    
         
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         
     | 
| 
      
 17 
     | 
    
         
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         
     | 
| 
      
 18 
     | 
    
         
            +
              s.require_paths = ["lib"]
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              # specify any dependencies here; for example:
         
     | 
| 
      
 21 
     | 
    
         
            +
              s.add_development_dependency "rspec"
         
     | 
| 
      
 22 
     | 
    
         
            +
              s.add_runtime_dependency "pebblebed"
         
     | 
| 
      
 23 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
    
        data/spec/writer_spec.rb
    ADDED
    
    | 
         @@ -0,0 +1,23 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'origami-importer/writer'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            describe Origami::Importer::Writer do
         
     | 
| 
      
 5 
     | 
    
         
            +
              let(:batch) { stub(:batch) }
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              subject { Origami::Importer::Writer.new(batch) }
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              it "saves records" do
         
     | 
| 
      
 10 
     | 
    
         
            +
                batch.should_receive(:save).with [:records]
         
     | 
| 
      
 11 
     | 
    
         
            +
                subject << [:records]
         
     | 
| 
      
 12 
     | 
    
         
            +
              end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
              it "handles single entries" do
         
     | 
| 
      
 15 
     | 
    
         
            +
                batch.should_receive(:save).with [:records]
         
     | 
| 
      
 16 
     | 
    
         
            +
                subject << :records
         
     | 
| 
      
 17 
     | 
    
         
            +
              end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              it "doesn't bother unless there are actually records" do
         
     | 
| 
      
 20 
     | 
    
         
            +
                batch.should_not_receive(:save)
         
     | 
| 
      
 21 
     | 
    
         
            +
                subject << [nil, nil, nil]
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,81 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: origami-importer
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.0
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Katrina Owen
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-09-18 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 14 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 15 
     | 
    
         
            +
              name: rspec
         
     | 
| 
      
 16 
     | 
    
         
            +
              requirement: &70151119500860 !ruby/object:Gem::Requirement
         
     | 
| 
      
 17 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 18 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 19 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 20 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 21 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 22 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 23 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 24 
     | 
    
         
            +
              version_requirements: *70151119500860
         
     | 
| 
      
 25 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 26 
     | 
    
         
            +
              name: pebblebed
         
     | 
| 
      
 27 
     | 
    
         
            +
              requirement: &70151119500340 !ruby/object:Gem::Requirement
         
     | 
| 
      
 28 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 29 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 30 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 31 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 32 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 33 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 34 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 35 
     | 
    
         
            +
              version_requirements: *70151119500340
         
     | 
| 
      
 36 
     | 
    
         
            +
            description: Superglue for origami imports
         
     | 
| 
      
 37 
     | 
    
         
            +
            email:
         
     | 
| 
      
 38 
     | 
    
         
            +
            - katrina.owen@gmail.com
         
     | 
| 
      
 39 
     | 
    
         
            +
            executables:
         
     | 
| 
      
 40 
     | 
    
         
            +
            - origami-importer
         
     | 
| 
      
 41 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 42 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 43 
     | 
    
         
            +
            files:
         
     | 
| 
      
 44 
     | 
    
         
            +
            - .gitignore
         
     | 
| 
      
 45 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 46 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 47 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 48 
     | 
    
         
            +
            - bin/origami-importer
         
     | 
| 
      
 49 
     | 
    
         
            +
            - lib/origami-importer.rb
         
     | 
| 
      
 50 
     | 
    
         
            +
            - lib/origami-importer/batch.rb
         
     | 
| 
      
 51 
     | 
    
         
            +
            - lib/origami-importer/writer.rb
         
     | 
| 
      
 52 
     | 
    
         
            +
            - origami-importer.gemspec
         
     | 
| 
      
 53 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 54 
     | 
    
         
            +
            - spec/writer_spec.rb
         
     | 
| 
      
 55 
     | 
    
         
            +
            homepage: ''
         
     | 
| 
      
 56 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 57 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 58 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 59 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 60 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 61 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 62 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 63 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 64 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 65 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 66 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 67 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 68 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 69 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 70 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 71 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 72 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 73 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 74 
     | 
    
         
            +
            rubyforge_project: origami-importer
         
     | 
| 
      
 75 
     | 
    
         
            +
            rubygems_version: 1.8.15
         
     | 
| 
      
 76 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 77 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 78 
     | 
    
         
            +
            summary: Superglue for origami imports
         
     | 
| 
      
 79 
     | 
    
         
            +
            test_files:
         
     | 
| 
      
 80 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 81 
     | 
    
         
            +
            - spec/writer_spec.rb
         
     |