Module: ExistClient

Extended by:
LogicalDate
Defined in:
lib/exist_client.rb,
lib/exist_client/tasks.rb,
lib/exist_client/config.rb,
lib/exist_client/version.rb,
lib/exist_client/reporter.rb,
lib/exist_client/tasks/task.rb,
lib/exist_client/logical_date.rb,
lib/exist_client/report_period.rb,
lib/exist_client/time_tracking.rb,
lib/exist_client/time_tracking/entry.rb

Defined Under Namespace

Modules: LogicalDate Classes: Config, ReportPeriod, Reporter, Tasks, TimeTracking

Constant Summary collapse

REPORTERS =
[TimeTracking, Tasks]
VERSION =
"0.1.0"

Class Method Summary collapse

Methods included from LogicalDate

logical_date

Class Method Details

.log(message, indent: 0) ⇒ Object



66
67
68
# File 'lib/exist_client.rb', line 66

def log(message, indent: 0)
  logger.info("#{"\t" * indent}#{message}")
end

.loggerObject



70
71
72
73
74
75
76
# File 'lib/exist_client.rb', line 70

def logger
  @logger ||= Logger.new(STDOUT).tap do |log|
    log.formatter = lambda do |severity, datetime, _, msg|
      format("[%s] %5s: %s\n", datetime.strftime("%F %T"), severity, msg)
    end
  end
end

.post(values) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/exist_client.rb', line 46

def post(values)
  log values.to_json, indent: 1

  headers = {
    "Authorization" => "Bearer #{ENV.fetch("EXIST_API_KEY")}",
    "Content-Type" => "application/json"
  }

  response = HTTParty.post("https://exist.io/api/1/attributes/update/", body: values.to_json, headers: headers)

  if response.success?
    log "Success!", indent: 1
  else
    log "Error!", indent: 1
    log response.code, indent: 1
    log response.body, indent: 1
    exit 1
  end
end

.reportObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/exist_client.rb', line 24

def report
  log "Starting report"

  # TODO: Pass in optional data path
  # Config.set_data_path()
  last_report_date = Date.parse(Config.last_report_date_file.read.strip)
  report_period = ReportPeriod.new(last_report_date)

  unless report_period.valid?
    log "Report period is not valid"
    exit 1
  end

  Config.enabled_reporters.each do |reporter|
    reporter.new(report_period).report
  end

  Config.last_report_date_file.write(logical_date(Time.now) - 1)

  log "Done"
end

.setupObject



18
19
20
21
22
# File 'lib/exist_client.rb', line 18

def setup
  puts "Installing ExistClient..."
  Config.setup # TODO: Pass in optional data path
  REPORTERS.each(&:setup)
end