Class: Tsks::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tsks/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_folderObject



12
13
14
# File 'lib/tsks/cli.rb', line 12

def self.setup_folder
  @setup_folder
end

.setup_folder=(folder_path) ⇒ Object



16
17
18
# File 'lib/tsks/cli.rb', line 16

def self.setup_folder= folder_path
  @setup_folder = folder_path
end

Instance Method Details

#add(tsk) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tsks/cli.rb', line 37

def add tsk
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  if options[:context]
    Tsks::Storage.insert tsk, options[:context]
  else
    Tsks::Storage.insert tsk
  end
end

#doing(id) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/tsks/cli.rb', line 226

def doing id
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  op_status = Tsks::Storage.update id, {status: 'doing'}
  if !op_status
    puts "the specified tsk do not exist."
  end
end

#done(id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/tsks/cli.rb', line 50

def done id
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  op_status = Tsks::Storage.update id
  if !op_status
    puts "the specified tsk do not exist."
  end
end

#initObject



26
27
28
29
30
31
32
33
# File 'lib/tsks/cli.rb', line 26

def init
  if File.directory? CLI.setup_folder
    return puts "tsks was already initialized."
  end

  Dir.mkdir CLI.setup_folder
  Tsks::Storage.init
end

#listObject



65
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
# File 'lib/tsks/cli.rb', line 65

def list
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  tsks = nil

  if options[:done] && options[:context]
    tsks = Tsks::Storage.select_by({status: 'done', context: options[:context]})
  elsif options[:done]
    tsks = Tsks::Storage.select_by({status: 'done'})
  elsif options[:context]
    tsks = Tsks::Storage.select_by({context: options[:context]})
  elsif options[:all]
    tsks = Tsks::Storage.select_all
  else
    tsks = Tsks::Storage.select_active
  end

  if tsks.count > 0
    for tsk in tsks
      tsk_status = Tsks::Actions.get_tsk_status tsk[:status]
      puts "#{tsk[:id]} | #{tsk_status} #{tsk[:tsk]} @#{tsk[:context]}"
    end
  else
    puts "no tsks found."
  end
end

#loginObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tsks/cli.rb', line 124

def 
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  begin
    res = Tsks::Request.post "/signin", {email: options[:email],
                                         password: options[:password]}

    if res && res[:ok] == true
      File.write File.join(CLI.setup_folder, "token"), res[:user][:auth_token]
      File.write File.join(CLI.setup_folder, "user_id"), res[:user][:id]
      Tsks::Actions.update_tsks_with_user_id res[:user][:id]
      puts "succesfully logged in."
    elsif res && res[:ok] == false
      puts "invalid e-mail or password."
    end
  rescue Errno::ECONNREFUSED, SocketError
    puts "failed to connect to API."
  rescue JSON::ParserError
    puts "error on reading data from API."
  end
end

#registerObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tsks/cli.rb', line 97

def register
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  begin
    res = Tsks::Request.post "/signup", {email: options[:email],
                                         password: options[:password]}

    if res && res[:ok] == true
      File.write File.join(CLI.setup_folder, "token"), res[:user][:auth_token]
      File.write File.join(CLI.setup_folder, "user_id"), res[:user][:id]
      Tsks::Actions.update_tsks_with_user_id res[:user][:id]
      puts "succesfully registered."
    elsif res && res[:ok] == false
      puts "this e-mail is already registered."
    end
  rescue Errno::ECONNREFUSED, SocketError
    puts "failed to connect to API."
  rescue JSON::ParserError
    puts "error on reading data from API."
  end
end

#remove(id) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/tsks/cli.rb', line 214

def remove id
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  op_status = Tsks::Storage.delete id
  if !op_status
    puts "the specified tsk do not exist."
  end
end

#syncObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/tsks/cli.rb', line 149

def sync
  if !File.directory? CLI.setup_folder
    return puts "tsks was not initialized yet."
  end

  if !File.exist? File.join CLI.setup_folder, "token"
    return puts "please, login before try to sync."
  end

  user_id = File.read File.join CLI.setup_folder, "user_id"
  token = File.read File.join CLI.setup_folder, "token"
  Tsks::Actions.update_tsks_with_user_id user_id
  Tsks::Actions.update_server_for_removed_tsks token
  Tsks::Storage.delete_removed_tsk_ids
  local_tsks = Tsks::Storage.select_all

  begin
    get_res = Tsks::Request.get "/tsks", token
    remote_tsks = get_res[:tsks]

    if get_res[:tsks]
      if get_res[:ok] == true
        raw_local_tsks = local_tsks.map {|t| t.reject{|k,_| k == :rowid}}
        local_tsks_to_post = raw_local_tsks - remote_tsks

        tsks_to_post = []
        for local_tsk in local_tsks
          for local_tsk_to_post in local_tsks_to_post
            if local_tsk[:tsk] == local_tsk_to_post[:tsk]
              tsks_to_post.append local_tsk
            end
          end
        end

        if tsks_to_post.count > 0
          for tsk in tsks_to_post
            post_res = Tsks::Request.post "/tsks", token, {tsk: tsk}
            posted_tsk = post_res[:tsk]

            if posted_tsk
              Tsks::Storage.update_by({rowid: tsk[:rowid]}, {id: posted_tsk[:id]})
            end
          end
        end

        # TODO: review this process
        updated_local_tsks = Tsks::Storage.select_all
        raw_updated_local_tsks = updated_local_tsks.map {|t| t.reject{|k,_| k == :rowid}}
        remote_tsks_to_storage = remote_tsks - raw_updated_local_tsks

        if remote_tsks_to_storage.count > 0
          Tsks::Storage.insert_many remote_tsks_to_storage
        end

        puts "your tsks were succesfully synchronized."
      end
    end
  rescue Errno::ECONNREFUSED, SocketError
    puts "failed to connect to API."
  rescue JSON::ParserError
    puts "error on reading data from API."
  end
end

#versionObject



21
22
23
# File 'lib/tsks/cli.rb', line 21

def version
  puts "tsks #{Tsks::VERSION}"
end