Class: DotPlan::Command::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/dotplan/command/default.rb

Class Method Summary collapse

Class Method Details

.run(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dotplan/command/default.rb', line 7

def self.run(*args)
  begin
    credentials = JSON.parse(File.read(DotPlan::CREDENTIALS_PATH))
  rescue => e
    raise "There was a problem reading your credentials.  Run dotplan auth first.".red
  end
  username = credentials["username"]
  password = credentials["password"]
  date = Date.today.strftime("%m-%d-%Y")
  url = "#{DotPlan::DOTPLAN_URL}/user/#{username}/plan/#{date}"
  resource = RestClient::Resource.new(url, :Password => password)
  begin
    response = resource.get(:Password => password)
  rescue RestClient::ResourceNotFound => e
    begin
      response = resource.put(nil, :Password => password)
    rescue => e
      response = JSON.parse(e.response)
      raise response["error"].red
    end
  rescue => e
    response = JSON.parse(e.response)
    raise response["error"].red
  end

  plan = JSON.parse(response)

  File.open(DotPlan::TEMP_PATH, "w") do |f|
    f.write(plan["text"])
  end

  result = system("$EDITOR #{DotPlan::TEMP_PATH}")

  if result
    new_text = URI.escape(File.read DotPlan::TEMP_PATH)
    begin
      resource = RestClient::Resource.new("#{url}?text=#{new_text}", :Password => password)
      response = resource.put nil, :Password => password
    rescue => e
      puts e.message
    end
  end
end