Commit 920cf802 authored by f's avatar f
Browse files

v0.2.0 -- generate random posts

parent 1b1b45ae
......@@ -22,7 +22,7 @@ module Sutty
end
map %w(--version -v) => :version
desc 'post NAME', 'Adds a post'
desc 'post', 'Adds a post'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
......@@ -36,8 +36,7 @@ module Sutty
method_option :title,
aliases: '-t',
type: :string,
desc: 'Title',
required: true
desc: 'Title'
method_option :date,
aliases: '-d',
......@@ -52,6 +51,13 @@ module Sutty
desc: 'Locale collection',
default: 'posts'
method_option :content,
aliases: '-c',
type: :string,
desc: 'Text length',
default: 'short',
enum: %w[short long]
def post
if options[:help]
invoke :help, ['post']
......
......@@ -16,7 +16,8 @@ module Sutty
CONTENT_FIELDS = %w[content markdown_content].freeze
def initialize(options)
@options = options
@options = options.to_h
@options['title'] ||= random_string(10)
end
def execute(input: $stdin, output: $stdout)
......@@ -25,7 +26,10 @@ module Sutty
return true
end
TTY::File.create_file path, YAML.dump(data) + "---\n\n"
TTY::File.create_file path,
YAML.dump(data) +
"---\n\n" +
random_text
end
private
......@@ -33,11 +37,13 @@ module Sutty
def data
return @data if @data
@data = Hash[(data_layout.keys - CONTENT_FIELDS).map { |k| [k, nil] }]
@data = (data_layout.keys - CONTENT_FIELDS).map do |k|
[k, generate_data(k)]
end.to_h
# Required fields
@data['title'] = options[:title]
@data['layout'] = options[:layout]
@data['title'] = options['title']
@data['layout'] = options['layout']
@data['uuid'] = SecureRandom.uuid
@data['liquid'] = false
......@@ -45,24 +51,112 @@ module Sutty
end
def data_layout
@data_layout ||= YAML.safe_load(File.read(File.join('_data', 'layouts', options[:layout] + '.yml')))
@data_layout ||= YAML.safe_load(File.read(File.join('_data', 'layouts', options['layout'] + '.yml')))
end
def slug
@slug ||= Jekyll::Utils.slugify(options[:title])
@slug ||= Jekyll::Utils.slugify(options['title'])
end
def dir
@dir ||= '_' + options[:locale]
@dir ||= '_' + options['locale']
end
def path
@path ||= File.join(dir, options[:date] + '-' + slug + '.markdown')
@path ||= File.join(dir, options['date'] + '-' + slug + '.markdown')
end
def logger
@logger ||= TTY::Logger.new
end
def generate_data(key)
# TODO: Generate private data
return if data_layout[key]['private']
# Sometimes data is optional
return unless data_layout[key]['required'] || random_boolean
case data_layout[key]['type']
when 'string' then random_text(1)
when 'text' then random_text(3)
when 'markdown' then random_text(3)
when 'number' then random_number(255)
when 'order' then random_number(255)
when 'tel'
require 'fake/phone_number'
Faker::PhoneNumber.phone_number
when 'date'
require 'faker/date'
Faker::Date.in_date_period
when 'array' then Array.new(random_number(10)) { random_string(random_number(3)) }
when 'predefined_array' then data_layout[key]['values'].keys.sample
when 'boolean' then random_boolean
when 'color' then random_color
when 'email'
require 'faker/internet'
Faker::Internet.email
when 'url'
require 'faker/internet'
Faker::Internet.url
when 'file' then random_file
when 'image' then random_file
when 'belongs_to' then random_post(key)
when 'has_many' then random_posts(key)
when 'has_and_belongs_to_many' then random_posts(key)
when 'locales' then random_posts(key)
when 'related_posts' then random_posts
when 'geo'
require 'faker/address'
{
'lat' => Faker::Address.latitude,
'lng' => Faker::Address.longitude
}
end
end
def random_number(digits)
rand(1..digits)
end
def random_text(longitude = nil)
require 'faker'
longitude ||= options['content'] == 'short' ? random_number(5) : random_number(20)
Faker::Lorem.paragraphs(number: longitude).join("\n\n")
end
def random_string(longitude = random_number(10))
require 'faker'
Faker::Lorem.sentence(word_count: longitude)
end
def random_color
Random.bytes(3).unpack1('H*')
end
def random_boolean
@random_boolean ||= [true,false]
@random_boolean.sample
end
def random_file
{
'path' => 'public/placeholder.png',
'description' => random_string(1)
}
end
# TODO: Implement relationships between posts
def random_post(key = nil)
SecureRandom.uuid
end
def random_posts(key = nil)
Array.new(random_number(10)) { random_post(key) }
end
end
end
end
......
......@@ -2,6 +2,6 @@
module Sutty
module Cli
VERSION = "0.1.2"
VERSION = "0.2.0"
end
end
......@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'tty-logger', '~> 0'
spec.add_dependency 'jekyll', '~> 4'
spec.add_dependency 'thor', '~> 1'
spec.add_dependency 'faker', '~> 2.14'
spec.add_development_dependency 'tty', '~> 0'
spec.add_development_dependency 'pry', '~> 0'
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment