class Sutty::Cli::Commands::Post
Constants
- CONTENT_FIELDS
Attributes
options[R]
Public Class Methods
new(options)
click to toggle source
# File lib/sutty/cli/commands/post.rb 19 def initialize(options) 20 @options = options.to_h 21 @options['title'] ||= random_string(10) 22 end
Public Instance Methods
execute(input: $stdin, output: $stdout)
click to toggle source
# File lib/sutty/cli/commands/post.rb 24 def execute(input: $stdin, output: $stdout) 25 if File.exist? path 26 logger.info "The file #{path} already exists" 27 return true 28 end 29 30 TTY::File.create_file path, 31 YAML.dump(data) + 32 "---\n\n" + 33 (content? ? random_markdown : '') 34 end
Private Instance Methods
content?()
click to toggle source
# File lib/sutty/cli/commands/post.rb 164 def content? 165 @content ||= CONTENT_FIELDS.map { |f| types.include? f }.any? 166 end
data()
click to toggle source
# File lib/sutty/cli/commands/post.rb 38 def data 39 return @data if @data 40 41 @data = (data_layout.keys - CONTENT_FIELDS).map do |k| 42 [k, generate_data(k)] 43 end.to_h 44 45 # Required fields 46 @data['title'] = options['title'] 47 @data['layout'] = options['layout'] 48 @data['uuid'] = SecureRandom.uuid 49 @data['liquid'] = false 50 51 @data 52 end
data_layout()
click to toggle source
# File lib/sutty/cli/commands/post.rb 54 def data_layout 55 @data_layout ||= YAML.safe_load(File.read(File.join('_data', 'layouts', options['layout'] + '.yml'))) 56 end
dir()
click to toggle source
# File lib/sutty/cli/commands/post.rb 62 def dir 63 @dir ||= '_' + options['locale'] 64 end
generate_data(key)
click to toggle source
# File lib/sutty/cli/commands/post.rb 74 def generate_data(key) 75 # TODO: Generate private data 76 return if data_layout[key]['private'] 77 # Sometimes data is optional 78 return unless data_layout[key]['required'] || random_boolean 79 80 case data_layout[key]['type'] 81 when 'string' then random_text(1) 82 when 'text' then random_text(3) 83 when 'markdown' then random_markdown(3) 84 when 'number' then random_number(255) 85 when 'order' then random_number(255) 86 when 'tel' then Faker::PhoneNumber.phone_number 87 when 'date' then Faker::Date.in_date_period 88 when 'array' then Array.new(random_number(10)) { random_string(random_number(3)) } 89 when 'predefined_array' then data_layout[key]['values'].keys.sample 90 when 'boolean' then random_boolean 91 when 'color' then random_color 92 when 'email' then Faker::Internet.email 93 when 'url' then Faker::Internet.url 94 when 'file' then random_file 95 when 'image' then random_file 96 when 'belongs_to' then random_post(key) 97 when 'has_many' then random_posts(key) 98 when 'has_and_belongs_to_many' then random_posts(key) 99 when 'locales' then random_posts(key) 100 when 'related_posts' then random_posts 101 when 'geo' 102 { 103 'lat' => Faker::Address.latitude, 104 'lng' => Faker::Address.longitude 105 } 106 end 107 end
logger()
click to toggle source
# File lib/sutty/cli/commands/post.rb 70 def logger 71 @logger ||= TTY::Logger.new 72 end
long?()
click to toggle source
# File lib/sutty/cli/commands/post.rb 130 def long? 131 options['content'] == 'long' 132 end
path()
click to toggle source
# File lib/sutty/cli/commands/post.rb 66 def path 67 @path ||= File.join(dir, options['date'] + '-' + slug + '.markdown') 68 end
random_boolean()
click to toggle source
# File lib/sutty/cli/commands/post.rb 138 def random_boolean 139 @random_boolean ||= [true,false] 140 141 @random_boolean.sample 142 end
random_color()
click to toggle source
# File lib/sutty/cli/commands/post.rb 134 def random_color 135 Random.bytes(3).unpack1('H*') 136 end
random_file()
click to toggle source
# File lib/sutty/cli/commands/post.rb 144 def random_file 145 { 146 'path' => 'public/placeholder.png', 147 'description' => random_string(1) 148 } 149 end
random_markdown(longitude = nil)
click to toggle source
# File lib/sutty/cli/commands/post.rb 123 def random_markdown(longitude = nil) 124 longitude ||= long? ? random_number(20) : random_number(5) 125 126 Faker::Markdown.sandwich(sentences: longitude, 127 repeat: long? ? random_number(5) : 1) 128 end
random_number(digits)
click to toggle source
# File lib/sutty/cli/commands/post.rb 109 def random_number(digits) 110 rand(1..digits) 111 end
random_post(key = nil)
click to toggle source
TODO: Implement relationships between posts
# File lib/sutty/cli/commands/post.rb 152 def random_post(key = nil) 153 SecureRandom.uuid 154 end
random_posts(key = nil)
click to toggle source
# File lib/sutty/cli/commands/post.rb 156 def random_posts(key = nil) 157 Array.new(random_number(10)) { random_post(key) } 158 end
random_string(longitude = random_number(10))
click to toggle source
# File lib/sutty/cli/commands/post.rb 119 def random_string(longitude = random_number(10)) 120 Faker::Lorem.sentence(word_count: longitude) 121 end
random_text(longitude = nil)
click to toggle source
# File lib/sutty/cli/commands/post.rb 113 def random_text(longitude = nil) 114 longitude ||= long? ? random_number(20) : random_number(5) 115 116 Faker::Lorem.paragraphs(number: longitude).join("\n\n") 117 end
slug()
click to toggle source
# File lib/sutty/cli/commands/post.rb 58 def slug 59 @slug ||= Jekyll::Utils.slugify(options['title']) 60 end
types()
click to toggle source
# File lib/sutty/cli/commands/post.rb 160 def types 161 @types ||= data_layout.values.map { |v| v['type'] }.uniq 162 end