Class: Scaffolding::Parser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffolding/parser/base.rb

Direct Known Subclasses

Csv, Raw

Instance Method Summary collapse

Constructor Details

#initialize(source, name, auto, uri) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
# File 'lib/scaffolding/parser/base.rb', line 7

def initialize(source, name, auto, uri)
  @source, @name, @auto, @uri = source, name, auto, uri
  @errors = []
  return unless @data = valid_data?
  @col_seperator = col_seperator
  @source_name = clean_source_name
  @scaffolding = {}
  @row_number = 0
end

Instance Method Details

#build_stringObject



100
101
102
103
104
105
106
# File 'lib/scaffolding/parser/base.rb', line 100

def build_string
  scaffold = @source_name.dup
  @scaffolding.each do |k, v|
    scaffold << " #{k}:#{v}" unless k.to_s.downcase == "id"
  end
  scaffold
end

#clean_source_nameObject



25
26
27
28
# File 'lib/scaffolding/parser/base.rb', line 25

def clean_source_name
  name = (@name == "" ? File.basename(@source, ".*") : @name.dup)
  name.to_s.downcase.split.join.camelize.strip.singularize
end

#col_seperatorObject



60
61
62
63
64
# File 'lib/scaffolding/parser/base.rb', line 60

def col_seperator
  seperators = {}
  [",","\t"].each {|seperator| seperators[seperator] = @data.count(seperator)}
  seperators.max_by{|k,v| v}[0]
end

#data_typesObject



56
57
58
# File 'lib/scaffolding/parser/base.rb', line 56

def data_types
  {string: 0, date: 0, integer: 0, boolean: 0, decimal: 0, time: 0, datetime: 0}
end

#errorsObject



17
18
19
# File 'lib/scaffolding/parser/base.rb', line 17

def errors
  @errors unless @errors.count == 0
end

#fileObject



42
43
44
45
46
47
48
49
50
# File 'lib/scaffolding/parser/base.rb', line 42

def file
  ext = File.extname(@source)
  if [".csv", ".dat", ".txt"].include? ext
    File.read(utf8_encode(@source))
  else
    @errors << "Unknown source type #{ext} for #{@source}"
    false
  end
end

#import_dataObject



116
117
118
119
120
121
122
# File 'lib/scaffolding/parser/base.rb', line 116

def import_data
  @saved = 0
  @failed = 0
  process_data "save_row"
  errors
  {saved: @saved, failed: @failed}
end

#predict_row(row) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/scaffolding/parser/base.rb', line 66

def predict_row(row)
  row.each do |column, data|
    data_type = :string
    data_type = :boolean if ["true", "false"].include?(data.to_s.downcase)
    data_type = :time if (data =~ /^([01]?[0-9]|2[0-3])\:[0-5][0-9]$/) == 0
    data_type = :date if (data =~ /\A(?:0?[1-9]|[1-2]\d|3[01])\/(?:0?[1-9]|[1-2]\d|3[01])\/\d{4}\Z/) == 0
    data_type = :datetime if (data =~ /^([0,1]?\d{1})\/([0-2]?\d{1}|[3][0,1]{1})\/([1]{1}[9]{1}[9]{1}\d{1}|[2-9]{1}\d{3})\s([0]?\d|1\d|2[0-3]):([0-5]\d):([0-5]\d)$/) == 0
    data_type = :integer if Integer(data) rescue data_type
    data_type = :decimal if (data =~ (/[-]?\d*[,]?\d*[.]\d*[%]?$/)) == 0
    @scaffolding[column.to_sym][data_type] += 1 unless data == ""
  end
end

#resultsObject



108
109
110
111
112
113
114
# File 'lib/scaffolding/parser/base.rb', line 108

def results
  groom_data
  process_data "predict_row"
  errors
  scaffold_rank
  build_string
end

#save_row(row) ⇒ Object



79
80
81
82
# File 'lib/scaffolding/parser/base.rb', line 79

def save_row(row)
   model = @source_name.classify.constantize.new(row.except!(:id))
   model.save! ? @saved += 1 : @failed += 1
end

#scaffold_rankObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/scaffolding/parser/base.rb', line 84

def scaffold_rank
  unless @auto
    puts "\n\e[33mManually choose data types?(y/n) for #{@source_name}\e[0m"
    manual = STDIN.gets.chomp
  end
  @scaffolding.each do |scaffold, data_types|
    data_type = data_types.max_by{|k,v| v}[0]
      unless @auto || manual != "y"
        puts "\n\e[32m#{scaffold}\e[0m is a \e[33m#{data_type}\e[0m? ([Enter]/string/integer/date ect)"
        answer = STDIN.gets.chomp.downcase
        data_type = answer if data_types.keys.include?(answer.to_sym)
      end
    @scaffolding[scaffold] = data_type
  end
end

#source_nameObject



21
22
23
# File 'lib/scaffolding/parser/base.rb', line 21

def source_name
  @source_name
end

#utf8_encode(s) ⇒ Object



52
53
54
# File 'lib/scaffolding/parser/base.rb', line 52

def utf8_encode(s)
  s.force_encoding(Encoding::ISO_8859_1).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
end

#valid_data?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/scaffolding/parser/base.rb', line 30

def valid_data?
  if @source == "" || @source.nil?
    @errors << "No source selected"
    return false
  end
  @uri.nil? ? file : web
end

#webObject



38
39
40
# File 'lib/scaffolding/parser/base.rb', line 38

def web
  utf8_encode(`curl "#{@source}"`)
end