patodo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/patodo +203 -0
  3. data/lib/response_decorator.rb +52 -0
  4. metadata +89 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9025f0c204fbf30133a69955c9b7bd56ed1b8970
4
+ data.tar.gz: bc41b73c3d8478c326a322ae51b02a86632d2add
5
+ SHA512:
6
+ metadata.gz: 4deafa6bb0dfacca59af74b6f16eb4ef27951376b9afc4c9375c373dfbb182c53164cdf0a1bde77c17a3c951afb1d6609ec712441d7834044d14b34c7ab62d49
7
+ data.tar.gz: 3d1252787007a3a2f096b581387ba78fe7fb884928005de84fc2c46d844f7bec74eb739b62e3a6a81b9fc3cf95b464b67fa5cba0109d68bc41002c0a3e08b46a
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'commander/import'
4
+ require 'httparty'
5
+ require 'json'
6
+ require_relative '../lib/response_decorator'
7
+
8
+ URL = 'https://pato-manager.herokuapp.com/'
9
+
10
+ def current_token
11
+ begin
12
+ @token ||= open(File.expand_path("~/.pato-manager")).read.chomp
13
+ rescue Errno::ENOENT => e
14
+ puts <<-MESSAGE
15
+
16
+ Error: the file #{ e.message.split("- ").last } doesn't exists.
17
+ Also the file needs to contain the GitHub token.
18
+ Try to run the script to create the file and get the token or for help.
19
+
20
+ MESSAGE
21
+ end
22
+ end
23
+
24
+ def json_response response
25
+ JSON.parse response, symbolize_names: true
26
+ end
27
+
28
+ class Patodo
29
+ include HTTParty
30
+ base_uri URL
31
+ headers 'Content-Type' => 'application/json'
32
+ headers 'User-Token' => current_token
33
+ end
34
+
35
+ program :name, 'Pato Manager'
36
+ program :version, '0.1.0'
37
+ program :description, 'A terminal application to keep tracking of your todo lists.'
38
+ program :help, 'Author', 'Costantini Leandro <costantinileandro1@gmail.com'
39
+
40
+ default_command :todo
41
+
42
+ command :todo do |c|
43
+ c.description = 'List all the todo tasks.'
44
+ c.option '--d', 'Show description column in table.'
45
+ c.option '--da', 'Show date column in table.'
46
+ c.action do |_, options|
47
+ show = { description: options.d, date: options.da }.reject { |_, v| v.nil? }
48
+ response = Patodo.get "/tasks"
49
+ puts ResponseDecorator.new.decorate_table json_response(response), show
50
+ end
51
+ end
52
+
53
+ command :new do |c|
54
+ c.syntax = 'new options \'string\''
55
+ c.description = 'Create a new todo task.'
56
+ c.option '--d STRING', String, 'Adds a description for a todo task.'
57
+ c.option '--c STRING', String, 'Add a category for a todo task.'
58
+ c.action do |args, options|
59
+ name = args.first || ask('Name: ')
60
+ category = options.c || ask('Category: ')
61
+ params = { task: { name: name, description: options.d, category: category } }
62
+ response = Patodo.post "/tasks", body: params.to_json
63
+ say '# SAVED' if response.code == 201
64
+ end
65
+ end
66
+
67
+ command :all do |c|
68
+ c.description = 'List all the tasks.'
69
+ c.action do
70
+ response = Patodo.get "/tasks/all"
71
+ puts ResponseDecorator.new.decorate_table json_response(response)
72
+ end
73
+ end
74
+
75
+ command :update do |c|
76
+ c.syntax = 'update id options \'string\''
77
+ c.description = 'Update a task.'
78
+ c.option '--n STRING', String, 'Adds a new name for a task'
79
+ c.option '--d STRING', String, 'Adds a new description for a task'
80
+ c.option '--c STRING', String, 'Add a category for a todo task.'
81
+ c.action do |args, options|
82
+ id = args.first || ask('ID: ')
83
+ params = { task: { name: options.n, description: options.d, category: options.c } }
84
+ response = Patodo.put "/tasks/#{ id }", body: params.to_json
85
+ say '# UPDATED' if response.code == 204
86
+ end
87
+ end
88
+
89
+ command :done do |c|
90
+ c.syntax = 'done id'
91
+ c.description = 'Mark a todo task as done.'
92
+ c.action do |args|
93
+ id = args.first || ask('ID: ')
94
+ response = Patodo.put "/tasks/#{ id }/done"
95
+ say '# MARKED AS DONE' if response.code == 204
96
+ end
97
+ end
98
+
99
+ command :undone do |c|
100
+ c.syntax = 'undone id'
101
+ c.description = 'Mark a task as undone.'
102
+ c.action do |args|
103
+ id = args.first || ask('ID: ')
104
+ response = Patodo.put "/tasks/#{ id }/undone"
105
+ say '# MARKED AS UNDONE' if response.code == 204
106
+ end
107
+ end
108
+
109
+ # TODO: mark a task as CANCEL
110
+ #command :cancel do |c|
111
+ # c.syntax = 'cancel id'
112
+ # c.description = 'Mark a task as canceled.'
113
+ # c.action do |args|
114
+ # id = args.join(' ')
115
+ # response = Patodo.put "/tasks/#{ id }/cancel"
116
+ # say "# OK" if response.code == 200
117
+ # end
118
+ #end
119
+
120
+ command :delete do |c|
121
+ c.syntax = 'delete id'
122
+ c.description = 'Delete a task.'
123
+ c.action do |args|
124
+ id = args.first || ask('ID: ')
125
+ response = Patodo.delete "/tasks/#{ id }"
126
+ say '# DELETED' if response.code == 204
127
+ end
128
+ end
129
+
130
+ # TODO: filtrar tambien por done (--d) y undone (--t).
131
+ command :category do |c|
132
+ c.syntax = 'category \'string\''
133
+ c.description = 'Find all tasks for a specific category.'
134
+ c.action do |args|
135
+ category = args.first || ask('Category: ')
136
+ response = Patodo.get "/tasks/category?topic=#{ category }"
137
+ puts ResponseDecorator.new.decorate_table json_response(response)
138
+ end
139
+ end
140
+
141
+ command :token do |c|
142
+ c.syntax = 'token \'string\''
143
+ c.description = <<-README
144
+ HOW IT WORKS:
145
+ To know the user that is using the app or create a new one,
146
+ Pato Manager needs to know your GitHub email[*], for that,
147
+ the script ask your GitHub username and try to create or read
148
+ a public token .- **only gets your public profile details** -.
149
+
150
+ The GitHub API ask for your password, then the script capture
151
+ the response from the API and save in pato-manager file.
152
+ You can see your token in $HOME/.pato-manager
153
+ and in GitHub https://github.com/settings/tokens.
154
+
155
+ [*]: Because it's easier than having to remember another login account.
156
+ README
157
+ c.action do |args|
158
+ username = args.first || ask('GitHub username: ')
159
+ response = `curl "https://api.github.com/authorizations" -u #{ username } --data '{"scopes": ["user:email"], "note": "pato-manager"}'`
160
+ puts
161
+ puts json_response(response)
162
+ if json_response(response)[:message]
163
+ puts 'You can run the \'token_help\' command to get more info.'
164
+ else
165
+ open(File.expand_path("~/.pato-manager"), 'a+') do |f|
166
+ f.write json_response(response)[:token]
167
+ end
168
+ puts 'Done! The token was saved.'
169
+ end
170
+ end
171
+ end
172
+
173
+ command :manually do |c|
174
+ c.syntax = 'manually'
175
+ c.description = 'This command help to create a token manually.'
176
+ c.action do |_|
177
+ puts <<-README
178
+ MANUALLY:
179
+ To get the token manually, run the commands at the bottom.
180
+ In the curl command replacing the USERNAME for your GitHub username,
181
+ then execute the second command that creates a hidden file called pato-manager.
182
+ Finally copy the token provided by the Github API and save it in the file.
183
+
184
+ curl "https://api.github.com/authorizations" -u USERNAME --data '{"scopes": ["user:email"], "note": "pato-manager"}'
185
+ touch ~/.pato-manager
186
+ README
187
+ end
188
+ end
189
+
190
+ command :token_help do |c|
191
+ c.syntax = 'token_help'
192
+ c.description = 'This command help to solve some error messages given from the GitHub API.'
193
+ c.action do |_|
194
+ puts <<-HELP
195
+ HELP:
196
+ * Validation Failed: if you get this error is because you already have a token.
197
+ You don't need to create a token, check if the file contains the token and you
198
+ are ready to use.
199
+ If the file doesn't contain the token or the file is missing you can delete the
200
+ token in your GitHub settings panel and run the command 'token' or 'manually'.
201
+ HELP
202
+ end
203
+ end
@@ -0,0 +1,52 @@
1
+ require 'terminal-table'
2
+
3
+ class ResponseDecorator
4
+ def decorate_table response, options = {}
5
+ @response = response
6
+ @options = options
7
+ return '# The list is empty' if response.empty?
8
+ return '# ' + response[:errors] if response.include? :errors
9
+ Terminal::Table.new headings: get_header, rows: get_rows
10
+ end
11
+
12
+ private
13
+
14
+ def default_rows
15
+ @response.map do |r|
16
+ ["#{ r[:id] }",
17
+ "#{ r[:name] }",
18
+ "#{ icon_state(r[:state]) }",
19
+ "#{ r[:category] }"]
20
+ end
21
+ end
22
+
23
+ def get_header
24
+ head = ['Id', 'Name', 'State', 'Category']
25
+ head << 'Date' if @options[:date]
26
+ head << 'Description' if @options[:description]
27
+ head
28
+ end
29
+
30
+ def get_rows
31
+ rows = default_rows
32
+ return rows if @options.empty?
33
+ new_rows = options_rows
34
+ (0...rows.count).map { |i| (rows[i] << new_rows[i]).flatten }
35
+ end
36
+
37
+ def options_rows
38
+ date = option_response :created_at if @options[:date]
39
+ description = option_response :description if @options[:description]
40
+ return ([date] + [description]).compact.first if @options.count == 1
41
+ [date, description].transpose
42
+ end
43
+
44
+ def option_response option
45
+ @response.map { |r| "#{ r[option] }" }
46
+ end
47
+
48
+ def icon_state state
49
+ return '❏' if state == 'todo'
50
+ '✔'
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: patodo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leandro Costantini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: commander
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: terminal-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ description: Patodo is a console application to keep track your todos.
56
+ email: costantinileandro1@gmail.com
57
+ executables:
58
+ - patodo
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/patodo
63
+ - lib/response_decorator.rb
64
+ homepage: https://github.com/lcostantini/pato-manager
65
+ licenses:
66
+ - GPL
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Console application to keep track your todos.
88
+ test_files: []
89
+ has_rdoc: