module LoadData

Retrieves data from config folder to save to constants.

Public Class Methods

config_path() click to toggle source
# File lib/tagmemics/load_data.rb, line 3
def self.config_path
  File.join(File.dirname(__FILE__), '../../config')
end
contents_to_a(part_of_speech) click to toggle source
# File lib/tagmemics/load_data.rb, line 16
def self.contents_to_a(part_of_speech)
  list_contents(part_of_speech).split("\n")
end
list_contents(part_of_speech) click to toggle source
# File lib/tagmemics/load_data.rb, line 12
def self.list_contents(part_of_speech)
  File.new(list_path(part_of_speech), 'r:utf-8').read
end
list_path(part_of_speech) click to toggle source

Returns the absolute path to the adjectives list

# File lib/tagmemics/load_data.rb, line 8
def self.list_path(part_of_speech)
  File.join(config_path, "#{part_of_speech}.txt")
end
update_list(part_of_speech, uri, css_selector) click to toggle source
# File lib/tagmemics/load_data.rb, line 20
def self.update_list(part_of_speech, uri, css_selector)
  require 'mechanize'

  agent = Mechanize.new
  page = agent.get(uri)
  destination = "./config/#{part_of_speech}.txt"
  target = page.search(css_selector)
  regx = /[^\047a-zA-Z\s]/ # \047 is an apostrophe

  arr = []
  target.each do |x|
    x = x.text
    x.gsub(/\r\n\s/, "\n").split("\n").each do |word|
      next if arr.include? word
      next if regx =~ word
      arr << word
    end
  end
  arr.sort!

  puts "Starting list from #{uri}"
  puts "There are #{arr.count} #{part_of_speech} to save."

  File.open(destination, 'w') do |line|
    arr.each do |word|
      line << word + "\n"
    end
  end

  puts 'Save complete.'
end