Module: Pinport

Defined in:
lib/pinport.rb,
lib/pinport/error.rb,
lib/pinport/parser.rb,
lib/pinport/version.rb,
lib/pinport/generators/generator.rb

Defined Under Namespace

Modules: Generator Classes: DatabaseConnectionError, DirectoryArgumentError, Error, InvalidFileError, Parser, UnexpectedArgumentError

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.column_nameObject



49
50
51
# File 'lib/pinport.rb', line 49

def self.column_name
  @settings['schema']['column'] ||= self.settings['schema']['column']
end

.database_nameObject



41
42
43
# File 'lib/pinport.rb', line 41

def self.database_name
  @settings['database']['database'] ||= self.settings['database']['database']
end

.dbObject



25
26
27
# File 'lib/pinport.rb', line 25

def self.db
  @db ||= db_connection(self.settings['database'])
end

.db_connection(db_config) ⇒ Sequel::Mysql2::Database

Establishes a connection to the database

Parameters:

Returns:

  • (Sequel::Mysql2::Database)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pinport.rb', line 93

def self.db_connection(db_config)
  db_config = db_config.symbolize_keys
  db_config[:test] = true
  begin
    Sequel.mysql2(db_config)
  rescue => e
    puts e.message
    if @verbose_errors
      puts "Trace:\n"
      puts e.backtrace
    end
    exit 1
  end
end

.import_file(file, table = nil, column = nil, filter = nil, fix_newlines = true) ⇒ Object

Imports pins into database

Parameters:

  • file (String)

    path to the file to be imported

  • table (String) (defaults to: nil)

    the table name that pins should be imported into

  • column (String) (defaults to: nil)

    the column name pins should be inserted into, default: “pin”

  • filter (String) (defaults to: nil)

    a string of characters to filter out from each pin

  • fix_newlines (Boolean) (defaults to: true)

    specifies whether to fix newlines (in case of cross-OS incompatibilities), default: true



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/pinport.rb', line 115

def self.import_file(file, table = nil, column = nil, filter = nil, fix_newlines = true)
  # convert supplied file path to absolute
  file_original = "#{file}"
  file = File.absolute_path("#{file}")
  if !File.exists?(file)
    raise Pinport::InvalidFileError
  elsif File.directory?(file)
    raise Pinport::DirectoryArgumentError
  end

  # escape filename as precaution
  # file = Shellwords.shellescape(file)
  source = File.open(file)
  source_dirname = File.dirname(file)
  source_basename = File.basename(file, '.txt')
  cleaned_filename = "#{source_dirname}/#{source_basename}-cleaned.txt"
  sorted_filename = "#{source_dirname}/#{source_basename}-sorted.txt"

  # clean up txt files
  cleaned = Pinport::Parser.dos2unix(file) unless fix_newlines == false
  cleaned = Pinport::Parser.strip_chars(cleaned, filter, cleaned_filename) unless filter == nil

  # perform the sort, if successful...
  if Pinport::Parser.sort_file_contents(cleaned, sorted_filename)

    # build a Sequel dataset and insert pins into it
    # retrieve table/column names from schema if not specified
    puts  "Importing..."
    table = self.table_name if table == nil
    column = self.column_name if column == nil
    pins = self.db[table.to_sym]

    # open the sorted file and insert each pin line-by-line
    count = 0
    start_time = Time.now
    File.open(sorted_filename).each_line do |line|
      pins.insert(column.to_sym => line.chomp)
      count += 1
      puts "Imported #{count} pins." if (count % self.notifications['import']['every'] == 0)
    end
    puts "Finished importing #{count} pins into #{table}."

    # remove any temporary files that were created
    File.delete(cleaned_filename) if File.exists?(cleaned_filename)
    File.delete(sorted_filename) if File.exists?(sorted_filename)
  else
    puts "Error encountered while sorting pins in file."
    return false
  end
end

.import_folder(folder, extension = "txt", table = nil, column = nil, filter = nil, fix_newlines = true) ⇒ Object

Imports pins from folder containing pin files

Accepts the same parameters as ‘import_file` along with the following:

Parameters:

  • extension (String) (defaults to: "txt")

    file extension of files to be imported, default: ‘txt’



170
171
172
173
174
175
176
# File 'lib/pinport.rb', line 170

def self.import_folder(folder, extension = "txt", table = nil, column = nil, filter = nil, fix_newlines = true)
  folder = File.absolute_path(folder)
  Dir.glob("#{folder}/*.#{extension}").each do |file|
    file = File.absolute_path("#{file}")
    import_file(file, table, column, filter, fix_newlines)
  end
end

.initialize(file) ⇒ Object



19
20
21
22
23
# File 'lib/pinport.rb', line 19

def self.initialize(file)
  @db = db_connection(self.settings['database'])
  @verbose_errors = self.settings['debug']['errors']['verbose']
  self.settings_file = file
end

.load_config_file(file) ⇒ Hash

Loads a config file to use with pinport

Parameters:

  • file (String)

    the config file to load

Returns:

  • (Hash)

    Parsed settings as a hash



56
57
58
59
60
61
62
63
# File 'lib/pinport.rb', line 56

def self.load_config_file(file)
  if File.exists?(file)
    puts "Loading config file: #{file}"
    return YAML.load_file(file)
  else
    puts "Could not find config file: #{file}"
  end
end

.notificationsObject



86
87
88
# File 'lib/pinport.rb', line 86

def self.notifications
  @notifications ||= self.settings['notifications']
end

.rootObject



82
83
84
# File 'lib/pinport.rb', line 82

def self.root
  File.expand_path '../..', __FILE__
end

.schemaObject



37
38
39
# File 'lib/pinport.rb', line 37

def self.schema
  @settings['schema'] ||= self.settings['schema']
end

.settingsObject



29
30
31
# File 'lib/pinport.rb', line 29

def self.settings
  @settings ||= YAML.load_file(self.settings_file)
end

.settings=(new_settings) ⇒ Object



33
34
35
# File 'lib/pinport.rb', line 33

def self.settings=(new_settings)
  @settings = new_settings
end

.settings_fileObject



78
79
80
# File 'lib/pinport.rb', line 78

def self.settings_file
  @settings_file ||= 'config.yml'
end

.settings_file=(file) ⇒ Object

Updates settings_file and settings vars if specified config file can be found

Parameters:

  • file (String)

    the config file to be loaded for settings



68
69
70
71
72
73
74
75
76
# File 'lib/pinport.rb', line 68

def self.settings_file=(file)
  if File.exists?(file)
    @settings_file = file
    self.settings = load_config_file(@settings_file)
    return true
  else
    raise "Specified settings file could assigned."
  end
end

.table_nameObject



45
46
47
# File 'lib/pinport.rb', line 45

def self.table_name
  @settings['schema']['table'] ||= self.settings['schema']['table']
end