Class: MagnoliaClient::ImportExportBase

Inherits:
Object
  • Object
show all
Defined in:
lib/magnolia_i_e.rb

Overview

Class that does the actual import export

Direct Known Subclasses

Export, Import

Constant Summary collapse

MAGNOLIA_BASE_URL =
"/.magnolia/pages/"
MAGNOLIA_IMPORT_URL =
MAGNOLIA_BASE_URL + "import.html"
MAGNOLIA_EXPORT_URL =
MAGNOLIA_BASE_URL + "export.html"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ImportExportBase

No checking of options, this is done in the parser assign values from the hash to attributes



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/magnolia_i_e.rb', line 156

def initialize(options)
  @console = options.console
  @action = options.action 
  @server_url = options.server_url
  @repository_path = options.repository_path
  @workspace = options.workspace
  @user = options.user
  @password = options.password
  @verbose = options.verbose
  @options = options
  @out = outfile_path
  @buffer = options.buffer
end

Instance Method Details

#execObject

main method this create the necessary http client to simulate interaction and just post the data collected from subclasses to the form.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/magnolia_i_e.rb', line 207

def exec 
  client = HTTPAccess2::Client.new
  boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
  extheader = {'content-type' => "multipart/form-data; boundary=___#{ boundary }___"}
  client.set_basic_auth(@server_url, @user, @password)     
          
  form_data = pad_data(prepare_form_data)
          
  verbose(form_data) if @verbose
          
  if @buffer
    process_with_buffer(client, form_data, extheader)
  else
    process_with_no_buffer(client, form_data, extheader)
  end
end

#outfile_pathObject

compute the outfile path take special care when the repository_path is the root



173
174
175
176
177
178
179
180
# File 'lib/magnolia_i_e.rb', line 173

def outfile_path
  subr = if @repository_path == '/' 
    ''
  else
    @repository_path.gsub('/','.') 
  end
  File.expand_path(@options.out+'/'+@workspace+subr+'.xml')
end

#pad_data(data) ⇒ Object

trying to fix a bug in http-access2 where all the parameters values are loosing their last char can’t find a reason for that.



192
193
194
195
196
197
198
199
200
201
# File 'lib/magnolia_i_e.rb', line 192

def pad_data(data)
    data.each { |key,value| begin
      if not key==:mgnlFileImport 
        value = value + " "; 
        data[key] = value.to_s
      end
    rescue
    end
  }
end

#prepare_form_dataObject

prepare form data. Should return a hash with the needed parameters this has to be implemented in the subclasses



185
186
187
# File 'lib/magnolia_i_e.rb', line 185

def prepare_form_data
  raise "Sub classing class need to implement this method"
end

#process_with_buffer(client, form_data, extheader) ⇒ Object

use ruby blocks to process the response from the server



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/magnolia_i_e.rb', line 242

def process_with_buffer(client, form_data, extheader)
  if @console
    client.post_content(@full_url, form_data, extheader) do |res|
      res.each('</sv:node>') do |line|         
        puts line
      end
    end
  else
    f = File.open(@out,'w')
    client.post_content(@full_url, form_data, extheader) do |res|
      client.post_content(@full_url, form_data, extheader) do |res|
        res.each do |line|             
          f <<  line
          f.flush
        end
      end
      f.flush
      f.close
      puts "Output of action "+@action+" has been written to "+@out if @verbose
    end
  end
end

#process_with_no_buffer(client, form_data, extheader) ⇒ Object

use a simple variable to process the response from the server



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/magnolia_i_e.rb', line 227

def process_with_no_buffer(client, form_data, extheader)
  res = client.post_content(@full_url, form_data, extheader) 
  if @console
    puts res
  else
    f = File.open(@out,'w')
    f.puts res
    f.flush
    f.close
    puts "Output of action "+@action+" has been written to "+@out if @verbose         
  end
end

#verbose(form_data) ⇒ Object

Verbose output before action



267
268
269
270
271
272
273
# File 'lib/magnolia_i_e.rb', line 267

def verbose(form_data)
  puts "------------ \tOptions data\t ---------------"
  puts @options.to_yaml
  puts "------------ \tPost data\t\t ---------------"
  puts form_data.to_yaml
  puts "------------ \tEnd data\t\t ---------------"
end