Class: DataShift::SpreeBaseLoader

Inherits:
LoaderBase
  • Object
show all
Includes:
CsvLoading, ExcelLoading, ImageLoading
Defined in:
lib/loaders/spree/spree_base_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, find_operators = true, loader_object = nil, options = {:instance_methods => true}) ⇒ SpreeBaseLoader

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/loaders/spree/spree_base_loader.rb', line 26

def initialize(klass, find_operators = true, loader_object = nil, options = {:instance_methods => true})

  super(klass, find_operators, loader_object, options)

  @@image_klass ||= DataShift::SpreeHelper::get_spree_class('Image')
  @@option_type_klass ||= DataShift::SpreeHelper::get_spree_class('OptionType')
  @@option_value_klass ||= DataShift::SpreeHelper::get_spree_class('OptionValue')
  @@product_klass ||= DataShift::SpreeHelper::get_spree_class('Product')
  @@property_klass ||= DataShift::SpreeHelper::get_spree_class('Property')
  @@product_property_klass ||= DataShift::SpreeHelper::get_spree_class('ProductProperty')
  @@taxonomy_klass ||= DataShift::SpreeHelper::get_spree_class('Taxonomy')
  @@taxon_klass ||= DataShift::SpreeHelper::get_spree_class('Taxon')
  @@variant_klass ||= DataShift::SpreeHelper::get_spree_class('Variant')
end

Instance Method Details

#add_images(record) ⇒ Object

Special case for Images

A list of entries for Images.

Multiple image items can be delimited by Delimiters::multi_assoc_delim

Each item can contain optional attributes for the Image class within {}.

For example to supply the optional ‘alt’ text, or position for an image

Example => path_1{:alt => text}|path_2{:alt => more alt blah blah, :position => 5}|path_3{:alt => the alt text for this path}


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/loaders/spree/spree_base_loader.rb', line 64

def add_images( record )

  #save_if_new

  # different versions have moved images around from Prod to Variant
  owner = DataShift::SpreeHelper::get_image_owner(record)

  get_each_assoc.each do |image|

    logger.debug("Processing IMAGE from #{image.inspect}")
         
    #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\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?' )
    
    if(image.match(@spree_uri_regexp))
       
      uri, attributes = image.split(Delimiters::attribute_list_start)
      
      uri.strip!
      
      logger.debug("Processing IMAGE from an URI #{uri.inspect} #{attributes.inspect}")
      
      if(attributes)
        #TODO move to ColumnPacker unpack ?
        attributes = attributes.split(', ').map{|h| h1,h2 = h.split('=>'); {h1.strip! => h2.strip!}}.reduce(:merge)
      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 class Mechanize::Image 
  
      # there is also an method called image.extract_filename - not sure of difference
      extname = image.respond_to?(:filename) ? File.extname(image.filename) : File.extname(uri)
      base = image.respond_to?(:filename) ? File.basename(image.filename, '.*') : File.basename(uri, '.*')
      
      @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           
        
        @current_image_temp_file.rewind

        # create_attachment(klass, attachment_path, record = nil, attach_to_record_field = nil, options = {})
        attachment = create_attachment(@@image_klass, @current_image_temp_file.path, nil, nil, attributes)
        
      rescue => e
        puts "ERROR: Failed to process image from URL #{uri}", e.message
        logger.error("Failed to create Image from URL #{uri}")
        raise DataShift::DataProcessingError.new("Failed to create Image from URL #{uri}")
   
      ensure 
        @current_image_temp_file.close
        @current_image_temp_file.unlink
      end

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

      logger.debug("Processing IMAGE from PATH #{path.inspect} #{alt_text.inspect}")
      
      path = File.join(@options[:image_path_prefix], path) if(@options[:image_path_prefix])

      # create_attachment(klass, attachment_path, record = nil, attach_to_record_field = nil, options = {})
      attachment = create_attachment(@@image_klass, path, nil, nil, :alt => alt_text)
    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}"
      logger.error("Failed to assign attachment to #{owner.class} #{owner.id}")
    end

  end

  record.save

end

#perform_load(file_name, opts = {}) ⇒ Object

Options :

:image_path_prefix : A common path to prefix before each image path
                     e,g to specifiy particular drive  {:image_path_prefix => 'C:\' }


46
47
48
49
50
# File 'lib/loaders/spree/spree_base_loader.rb', line 46

def perform_load( file_name, opts = {} )
  @options = opts.dup

  super(file_name, @options)
end