Class: Synaptic4r::Download

Inherits:
Result
  • Object
show all
Defined in:
lib/synaptic4r/result.rb

Instance Attribute Summary

Attributes inherited from Result

#headers, #http_request, #payload, #result, #sign, #url

Instance Method Summary collapse

Methods inherited from Result

#extract_header, #method_missing, #stringify_array, #stringify_hash

Constructor Details

#initialize(args) ⇒ Download

.….….….….….….….….….….….….….….….….….….….….….….….….……



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/synaptic4r/result.rb', line 270

def initialize(args)
  super   
  if args[:result]
    @body = args[:result].net_http_res.body
    @result[:content_type] = args[:result].headers[:content_type]
    if content_type.eql?('text/xml')
      @result[:directory] = directory_entries
    else
      @result[:file] = @body
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Synaptic4r::Result

Instance Method Details

#directory_entriesObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



293
294
295
296
297
# File 'lib/synaptic4r/result.rb', line 293

def directory_entries
  REXML::Document.new(@body).root.elements.to_a.first.elements.to_a.map do |i|
    i.elements.inject(nil, {}){|h,e| h.update(e.name.downcase.to_sym => e.text)}
  end
end

#octet_streamObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



313
314
315
# File 'lib/synaptic4r/result.rb', line 313

def octet_stream
  @body
end

.….….….….….….….….….….….….….….….….….….….….….….….….……



284
285
286
287
288
289
290
# File 'lib/synaptic4r/result.rb', line 284

def print
  if content_type.eql?('text/xml')
    print_directory_entries
  else
    @body
  end
end

.….….….….….….….….….….….….….….….….….….….….….….….….……



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/synaptic4r/result.rb', line 300

def print_directory_entries
  dir_list = directory_entries
  unless dir_list.empty?
    fmt = "%-30s %-10s %-s\n"
    fmt % ['Name', 'Type', 'OID'] +
    dir_list.inject("") do |r,e| 
      t = e[:filetype]
      r += fmt % [e[:filename], (t.eql?('regular') ? 'file' : t), e[:objectid]]
    end
  end
end