module Pinport

Constants

VERSION

Public Class Methods

column_name() click to toggle source
# File lib/pinport.rb, line 49
def self.column_name
  @settings['schema']['column'] ||= self.settings['schema']['column']
end
database_name() click to toggle source
# File lib/pinport.rb, line 41
def self.database_name
  @settings['database']['database'] ||= self.settings['database']['database']
end
db() click to toggle source
# File lib/pinport.rb, line 25
def self.db
  @db ||= db_connection(self.settings['database'])
end
db_connection(db_config) click to toggle source

Establishes a connection to the database @param db_config [String] path to a YAML file specifying database connection details, refer to mysql2 connection options for available settings (github.com/brianmario/mysql2#connection-options). @return [Sequel::Mysql2::Database]

# 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) click to toggle source

Imports pins into database

@param file [String] path to the file to be imported @param table [String] the table name that pins should be imported into @param column [String] the column name pins should be inserted into, default: “pin” @param filter [String] a string of characters to filter out from each pin @param fix_newlines [Boolean] specifies whether to fix newlines (in case of cross-OS incompatibilities), default: true

# 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) click to toggle source

Imports pins from folder containing pin files

Accepts the same parameters as ‘import_file` along with the following: @param extension [String] file extension of files to be imported, default: ’txt’

# 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) click to toggle source
# 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) click to toggle source

Loads a config file to use with pinport @param file [String] the config file to load @return [Hash] Parsed settings as a hash

# 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
notifications() click to toggle source
# File lib/pinport.rb, line 86
def self.notifications
  @notifications ||= self.settings['notifications']
end
root() click to toggle source
# File lib/pinport.rb, line 82
def self.root
  File.expand_path '../..', __FILE__
end
schema() click to toggle source
# File lib/pinport.rb, line 37
def self.schema
  @settings['schema'] ||= self.settings['schema']
end
settings() click to toggle source
# File lib/pinport.rb, line 29
def self.settings
  @settings ||= YAML.load_file(self.settings_file)
end
settings=(new_settings) click to toggle source
# File lib/pinport.rb, line 33
def self.settings=(new_settings)
  @settings = new_settings
end
settings_file() click to toggle source
# File lib/pinport.rb, line 78
def self.settings_file
  @settings_file ||= 'config.yml'
end
settings_file=(file) click to toggle source

Updates settings_file and settings vars if specified config file can be found @param file [String] the config file to be loaded for settings

# 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_name() click to toggle source
# File lib/pinport.rb, line 45
def self.table_name
  @settings['schema']['table'] ||= self.settings['schema']['table']
end