spreader 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/spreader.rb +35 -16
  2. metadata +2 -2
data/lib/spreader.rb CHANGED
@@ -7,30 +7,48 @@
7
7
  # http://libxml.rubyforge.org/svn/tags/REL_0_9_2/README
8
8
  # http://guides.rubygems.org/make-your-own-gem/
9
9
  # http://visual.merriam-webster.com/plants-gardening/gardening/seeding-planting-tools_2.php
10
+ # http://stackoverflow.com/questions/2741260/can-ruby-access-output-from-shell-commands-as-it-appears
11
+ # http://www.wallpaperama.com/forums/how-to-count-the-number-of-lines-in-a-file-in-linux-shell-command-t1084.html
10
12
 
11
13
  class Spreader
12
14
  #require 'rubygems'
13
15
  require 'xml'
16
+ @xml = false
14
17
 
15
- def extract(nodes)
16
- return yield nodes
18
+ def extract(data)
19
+ return yield data
20
+ end
21
+
22
+ def xml?(file)
23
+ head = (IO.popen('head -' + IO.popen("wc -l #{file}").gets.split(' ').first + " #{file}"))
24
+ while (line = head.gets)
25
+ @xml = true if (line.include? '<') && (!@xml)
26
+ end
27
+
28
+ return @xml
17
29
  end
18
30
 
19
- def transform(nodes, model_name, latitude_field_name, longitude_field_name)
20
- prev_name = ''
31
+ def transform(data, model_name, latitude_field_name, longitude_field_name)
21
32
  seeds = ''
22
- # i = 0
23
-
24
- while nodes.read
25
- unless nodes.node_type == XML::Reader::TYPE_END_ELEMENT
26
- seeds << model_name + '.create(:' + longitude_field_name + ' => ' + nodes.value.to_s.split(',').first + ', ' + ':' + latitude_field_name + ' => ' + nodes.value.to_s.split(',')[1] + ")\n" if (prev_name == 'coordinates')
27
- # seeds << 'thing_' + (i+=1).to_s + ":\n lat: " + nodes.value.to_s.split(',')[1] + "\n lng: " + nodes.value.to_s.split(',').first + "\n\n" if (prev_name == 'coordinates')
28
- prev_name = nodes.name
33
+ if @xml
34
+ prev_name = ''
35
+ # i = 0
36
+
37
+ while data.read
38
+ unless data.node_type == XML::Reader::TYPE_END_ELEMENT
39
+ seeds << model_name + '.create(:' + longitude_field_name + ' => ' + data.value.to_s.split(',').first + ', ' + ':' + latitude_field_name + ' => ' + data.value.to_s.split(',')[1] + ")\n" if (prev_name == 'coordinates')
40
+ # seeds << 'thing_' + (i+=1).to_s + ":\n lat: " + nodes.value.to_s.split(',')[1] + "\n lng: " + nodes.value.to_s.split(',').first + "\n\n" if (prev_name == 'coordinates')
41
+ prev_name = data.name
42
+ end
43
+ end
44
+ # puts seeds.chop
45
+ else
46
+ data.each do |datum|
47
+ seeds << model_name + '.create(:' + longitude_field_name + ' => ' + datum.split(',').first + ', ' + ':' + latitude_field_name + ' => ' + datum.split(',').last.chop + ")\n"
29
48
  end
30
49
  end
31
50
 
32
- nodes.close
33
- # puts seeds.chop
51
+ data.close
34
52
  return seeds
35
53
  end
36
54
 
@@ -40,12 +58,13 @@ class Spreader
40
58
  system('rake db:seed')
41
59
  end
42
60
 
43
- def transformLoad(nodes, model_name, latitude_field_name, longitude_field_name)
44
- load(transform(nodes, model_name, latitude_field_name, longitude_field_name))
61
+ def transformLoad(data, model_name, latitude_field_name, longitude_field_name)
62
+ load(transform(data, model_name, latitude_field_name, longitude_field_name))
45
63
  end
46
64
 
47
65
  def self.seed(filename, model_name, latitude_field_name, longitude_field_name)
48
66
  spreader = Spreader.new
49
- spreader.transformLoad(spreader.extract(filename){|nodes| XML::Reader.file("#{nodes}", :options => XML::Parser::Options::NOBLANKS | XML::Parser::Options::NOENT)}, model_name, latitude_field_name, longitude_field_name)
67
+ spreader.transformLoad(spreader.extract(filename){|data| XML::Reader.file("#{data}", :options => XML::Parser::Options::NOBLANKS | XML::Parser::Options::NOENT)}, model_name, latitude_field_name, longitude_field_name) if spreader.xml?(filename)
68
+ spreader.transformLoad(spreader.extract(filename){|data| File.open("#{data}")}, model_name, latitude_field_name, longitude_field_name) if !spreader.xml?(filename)
50
69
  end
51
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-28 00:00:00.000000000 Z
12
+ date: 2012-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: libxml-ruby