Class: Journal::Checkin
- Inherits:
-
Object
- Object
- Journal::Checkin
- Defined in:
- lib/journal-cli/checkin.rb
Overview
Main class
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#journal ⇒ Object
readonly
Returns the value of attribute journal.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#add_title(string) ⇒ Object
Add a title (Markdown) to the output.
-
#go ⇒ Object
Finalize the checkin, saving data to JSON, Day One, and Markdown as configured.
-
#header(string) ⇒ Object
Add a question header (Markdown) to the output.
-
#hr ⇒ Object
Add a horizontal rule (Markdown) to the output.
-
#initialize(journal) ⇒ Checkin
constructor
Initialize a new checkin using a configured journal.
-
#launch_day_one ⇒ Object
Launch Day One and quit if it wasn’t running.
-
#newline ⇒ Object
Add a newline to the output.
- #print_answer(prompt, type, key, data) ⇒ Object
-
#save_daily_markdown ⇒ Object
Save journal entry to daily Markdown file.
- #save_data ⇒ Object
-
#save_day_one_entry ⇒ Object
Save journal entry to Day One using the command line tool.
-
#save_individual_markdown ⇒ Object
Save journal entry to an new individual Markdown file.
-
#save_single_markdown ⇒ Object
Save entry to an existing Markdown file.
-
#section(string) ⇒ Object
Add a section header (Markdown) to the output.
- #to_markdown(yaml: false, title: false, date: false, time: false) ⇒ Object
- #weather_to_yaml(answers) ⇒ Object
Constructor Details
#initialize(journal) ⇒ Checkin
Initialize a new checkin using a configured journal
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/journal-cli/checkin.rb', line 11 def initialize(journal) @key = journal @output = [] @date = Journal.date @date.localtime raise StandardError, "No journal with key #{@key} found" unless Journal.config["journals"].key? @key @journal = Journal.config["journals"][@key] @sections = Sections.new(@journal["sections"]) @data = {} meridian = (@date.hour < 13) ? "AM" : "PM" @title = @journal["title"].sub(/%M/, meridian) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def data @data end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def date @date end |
#journal ⇒ Object (readonly)
Returns the value of attribute journal.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def journal @journal end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def key @key end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def output @output end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def sections @sections end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
4 5 6 |
# File 'lib/journal-cli/checkin.rb', line 4 def title @title end |
Instance Method Details
#add_title(string) ⇒ Object
Add a title (Markdown) to the output
32 33 34 |
# File 'lib/journal-cli/checkin.rb', line 32 def add_title(string) @output << "\n## #{string}\n" unless string.nil? end |
#go ⇒ Object
Finalize the checkin, saving data to JSON, Day One, and Markdown as configured
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/journal-cli/checkin.rb', line 72 def go @sections.each { |key, section| @data[key] = section } save_data save_day_one_entry if @journal["dayone"] return unless @journal["markdown"] case @journal["markdown"] when /^da(y|ily)/ save_daily_markdown when /^(ind|sep)/ save_individual_markdown else save_single_markdown end end |
#header(string) ⇒ Object
Add a question header (Markdown) to the output
41 42 43 |
# File 'lib/journal-cli/checkin.rb', line 41 def header(string) @output << "\n##### #{string}\n" unless string.nil? end |
#hr ⇒ Object
Add a horizontal rule (Markdown) to the output
64 65 66 |
# File 'lib/journal-cli/checkin.rb', line 64 def hr @output << "\n* * * * * *\n" end |
#launch_day_one ⇒ Object
Launch Day One and quit if it wasn’t running
93 94 95 96 97 98 99 100 101 |
# File 'lib/journal-cli/checkin.rb', line 93 def launch_day_one # Launch Day One to ensure database is up-to-date # test if Day One is open @running = !`ps ax | grep "/MacOS/Day One" | grep -v grep`.strip.empty? # -g do not bring app to foreground # -j launch hidden `/usr/bin/open -gj -a "Day One"` sleep 3 end |
#newline ⇒ Object
Add a newline to the output
57 58 59 |
# File 'lib/journal-cli/checkin.rb', line 57 def newline @output << "\n" end |
#print_answer(prompt, type, key, data) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/journal-cli/checkin.rb', line 196 def print_answer(prompt, type, key, data) return if data.nil? || !data.key?(key) || data[key].nil? case type when /^(weather|forecast|moon)/ header prompt @output << case type when /current$/ data[key].current when /moon$/ "Moon phase: #{data[key].moon}" else data[key].to_markdown end when /^(int|num)/ @output << "#{prompt}: #{data[key]} " unless data[key].nil? when /^date/ @output << "#{prompt}: #{data[key].strftime("%Y-%m-%d %H:%M")}" unless data[key].nil? else unless data[key].strip.empty? header prompt @output << data[key] end hr end end |
#save_daily_markdown ⇒ Object
Save journal entry to daily Markdown file
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/journal-cli/checkin.rb', line 154 def save_daily_markdown dir = if @journal.key?("entries_folder") File.join(File.(@journal["entries_folder"]), "entries") elsif Journal.config.key?("entries_folder") File.join(File.(Journal.config["entries_folder"]), @key) else File.join(File.("~/.local/share/journal/#{@key}/entries")) end FileUtils.mkdir_p(dir) unless File.directory?(dir) @date.localtime filename = "#{@key}_#{@date.strftime("%Y-%m-%d")}.md" target = File.join(dir, filename) if File.exist? target File.open(target, "a") { |f| f.puts to_markdown(yaml: false, title: true, date: false, time: true) } else File.open(target, "w") { |f| f.puts to_markdown(yaml: true, title: true, date: false, time: true) } end Journal.notify "{bg}Saved daily Markdown to {bw}#{target}" end |
#save_data ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/journal-cli/checkin.rb', line 299 def save_data @date.localtime dir = if @journal.key?("entries_folder") File.(@journal["entries_folder"]) elsif Journal.config.key?("entries_folder") File.(Journal.config["entries_folder"]) else File.("~/.local/share/journal") end FileUtils.mkdir_p(dir) unless File.directory?(dir) db = File.join(dir, "#{@key}.json") data = if File.exist?(db) JSON.parse(IO.read(db)) else [] end date = @date.utc output = {} @data.each do |jk, journal| output[jk] = {} journal.answers.each do |k, v| if v.is_a? Hash v.each do |key, value| result = case value.class.to_s when /Weather/ case key when /current$/ { "temp" => value.data[:temp], "condition" => value.data[:current_condition] } when /moon(_?phase)?$/ { "phase" => value.data[:moon_phase] } else { "high" => value.data[:high], "low" => value.data[:low], "condition" => value.data[:condition], "moon_phase" => value.data[:moon_phase] } end else value end if jk == k output[jk][key] = result else output[jk][k] ||= {} output[jk][k][key] = result end end elsif jk == k output[jk] = v else output[jk][k] = v end end end data << {"date" => date, "data" => output} data.map! do |d| { "date" => d["date"].is_a?(String) ? Time.parse(d["date"]) : d["date"], "data" => d["data"] } end data.sort_by! { |e| e["date"] } File.open(db, "w") { |f| f.puts JSON.pretty_generate(data) } Journal.notify "{bg}Saved {bw}#{db}" end |
#save_day_one_entry ⇒ Object
Save journal entry to Day One using the command line tool
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/journal-cli/checkin.rb', line 106 def save_day_one_entry unless TTY::Which.exist?("dayone2") Journal.notify("{br}Day One CLI not installed, no Day One entry created") return end launch_day_one @date.localtime cmd = ["dayone2"] cmd << %(-j "#{@journal["journal"]}") if @journal.key?("journal") cmd << %(-t #{@journal["tags"].join(" ")}) if @journal.key?("tags") cmd << %(-date "#{@date.strftime("%Y-%m-%d %I:%M %p")}") `echo #{Shellwords.escape(to_markdown(yaml: false, title: true))} | #{cmd.join(" ")} -- new` Journal.notify("{bg}Entered one entry into Day One") # quit if it wasn't running `osascript -e 'tell app "Day One" to quit'` if !@running end |
#save_individual_markdown ⇒ Object
Save journal entry to an new individual Markdown file
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/journal-cli/checkin.rb', line 178 def save_individual_markdown dir = if @journal.key?("entries_folder") File.join(File.(@journal["entries_folder"]), "entries") elsif Journal.config.key?("entries_folder") File.join(File.(Journal.config["entries_folder"]), @key, "entries") else File.join(File.("~/.local/share/journal"), @key, "entries") end FileUtils.mkdir_p(dir) unless File.directory?(dir) @date.localtime filename = @date.strftime("%Y-%m-%d_%H%M.md") target = File.join(dir, filename) File.open(target, "w") { |f| f.puts to_markdown(yaml: true, title: true) } puts "Saved new entry to #{target}" end |
#save_single_markdown ⇒ Object
Save entry to an existing Markdown file
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/journal-cli/checkin.rb', line 129 def save_single_markdown dir = if @journal.key?("entries_folder") File.join(File.(@journal["entries_folder"]), "entries") elsif Journal.config.key?("entries_folder") File.join(File.(Journal.config["entries_folder"]), @key) else File.("~/.local/share/journal/#{@key}/entries") end FileUtils.mkdir_p(dir) unless File.directory?(dir) filename = "#{@key}.md" @date.localtime target = File.join(dir, filename) File.open(target, "a") do |f| f.puts f.puts "## #{@title} #{@date.strftime("%x %X")}" f.puts f.puts to_markdown(yaml: false, title: false) end Journal.notify "{bg}Added new entry to {bw}#{target}" end |
#section(string) ⇒ Object
Add a section header (Markdown) to the output
50 51 52 |
# File 'lib/journal-cli/checkin.rb', line 50 def section(string) @output << "\n###### #{string}\n" unless string.nil? end |
#to_markdown(yaml: false, title: false, date: false, time: false) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/journal-cli/checkin.rb', line 252 def to_markdown(yaml: false, title: false, date: false, time: false) @output = [] if yaml @date.localtime yaml_data = {"title" => @title, "date" => @date.strftime("%x %X")} @data.each do |key, data| yaml_data = yaml_data.merge(weather_to_yaml(data.answers)) end @output << YAML.dump(yaml_data).strip @output << "---" end if title if date || time fmt = "" fmt += "%x" if date fmt += "%X" if time add_title "#{@title} #{@date.strftime(fmt)}" else add_title @title end end @sections.each do |key, section| section section.title section.questions.each do |question| if /\./.match?(question.key) res = section.answers.dup keys = question.key.split(".") keys.each_with_index do |key, i| next if i == keys.count - 1 res = res[key] end print_answer(question.prompt, question.type, keys.last, res) else print_answer(question.prompt, question.type, question.key, section.answers) end end end @output.join("\n") end |
#weather_to_yaml(answers) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/journal-cli/checkin.rb', line 223 def weather_to_yaml(answers) data = {} answers.each do |k, v| case v.class.to_s when /String/ next when /Hash/ data[k] = weather_to_yaml(v) when /Date/ v.localtime data[k] = v.strftime("%Y-%m-%d %H:%M") when /Weather/ data[k] = case k when /current$/ v.current when /forecast$/ data[k] = v.forecast when /moon(_?phase)?$/ data[k] = v.moon else data[k] = v.to_s end else data[k] = v end end data end |