class Hbtrack::Importer::HbtrackImporter

Constants

ENTRY_TYPE
Entry
Habit

Public Class Methods

new() click to toggle source
# File lib/hbtrack/importer/hbtrack_importer.rb, line 19
def initialize
  super
  @index = 1
end

Public Instance Methods

create_entries_of(id, entries) click to toggle source
# File lib/hbtrack/importer/hbtrack_importer.rb, line 52
def create_entries_of(id, entries)
  @entries[id] = entries.flat_map do |entry|
    month, values = entry.split(': ')

    values.split("").map.with_index(1) do |value, index|
      create_entry(month, index, value)
    end
  end
end
create_entry(month, day, value) click to toggle source
# File lib/hbtrack/importer/hbtrack_importer.rb, line 62
def create_entry(month, day, value)
  timestamp = create_timestamp_for(month, day)
  type = ENTRY_TYPE[value]
  Entry.new(timestamp, type)
end
create_habit(habit) click to toggle source

Create a Habit

# File lib/hbtrack/importer/hbtrack_importer.rb, line 46
def create_habit(habit)
  habit = Habit.new(habit, @index)
  @index += 1
  habit
end
create_timestamp_for(month, day) click to toggle source
# File lib/hbtrack/importer/hbtrack_importer.rb, line 68
def create_timestamp_for(month, day)
  year, month = month.split(',').map(&:to_i)
  time_zone = Time.new.zone
  DateTime.new(year, month, day, 0, 0, 0, "#{time_zone}:00").to_s
end
extract_from(id, collection) click to toggle source
# File lib/hbtrack/importer/hbtrack_importer.rb, line 35
def extract_from(id, collection)
  arr = collection.split("\n")

  # Get habit name
  habit_name = arr.shift
  @habits[id] = create_habit(habit_name)

  create_entries_of(id, arr)
end
import_from(file) click to toggle source

Import and parse the CSV from Streaks

# File lib/hbtrack/importer/hbtrack_importer.rb, line 25
def import_from(file)
  raise 'File not found' unless File.exist?(file)
  input = File.read(file).split(/\n\n/)
  input.each_with_index do |collection, index|
    extract_from(index, collection)
  end

  [@habits, @entries]
end