Skip to content

Commit

Permalink
Revert "start migrattion to new datashuft api"
Browse files Browse the repository at this point in the history
Should have started new branch to leave master

This reverts commit ce4e961.
  • Loading branch information
tom statter committed Mar 10, 2016
1 parent ce4e961 commit 02de537
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 104 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.6.0
54 changes: 25 additions & 29 deletions datashift_spree.gemspec
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
$LOAD_PATH.push File.expand_path('../lib', __FILE__)

require 'rake'

# Maintain your gem"s version:
require 'datashift_spree/version'


Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'datashift_spree'#Helper::gem_name
s.version = '0.6.0'

s.name = 'datashift_spree'
s.version = DataShiftSpree::VERSION
s.authors = ['Thomas Statter']
s.email = '[email protected]'
s.homepage = 'http://github.com/autotelik/datashift_spree'
s.summary = 'Shift data between Excel/CSV and Spree'
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Thomas Statter"]
s.description = "Comprehensive Excel/CSV import/export for Spree, Products,Images, any model with full associations"
s.license = 'Open Source - MIT'

s.required_ruby_version = '~> 2.0'

s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=

s.files = Dir['{lib}/**/*', 'spec/factories/**/*', 'LICENSE', 'Rakefile', 'README.markdown', 'datashift.thor']
s.test_files = Dir['spec/**/*']

s.require_paths = ['lib']

s.add_runtime_dependency 'datashift', '~> 0.16'
s.email = "[email protected]"

s.files = FileList["datashift_spree.thor",
"README.markdown",
"datashift_spree.gemspec",
'VERSION',
"LICENSE.txt",
"{lib}/**/*"].exclude("rdoc").exclude("nbproject").exclude("fixtures").exclude(".log").exclude(".contrib").to_a

s.test_files = FileList["{spec}/*"]

s.homepage = "http://github.com/autotelik/datashift_spree"
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = "1.8.15"
s.summary = "Product and image import/export for Spree from Excel/CSV"

s.add_runtime_dependency 'datashift', '~> 0.15', '>= 0.15.0'
s.add_runtime_dependency 'mechanize', '~> 2.6', '>= 2.6.0'

# for the dummy rails sandbox used in testing
s.add_development_dependency 'rubocop', '~> 0.38'
s.add_development_dependency 'rspec', '~> 3.4'
s.add_development_dependency 'factory_girl_rails', '~> 4.5'
s.add_development_dependency 'database_cleaner', '~> 1.5'

# should work with any version of spree so leave it to the client app to define
end


8 changes: 5 additions & 3 deletions lib/datashift_spree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
require 'datashift'
require 'spree'

$:.unshift '.' unless $:.include?('.')

module DataShift

module SpreeEcom
Expand Down Expand Up @@ -87,9 +89,7 @@ def self.require_libraries
end

def self.require_datashift_spree

require_relative 'datashift_spree/exceptions'


require_libs = %w{ loaders helpers }
require_libs.each do |base|
Dir[File.join(library_path, base, '**/*.rb')].each do |rb|
Expand Down Expand Up @@ -126,3 +126,5 @@ def self.load_commands()
DataShift::SpreeEcom::require_libraries
DataShift::SpreeEcom::require_datashift_spree

require 'datashift_spree/exceptions'

3 changes: 1 addition & 2 deletions lib/datashift_spree/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Date :: June 2014
# License:: Free, Open Source.
#
require 'datashift/exceptions'

module DataShift

Expand All @@ -22,4 +21,4 @@ def initialize( msg )
end

end
end
end
4 changes: 3 additions & 1 deletion lib/datashift_spree/spree_ecom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# Since datashift gem is not a Rails app or a Spree App, provides utilities to internally
# create a Spree Database, and to load Spree components, enabling standalone testing.
#
# => Has been tested with 0.11.2, 0.7, 1.0.0, 1.1.2, 1.1.3
#
# => TODO - See if we can improve DB creation/migration ....
# N.B Some or all of Spree Tests may fail very first time run,
# as the database is auto generated
Expand Down Expand Up @@ -87,4 +89,4 @@ def self.load()
require 'spree_core'
end
end
end
end
3 changes: 0 additions & 3 deletions lib/datashift_spree/version.rb

This file was deleted.

15 changes: 7 additions & 8 deletions lib/loaders/spree/product_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@ module SpreeEcom
class ProductLoader < SpreeBaseLoader

# Options
#
# :reload : Force load of the method dictionary for object_class even if already loaded
# :verbose : Verbose logging and to STDOUT
#
# :file_name : Filename that will be loaded
# :verbose : Verbose logging and to STDOUT
# :strict : Raise exceptions when issues like missing mandatory columns
#
def initialize( options = {})
def initialize(product = nil, options = {})

# We want the delegated methods on Variant so always include instance methods
opts = {:find_operators => true, :instance_methods => true}.merge( options )

# depending on version get_product_class should return us right class, namespaced or not

super(opts)
super( DataShift::SpreeEcom::get_product_class, product, opts)

raise "Failed to create Product for loading" unless @load_object
end

# Options:
# [:dummy] : Perform a dummy run - attempt to load everything but then roll back
#
def perform_load( klass = DataShift::SpreeEcom::get_product_class, opts = {} )
def perform_load( file_name, opts = {} )

logger.info "Product load from File [#{file_name}]"

Expand Down Expand Up @@ -742,4 +741,4 @@ def add_variant_images(current_value)

end
end
end
end
82 changes: 50 additions & 32 deletions lib/loaders/spree/spree_base_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,47 @@ module DataShift

class SpreeBaseLoader < LoaderBase

include DataShift::CsvLoading
include DataShift::ExcelLoading
include DataShift::ImageLoading

# Options
#
# :file_name : Filename that will be loaded
# :verbose : Verbose logging and to STDOUT
# :strict : Raise exceptions when issues like missing mandatory columns
#
def initialize(options = {})
super(options)
# depending on version get_product_class should return us right class, namespaced or not

def initialize(klass, loader_object = nil, options = {})

super(klass, loader_object, options)

logger.info "Spree Loading initialised with:\n#{options.inspect}"
end

#TODO - ditch this backward compatability now and just go with namespaced ?
#
@@image_klass ||= DataShift::SpreeEcom::get_spree_class('Image')
@@option_type_klass ||= DataShift::SpreeEcom::get_spree_class('OptionType')
@@option_value_klass ||= DataShift::SpreeEcom::get_spree_class('OptionValue')
@@product_klass ||= DataShift::SpreeEcom::get_spree_class('Product')
@@property_klass ||= DataShift::SpreeEcom::get_spree_class('Property')
@@product_property_klass ||= DataShift::SpreeEcom::get_spree_class('ProductProperty')
@@stock_location_klass ||= DataShift::SpreeEcom::get_spree_class('StockLocation')
@@stock_movement_klass ||= DataShift::SpreeEcom::get_spree_class('StockMovement')
@@taxonomy_klass ||= DataShift::SpreeEcom::get_spree_class('Taxonomy')
@@taxon_klass ||= DataShift::SpreeEcom::get_spree_class('Taxon')
@@variant_klass ||= DataShift::SpreeEcom::get_spree_class('Variant')

end

def perform_load( options = {} )

# Options :
# :image_path_prefix : A common path to prefix before each image path
# e,g to specifiy particular drive {:image_path_prefix => 'C:\' }
#
def perform_load( file_name, opts = {} )
logger.info "SpreeBaseLoader - starting load from file [#{file_name}]"
super(file_name, opts)
end

# TOFIX - why is this in the base class when it looks like tis Prod/Vars ?
# either move it or make it generic so the owner can be any model that supports attachments

# Special case for Images
#
# A list of entries for Images.
Expand All @@ -66,15 +84,15 @@ def add_images( record )
#TODO - make this Delimiters::attributes_start_delim and support {alt=> 'blah, :position => 2 etc}

# Test and code for this saved at : http://www.rubular.com/r/1de2TZsVJz

@spree_uri_regexp ||= Regexp::new('(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?' )

if(image.match(@spree_uri_regexp))

uri, attributes = image.split(Delimiters::attribute_list_start)

uri.strip!

logger.info("Processing IMAGE from URI [#{uri.inspect}]")

if(attributes)
Expand All @@ -84,16 +102,16 @@ def add_images( record )
else
attributes = {} # will blow things up later if we pass nil where {} expected
end

agent = Mechanize.new

image = begin
agent.get(uri)
rescue => e
puts "ERROR: Failed to fetch image from URL #{uri}", e.message
raise DataShift::BadUri.new("Failed to fetch image from URL #{uri}")
end

# Expected image is_a Mechanize::Image
# image.filename& image.extract_filename do not handle query string well e,g blah.jpg?v=1234
# so for now use URI
Expand All @@ -105,49 +123,49 @@ def add_images( record )
logger.debug("Storing Image in TempFile #{base.inspect}.#{extname.inspect}")

@current_image_temp_file = Tempfile.new([base, extname], :encoding => 'ascii-8bit')

begin

# TODO can we handle embedded img src e.g from Mechanize::Page::Image ?

# If I call image.save(@current_image_temp_file.path) then it creates a new file with a .1 extension
# so the real temp file data is empty and paperclip chokes
# so this is a copy from the Mechanize::Image save method. don't like it much, very brittle, but what to do ...
until image.body_io.eof? do
@current_image_temp_file.write image.body_io.read 16384
end

end
@current_image_temp_file.rewind

logger.info("IMAGE downloaded from URI #{uri.inspect}")

attachment = create_attachment(Spree::Image, @current_image_temp_file.path, nil, nil, attributes)

rescue => e
logger.error(e.message)
logger.error("Failed to create Image from URL #{uri}")
raise DataShift::DataProcessingError.new("Failed to create Image from URL #{uri}")

ensure
ensure
@current_image_temp_file.close
@current_image_temp_file.unlink
end

else

else
path, alt_text = image.split(Delimiters::name_value_delim)

logger.debug("Processing IMAGE from PATH #{path.inspect} #{alt_text.inspect}")

path = File.join(config[:image_path_prefix], path) if(config[:image_path_prefix])

# create_attachment(klass, attachment_path, record = nil, attach_to_record_field = nil, options = {})
attachment = create_attachment(Spree::Image, path, nil, nil, :alt => alt_text)
end
end

begin
owner.images << attachment

logger.debug("Product assigned Image from : #{path.inspect}")
rescue => e
puts "ERROR - Failed to assign attachment to #{owner.class} #{owner.id}"
Expand All @@ -160,4 +178,4 @@ def add_images( record )

end
end
end
end
6 changes: 3 additions & 3 deletions lib/thor/spree/products_images.thor
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module DatashiftSpree

require 'product_loader'

loader = DataShift::SpreeEcom::ProductLoader.new( file_name: input, verbose: options[:verbose] )
loader = DataShift::SpreeEcom::ProductLoader.new( nil, {:verbose => options[:verbose]})

# YAML configuration file to drive defaults etc

Expand All @@ -66,7 +66,7 @@ module DatashiftSpree
opts = options.dup
opts[:mandatory] = ['sku', 'name', 'price']

loader.run(opts)
loader.perform_load(input, opts)
end


Expand Down Expand Up @@ -161,4 +161,4 @@ module DatashiftSpree
end

end
end
end
Loading

0 comments on commit 02de537

Please sign in to comment.