Module: SifttterRedux::Sifttter
Overview
Sifttter Module Used to examine Sifttter data and create Day One entries as necessary.
Class Attribute Summary collapse
-
.entries ⇒ Hash
Stores the collection of entries to create.
Class Method Summary collapse
-
.run(date) ⇒ void
Finds Siftter data for the passed date and creates corresponding Day One entries.
Instance Method Summary collapse
-
#generate_template(datestamp, entrytext, starred, uuid) ⇒ Object
Generates an ERB template for a Day One entry.
-
#parse_sifttter_file(filepath, date) ⇒ void
Opens a filepath and parses it for Sifttter data for the passed date.
Class Attribute Details
.entries ⇒ Hash
Stores the collection of entries to create.
11 12 13 |
# File 'lib/sifttter-redux/sifttter.rb', line 11 def entries @entries end |
Class Method Details
.run(date) ⇒ void
This method returns an undefined value.
Finds Siftter data for the passed date and creates corresponding Day One entries.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sifttter-redux/sifttter.rb', line 66 def self.run(date) @entries = {} uuid = SecureRandom.uuid.upcase.gsub(/-/, '').strip date_for_title = date.strftime('%B %d, %Y') datestamp = date.to_time.utc.iso8601 starred = false output_dir = configuration.sifttter_redux[:dayone_local_filepath] Dir.mkdir(output_dir) unless Dir.exists?(output_dir) files = `find #{ configuration.sifttter_redux[:sifttter_local_filepath] } -type f -name "*.txt" | grep -v -i daily | sort` if files.empty? messenger.error('No Sifttter files to parse...') messenger.error('Is Dropbox Uploader configured correctly?') messenger.error("Is #{ configuration.sifttter_redux[:sifttter_remote_filepath] } the correct remote filepath?") exit!(1) end files.split("\n").each do |file| file.strip! if File.exists?(file) parse_sifttter_file(file, date) end end if @entries.length > 0 entrytext = "# Things done on #{ date_for_title }\n" @entries.each do |key, value| entrytext += '### ' + key.gsub(/.txt/, '').gsub(/_/, ' ').upcase + "\n\n" value.each { |v| entrytext += "#{ v[1] }\n" } entrytext += "\n" end template = generate_template(datestamp, entrytext, starred, uuid) fh = File.new(File.("#{ output_dir }/#{ uuid }.doentry"), 'w+') fh.puts template.result(binding) fh.close messenger.success("Entry logged for #{ date_for_title }...") else messenger.warn('No entries found...') end end |
Instance Method Details
#generate_template(datestamp, entrytext, starred, uuid) ⇒ Object
Generates an ERB template for a Day One entry
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sifttter-redux/sifttter.rb', line 19 def generate_template(datestamp, entrytext, starred, uuid) ERB.new " <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n <plist version=\"1.0\">\n <dict>\n <key>Creation Date</key>\n <date><%= datestamp %></date>\n <key>Entry Text</key>\n <string><%= entrytext %></string>\n <key>Starred</key>\n <<%= starred %>/>\n <key>Tags</key>\n <array>\n <string>daily logs</string>\n </array>\n <key>UUID</key>\n <string><%= uuid %></string>\n </dict>\n </plist>\n XMLTEMPLATE\nend\n" |
#parse_sifttter_file(filepath, date) ⇒ void
This method returns an undefined value.
Opens a filepath and parses it for Sifttter data for the passed date.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sifttter-redux/sifttter.rb', line 47 def parse_sifttter_file(filepath, date) title = File.basename(filepath).gsub(/^.*?\/([^\/]+)$/, "\\1") + "\n" date_regex = "(?:#{ date.strftime("%B") } 0?#{ date.strftime("%-d") }, #{ date.strftime("%Y") })" time_regex = "(?:\d{1,2}:\d{1,2}\s?[AaPpMm]{2})" entry_regex = /@begin\n@date\s#{ date_regex }(?: at (.*?)\n)?(.*?)@end/m contents = File.read(filepath) cur_entries = contents.scan(entry_regex) unless cur_entries.empty? @entries.merge!(title => []) unless @entries.key?(title) cur_entries.each { |e| @entries[title] << [e[0], e[1].strip] } end end |