cv-tool 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/app/arguments.rb +103 -0
- data/app/configuration.rb +20 -0
- data/app/inputs.rb +59 -0
- data/app/main.rb +95 -0
- data/app/signals.rb +5 -0
- data/bin/cvtool +5 -0
- data/lib/cv_tool/constants.rb +55 -0
- data/lib/cv_tool/event.rb +28 -0
- data/lib/cv_tool/files.rb +47 -0
- data/lib/cv_tool/http.rb +20 -0
- data/lib/cv_tool/os.rb +15 -0
- data/lib/cv_tool/rest_api.rb +163 -0
- data/lib/cv_tool/version.rb +3 -0
- data/lib/cv_tool.rb +39 -0
- data/lib/json_parser.rb +96 -0
- data/lib/option_parser.rb +91 -0
- data/lib/string.rb +9 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6b4ea67b5e07754ecf8e767310d84e604378ff05d32c3e2ef8084a369a3127ae
|
4
|
+
data.tar.gz: 72f397c82a4180513710d6c1059e537f2f7d461feea56ab7cc373e1dd6d38433
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b814fb52649dc7fae5ea6acd9a9776822e654189fe930808a158c5b283c79de4c0e65a2646ec072c7d3f61717724fab5705ec50c5083d44d47e7fec04646dce
|
7
|
+
data.tar.gz: 2a2b26d733e9349b02f83de36838a89ea6c2a74e8e5cd3f148b406177aa1f7c74e1ca682f66da0b71696d011d9dad362b3eaaac3ce8fe5d69702e6c87c6beeb8
|
data/app/arguments.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'option_parser'
|
2
|
+
|
3
|
+
@options = {
|
4
|
+
rest_api: {
|
5
|
+
is_active: false,
|
6
|
+
endpoint: nil,
|
7
|
+
request_body: nil,
|
8
|
+
generate_db: nil,
|
9
|
+
},
|
10
|
+
gt_length: -1,
|
11
|
+
api_url: nil,
|
12
|
+
is_ssl: nil,
|
13
|
+
}
|
14
|
+
|
15
|
+
OptionParser.parse do |parser|
|
16
|
+
parser.banner(
|
17
|
+
"Using the REST API, this tool is used to modify the website CV.\n" +
|
18
|
+
"Usage: #{CVTool::Constants::APP_NAME} [options]\n" +
|
19
|
+
"\nOptions:"
|
20
|
+
)
|
21
|
+
parser.on( "-h", "--help", "Show help" ) do
|
22
|
+
puts parser
|
23
|
+
exit
|
24
|
+
end
|
25
|
+
parser.on( "-v", "--version", "Show version" ) do
|
26
|
+
CVTool::Event.print('VERSION |', CVTool::VERSION)
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
parser.on( "-dp", "--deactive-print", "Disables the printing process." ) do
|
30
|
+
bool = false
|
31
|
+
CVTool::Event.print('PRINT', bool)
|
32
|
+
CVTool::Event::set_print_active(bool)
|
33
|
+
end
|
34
|
+
parser.on( "-ra", "--rest-api", "Rest API for get and post operations\n" +
|
35
|
+
"(additional setting options)." ) do
|
36
|
+
@options[:rest_api][:is_active] = true
|
37
|
+
|
38
|
+
OptionParser.parse do |parser|
|
39
|
+
parser.banner(
|
40
|
+
"Rest API for get and post operations.\n" +
|
41
|
+
"Usage: #{CVTool::Constants::APP_NAME} -ra [options]\n" +
|
42
|
+
"\nOptions:"
|
43
|
+
)
|
44
|
+
parser.on( "-h", "--help", "Show help" ) do
|
45
|
+
puts parser
|
46
|
+
exit
|
47
|
+
end
|
48
|
+
parser.on( "-ep ROUTE", "--endpoint ROUTE", "Defining an endpoint to use in order\n" +
|
49
|
+
"to access a specific function." ) do |route|
|
50
|
+
@options[:rest_api][:endpoint] = route
|
51
|
+
CVTool::Event.print('ENDPOINT', route)
|
52
|
+
end
|
53
|
+
parser.on( "-epl", "--endpoints-list", "A list of endpoints is printed." ) do
|
54
|
+
CVTool::Event.print('ENDPOINTS', "| All endpoints are listed in a printout.\n\n")
|
55
|
+
puts CVTool::Constants::ENDPOINTS
|
56
|
+
exit
|
57
|
+
end
|
58
|
+
parser.on( "-reb PATH", "--request-body PATH", "The information that must be sent\n" +
|
59
|
+
"as *request.body* via a json file.\n" +
|
60
|
+
"(The location to the *.json* file must be\n" +
|
61
|
+
"entered if this option is chosen;\n" +
|
62
|
+
"else, the data must be manually entered\n" +
|
63
|
+
"via the terminal input.)" ) do |path|
|
64
|
+
@options[:rest_api][:request_body] = path
|
65
|
+
CVTool::Event.print('REQUEST-BODY', path)
|
66
|
+
end
|
67
|
+
parser.on( "-gdb PATH", "--generate-db PATH", "It creates *json* files for Projects and\n" +
|
68
|
+
"Articles in the defined path (it obtains\n" +
|
69
|
+
"the relevant data from the Rest API, which\n" +
|
70
|
+
"is then sorted and saved).\n" ) do |path|
|
71
|
+
|
72
|
+
unless path
|
73
|
+
path = Dir.pwd
|
74
|
+
end
|
75
|
+
|
76
|
+
@options[:rest_api][:generate_db] = path
|
77
|
+
CVTool::Event.print('GENERATE-DB', path)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
parser.on( "-gt LENG", "--generate-token LENG",
|
82
|
+
"To secure API server access, this function\n" +
|
83
|
+
"generates a token with a specified length\n" +
|
84
|
+
"(manual entry of the token into the ENV\n" +
|
85
|
+
"is required).\n" +
|
86
|
+
"The default: #{CVTool::Constants::GT_LENGTH} length" ) do |length|
|
87
|
+
|
88
|
+
length = length.to_i
|
89
|
+
if length <= 0
|
90
|
+
length = CVTool::Constants::GT_LENGTH
|
91
|
+
end
|
92
|
+
@options[:gt_length] = length
|
93
|
+
end
|
94
|
+
parser.on( "-sa URL", "--set-api URL", "Sets the API server's primary URL.\n" +
|
95
|
+
"The default: #{@configuration.parse(:api_url)}" ) do |url|
|
96
|
+
@options[:api_url] = url
|
97
|
+
end
|
98
|
+
parser.on( "-ss BOOL", "--set-ssl BOOL", "Sets the SSL encryption protocol.\n" +
|
99
|
+
"The default: #{@configuration.parse(:is_ssl)}" ) do |is_ssl|
|
100
|
+
|
101
|
+
@options[:is_ssl] = is_ssl.to_s.to_b
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
@configuration = JsonParser.new File.join(ROOT, 'config/default.json')
|
4
|
+
@configuration.on :api_url, CVTool::Constants::API_URI
|
5
|
+
@configuration.on :is_ssl, true
|
6
|
+
|
7
|
+
h_api_url = lambda do |_|
|
8
|
+
return @configuration.parse(:api_url)
|
9
|
+
end
|
10
|
+
|
11
|
+
h_is_ssl = lambda do |_|
|
12
|
+
return @configuration.parse(:is_ssl).to_b
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_config_str()
|
16
|
+
JSON.pretty_generate(@configuration.db)
|
17
|
+
end
|
18
|
+
|
19
|
+
CVTool::Event.add(:api_url, h_api_url)
|
20
|
+
CVTool::Event.add(:is_ssl, h_is_ssl)
|
data/app/inputs.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
h_add = lambda do |args|
|
2
|
+
result = {}
|
3
|
+
symbol = args[0]
|
4
|
+
|
5
|
+
begin
|
6
|
+
CVTool::Constants::SHEMAS[symbol].each do |name|
|
7
|
+
l_input = lambda do |info|
|
8
|
+
print "#{info}: "
|
9
|
+
input = STDIN.gets.chomp
|
10
|
+
return input
|
11
|
+
end
|
12
|
+
|
13
|
+
unless name == CVTool::Constants::SHEMAS[:project][3]
|
14
|
+
input = l_input.call(name)
|
15
|
+
if name.index('is') or name.index('id')
|
16
|
+
input = input.to_i()
|
17
|
+
end
|
18
|
+
result[name.to_sym] = input
|
19
|
+
else
|
20
|
+
input = l_input.call("#{name} (a file's path)")
|
21
|
+
content = CVTool::Files.get_content( input )
|
22
|
+
result[name.to_sym] = content
|
23
|
+
end
|
24
|
+
end
|
25
|
+
rescue Interrupt => e
|
26
|
+
end
|
27
|
+
|
28
|
+
return result
|
29
|
+
end
|
30
|
+
|
31
|
+
h_free = lambda do |_|
|
32
|
+
result = {}
|
33
|
+
|
34
|
+
begin
|
35
|
+
print "id: "
|
36
|
+
result[:id] = STDIN.gets.chomp.to_i
|
37
|
+
rescue Interrupt => e
|
38
|
+
end
|
39
|
+
|
40
|
+
return result
|
41
|
+
end
|
42
|
+
|
43
|
+
h_update = lambda do |_|
|
44
|
+
result = {}
|
45
|
+
|
46
|
+
begin
|
47
|
+
print "id: "
|
48
|
+
result[:id] = STDIN.gets.chomp.to_i
|
49
|
+
print "query: "
|
50
|
+
result[:query] = STDIN.gets.chomp
|
51
|
+
rescue Interrupt => e
|
52
|
+
end
|
53
|
+
|
54
|
+
return result
|
55
|
+
end
|
56
|
+
|
57
|
+
CVTool::Event.add(:input_add, h_add)
|
58
|
+
CVTool::Event.add(:input_free, h_free)
|
59
|
+
CVTool::Event.add(:input_update, h_update)
|
data/app/main.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'cv_tool'
|
2
|
+
require_relative './configuration'
|
3
|
+
require_relative './arguments'
|
4
|
+
require_relative './inputs'
|
5
|
+
require_relative './signals'
|
6
|
+
|
7
|
+
def token_state()
|
8
|
+
unless @options[:gt_length] == -1
|
9
|
+
token = CVTool.generate_token( @options[:gt_length] )
|
10
|
+
CVTool::Event.print('TOKEN |', token)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def api_url_state()
|
15
|
+
sym = :api_url
|
16
|
+
api_url = @options[sym]
|
17
|
+
if api_url
|
18
|
+
@configuration.parse(sym, api_url)
|
19
|
+
CVTool::Event.print('SET', "#{sym.to_s}: #{api_url.to_s}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def is_ssl_state()
|
24
|
+
sym = :is_ssl
|
25
|
+
is_ssl = @options[sym]
|
26
|
+
unless is_ssl == nil
|
27
|
+
@configuration.parse(sym, is_ssl.to_s)
|
28
|
+
CVTool::Event.print('SET', "#{sym.to_s}: #{is_ssl.to_s}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def ra_define_method_state(endpoint, &block)
|
33
|
+
h_get = lambda do
|
34
|
+
CVTool::RestApi.get(@options[:rest_api]) do |response|
|
35
|
+
if block
|
36
|
+
block.call(response)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
h_post = lambda do
|
41
|
+
CVTool::RestApi.post(@options[:rest_api]) do |response|
|
42
|
+
if block
|
43
|
+
block.call(response)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
CVTool::RestApi.define_method( endpoint, h_get, h_post )
|
49
|
+
end
|
50
|
+
|
51
|
+
def rest_api_state(&block)
|
52
|
+
endpoint = @options[:rest_api][:endpoint]
|
53
|
+
unless endpoint
|
54
|
+
CVTool::Event.print('WARNING', "| You must fill out the Endpoint for " +
|
55
|
+
"the Rest API in order to manage the server.")
|
56
|
+
exit
|
57
|
+
end
|
58
|
+
|
59
|
+
ra_define_method_state(endpoint) do |response|
|
60
|
+
if block
|
61
|
+
block.call(response)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def generate_db_state()
|
67
|
+
get_endpoints = [
|
68
|
+
CVTool::Constants::ENDPOINTS[0],
|
69
|
+
CVTool::Constants::ENDPOINTS[1]
|
70
|
+
]
|
71
|
+
|
72
|
+
get_endpoints.each do |endpoint|
|
73
|
+
@options[:rest_api][:endpoint] = endpoint
|
74
|
+
rest_api_state() do |response|
|
75
|
+
args = {
|
76
|
+
endpoint: endpoint,
|
77
|
+
response: response,
|
78
|
+
path: @options[:rest_api][:generate_db]
|
79
|
+
}
|
80
|
+
CVTool.generate_db(args)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
if @options[:rest_api][:is_active]
|
86
|
+
unless @options[:rest_api][:generate_db]
|
87
|
+
rest_api_state()
|
88
|
+
else
|
89
|
+
generate_db_state()
|
90
|
+
end
|
91
|
+
else
|
92
|
+
token_state()
|
93
|
+
api_url_state()
|
94
|
+
is_ssl_state()
|
95
|
+
end
|
data/app/signals.rb
ADDED
data/bin/cvtool
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module CVTool
|
2
|
+
module Constants
|
3
|
+
APP_NAME = 'cvtool'
|
4
|
+
API_URI = 'http://localhost:8080/api/v1'
|
5
|
+
ENDPOINTS = [
|
6
|
+
'get/articles',
|
7
|
+
'get/projects',
|
8
|
+
'get/profiles',
|
9
|
+
|
10
|
+
'post/article/add',
|
11
|
+
'post/article/free',
|
12
|
+
'post/article/update',
|
13
|
+
|
14
|
+
'post/project/add',
|
15
|
+
'post/project/free',
|
16
|
+
'post/project/update',
|
17
|
+
|
18
|
+
'post/profile/add',
|
19
|
+
'post/profile/free',
|
20
|
+
'post/profile/update',
|
21
|
+
|
22
|
+
'post/tables/reset',
|
23
|
+
]
|
24
|
+
METHODS = [
|
25
|
+
'get',
|
26
|
+
'post'
|
27
|
+
]
|
28
|
+
SHEMAS = {
|
29
|
+
article: [
|
30
|
+
'author_id',
|
31
|
+
'project_id',
|
32
|
+
'name',
|
33
|
+
'description',
|
34
|
+
'url'
|
35
|
+
],
|
36
|
+
project: [
|
37
|
+
'author_id',
|
38
|
+
'name',
|
39
|
+
'category',
|
40
|
+
'content'
|
41
|
+
],
|
42
|
+
profile: [
|
43
|
+
'is_author',
|
44
|
+
'full_name',
|
45
|
+
'avatar',
|
46
|
+
'email',
|
47
|
+
'phone',
|
48
|
+
'bio',
|
49
|
+
'password'
|
50
|
+
]
|
51
|
+
}
|
52
|
+
GT_LENGTH = 40
|
53
|
+
ENV_NAME = 'CV_TOKEN'
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module CVTool
|
2
|
+
module Event
|
3
|
+
HANDLERS = {}
|
4
|
+
@is_print_active = true
|
5
|
+
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def set_print_active(bool)
|
9
|
+
@is_print_active = bool
|
10
|
+
end
|
11
|
+
|
12
|
+
def print(event, message = "")
|
13
|
+
unless @is_print_active
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
puts "#{Time.now.strftime("%l:%M:%S %p").lstrip} [#{Constants::APP_NAME}] #{event} #{message}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def add(symbol, handler)
|
21
|
+
HANDLERS[symbol] = handler
|
22
|
+
end
|
23
|
+
|
24
|
+
def emit(symbol, *args)
|
25
|
+
HANDLERS[symbol].call(args)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'json_parser'
|
3
|
+
|
4
|
+
module CVTool
|
5
|
+
module Files
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def open(path)
|
9
|
+
result = ""
|
10
|
+
|
11
|
+
if File.exist? path
|
12
|
+
File.open path do |f|
|
13
|
+
result = f.read
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
result
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_content(path_or_content)
|
21
|
+
content = path_or_content
|
22
|
+
path = content.gsub('\'', '').strip
|
23
|
+
if File.exist? path
|
24
|
+
content = Files.open(path)
|
25
|
+
end
|
26
|
+
|
27
|
+
return URI.encode_www_form_component(content)
|
28
|
+
end
|
29
|
+
|
30
|
+
def json?(str)
|
31
|
+
result = JSON.parse(str)
|
32
|
+
result.is_a?(Hash) || result.is_a?(Array)
|
33
|
+
rescue JSON::ParserError, TypeError
|
34
|
+
return false
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_json_db(path)
|
38
|
+
if File.exist? path
|
39
|
+
path = path.gsub('\'', '').strip
|
40
|
+
json_parser = JsonParser.new(path)
|
41
|
+
return json_parser.db
|
42
|
+
else
|
43
|
+
return nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/cv_tool/http.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module CVTool
|
4
|
+
module Http
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def get_status_code(response)
|
8
|
+
code = response.scan(/\d+/)[0]
|
9
|
+
status = response.sub(code, '').strip
|
10
|
+
|
11
|
+
status_code = {
|
12
|
+
'status_code': {
|
13
|
+
'code': code.to_i,
|
14
|
+
'status': status
|
15
|
+
}
|
16
|
+
}
|
17
|
+
return status_code
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/cv_tool/os.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module CVTool
|
2
|
+
module OS
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def get_token()
|
6
|
+
env = ENV[Constants::ENV_NAME]
|
7
|
+
if env == nil
|
8
|
+
Event.print('TOKEN', "| It needs to be inserted as a value because it does " +
|
9
|
+
"not receive a token from the #{Constants::ENV_NAME} environment.")
|
10
|
+
exit
|
11
|
+
end
|
12
|
+
return env
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module CVTool
|
6
|
+
module RestApi
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def get_uri(endpoint)
|
10
|
+
return "#{Event.emit(:api_url)}/#{ endpoint }"
|
11
|
+
end
|
12
|
+
|
13
|
+
def define_method(endpoint, h_get, h_post)
|
14
|
+
method = endpoint.sub(/\/.*$/, '')
|
15
|
+
|
16
|
+
case method
|
17
|
+
when Constants::METHODS[0]
|
18
|
+
h_get.call()
|
19
|
+
when Constants::METHODS[1]
|
20
|
+
h_post.call()
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def defined_data(endpoint, request_body)
|
25
|
+
result = {}
|
26
|
+
|
27
|
+
if request_body == nil
|
28
|
+
h_event_print = lambda do
|
29
|
+
Event.print('INPUT', '| The data for the following ' +
|
30
|
+
'process is defined by the input process.')
|
31
|
+
end
|
32
|
+
h_add = lambda do |symbol|
|
33
|
+
h_event_print.call()
|
34
|
+
result = Event::emit(:input_add, symbol)
|
35
|
+
end
|
36
|
+
h_free = lambda do
|
37
|
+
h_event_print.call()
|
38
|
+
result = Event::emit(:input_free)
|
39
|
+
end
|
40
|
+
h_update = lambda do
|
41
|
+
h_event_print.call()
|
42
|
+
result = Event::emit(:input_update)
|
43
|
+
end
|
44
|
+
|
45
|
+
case endpoint
|
46
|
+
# Articles
|
47
|
+
when Constants::ENDPOINTS[3]
|
48
|
+
h_add.call(:article)
|
49
|
+
when Constants::ENDPOINTS[4]
|
50
|
+
h_free.call()
|
51
|
+
when Constants::ENDPOINTS[5]
|
52
|
+
h_update.call()
|
53
|
+
# Projects
|
54
|
+
when Constants::ENDPOINTS[6]
|
55
|
+
h_add.call(:project)
|
56
|
+
when Constants::ENDPOINTS[7]
|
57
|
+
h_free.call()
|
58
|
+
when Constants::ENDPOINTS[8]
|
59
|
+
h_update.call()
|
60
|
+
# Profiles
|
61
|
+
when Constants::ENDPOINTS[9]
|
62
|
+
h_add.call(:profile)
|
63
|
+
when Constants::ENDPOINTS[10]
|
64
|
+
h_free.call()
|
65
|
+
when Constants::ENDPOINTS[11]
|
66
|
+
h_update.call()
|
67
|
+
end
|
68
|
+
else
|
69
|
+
result = Files.get_json_db(request_body)
|
70
|
+
|
71
|
+
if result == nil
|
72
|
+
CVTool::Event.print('ERROR', "| This '#{request_body}' " +
|
73
|
+
"file cannot be loaded because of an invalid path.")
|
74
|
+
exit
|
75
|
+
end
|
76
|
+
|
77
|
+
content_symbol = Constants::SHEMAS[:project][3].to_s
|
78
|
+
content = result[content_symbol]
|
79
|
+
if content
|
80
|
+
content = Files.get_content( content )
|
81
|
+
result[content_symbol] = content
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
return result
|
86
|
+
end
|
87
|
+
|
88
|
+
def get(args, &block)
|
89
|
+
uri_api = get_uri(args[:endpoint])
|
90
|
+
response = http_response(uri_api)
|
91
|
+
|
92
|
+
CVTool::Event.print('RESPONSE', "#{uri_api} #{JSON.pretty_generate(response)}")
|
93
|
+
if block
|
94
|
+
block.call(response)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def post(args, &block)
|
99
|
+
uri_api = get_uri(args[:endpoint])
|
100
|
+
data = defined_data(args[:endpoint], args[:request_body])
|
101
|
+
response = http_request(uri_api, data)
|
102
|
+
|
103
|
+
CVTool::Event.print('RESPONSE', "#{uri_api} #{JSON.pretty_generate(response)}")
|
104
|
+
if block
|
105
|
+
block.call(response)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def http_request(uri_api, data)
|
110
|
+
|
111
|
+
header = { 'Content-Type': 'text/json' }
|
112
|
+
token = OS.get_token
|
113
|
+
data[:token] = token
|
114
|
+
|
115
|
+
begin
|
116
|
+
uri = URI.parse(uri_api)
|
117
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
118
|
+
http.use_ssl = Event.emit(:is_ssl)
|
119
|
+
|
120
|
+
request = Net::HTTP::Post.new(uri.request_uri, header)
|
121
|
+
request.body = data.to_json
|
122
|
+
|
123
|
+
response = http.request(request)
|
124
|
+
CVTool::Event.print('REQUEST', '| Server has received the request and is processing it.')
|
125
|
+
|
126
|
+
obj = if Files.json?(response.body) then JSON.parse(response.body)
|
127
|
+
else Http.get_status_code(response.body) end
|
128
|
+
|
129
|
+
return obj
|
130
|
+
rescue Errno::ECONNREFUSED => e
|
131
|
+
Event.print('ERROR', "| A request from the server is not " +
|
132
|
+
"answered by this #{uri_api} url address.")
|
133
|
+
exit
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def http_response(uri_api)
|
138
|
+
begin
|
139
|
+
uri = URI(uri_api)
|
140
|
+
response = Net::HTTP.get(uri)
|
141
|
+
obj = if Files.json?(response) then JSON.parse(response)
|
142
|
+
else Http.get_status_code(response) end
|
143
|
+
|
144
|
+
return obj
|
145
|
+
rescue Errno::ECONNREFUSED => e
|
146
|
+
Event.print('ERROR', "| A request from the server is not " +
|
147
|
+
"answered by this #{uri_api} url address.")
|
148
|
+
exit
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def get_route(args)
|
153
|
+
route = ""
|
154
|
+
if args[:is_get]
|
155
|
+
route += "get"
|
156
|
+
else args[:is_post]
|
157
|
+
route += "post"
|
158
|
+
end
|
159
|
+
|
160
|
+
return route
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
data/lib/cv_tool.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'string'
|
2
|
+
require 'json_parser'
|
3
|
+
require 'cv_tool/version'
|
4
|
+
require 'cv_tool/constants'
|
5
|
+
require 'cv_tool/event'
|
6
|
+
require 'cv_tool/files'
|
7
|
+
require 'cv_tool/http'
|
8
|
+
require 'cv_tool/os'
|
9
|
+
require 'cv_tool/rest_api'
|
10
|
+
|
11
|
+
module CVTool
|
12
|
+
module_function
|
13
|
+
|
14
|
+
def generate_token(length = Constants::GT_LENGTH)
|
15
|
+
chars = Array('A'..'Z') + Array('a'..'z') + Array(0..9)
|
16
|
+
token = Array.new(length) { chars.sample }.join
|
17
|
+
return token
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate_db(args)
|
21
|
+
name_dir = args[:endpoint].sub('get/', '')
|
22
|
+
absolute_path_dir = File.expand_path(name_dir, args[:path])
|
23
|
+
contents = args[:response]
|
24
|
+
|
25
|
+
contents.each.with_index do |c, i|
|
26
|
+
content = c.delete_if do |k, _|
|
27
|
+
k == :id.to_s or k == :created_at.to_s or k == :last_change.to_s
|
28
|
+
end
|
29
|
+
name_file = "#{ name_dir.sub(/s$/, '') }_#{i + 1}.json"
|
30
|
+
absolute_path_file = File.join(absolute_path_dir, name_file)
|
31
|
+
json = JsonParser.new(absolute_path_file)
|
32
|
+
|
33
|
+
content.each do |k, v|
|
34
|
+
json.parse(k, v)
|
35
|
+
end
|
36
|
+
CVTool::Event.print('GENERATE', absolute_path_file)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/json_parser.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "json"
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
class JsonParser
|
5
|
+
attr_reader :path, :db
|
6
|
+
|
7
|
+
def initialize path, auto_write = true
|
8
|
+
@path = path
|
9
|
+
@db = open @path
|
10
|
+
@auto_write = auto_write
|
11
|
+
end
|
12
|
+
|
13
|
+
def on symbol, value
|
14
|
+
unless @db.include?(symbol.to_s)
|
15
|
+
parse symbol, value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def parse symbols, value = nil, delete = nil
|
20
|
+
symbols_join = ""
|
21
|
+
if symbols.class.name == "Array"
|
22
|
+
symbols_join = symbols.join("\"][\"")
|
23
|
+
else
|
24
|
+
symbols_join = symbols.to_s
|
25
|
+
end
|
26
|
+
symbols_join = "[\"#{symbols_join}\"]"
|
27
|
+
|
28
|
+
unless delete
|
29
|
+
unless value
|
30
|
+
eval "@db#{ symbols_join }"
|
31
|
+
else
|
32
|
+
eval "@db#{ symbols_join } = value"
|
33
|
+
|
34
|
+
if @auto_write
|
35
|
+
write @path, @db
|
36
|
+
end
|
37
|
+
end
|
38
|
+
else
|
39
|
+
if symbols
|
40
|
+
eval "@db#{ symbols_join }.delete('#{delete}')"
|
41
|
+
else
|
42
|
+
eval "@db.delete('#{delete}')"
|
43
|
+
end
|
44
|
+
|
45
|
+
if @auto_write
|
46
|
+
write @path, @db
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def exist?
|
52
|
+
@db.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete key, symbols = nil
|
56
|
+
parse(symbols, nil, key)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def open path
|
61
|
+
begin
|
62
|
+
result = String.new
|
63
|
+
File.open path do |f|
|
64
|
+
result = JSON.parse f.read
|
65
|
+
end
|
66
|
+
|
67
|
+
return result
|
68
|
+
rescue
|
69
|
+
return Hash.new
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def write path, db
|
74
|
+
begin
|
75
|
+
create_dir path do
|
76
|
+
|
77
|
+
f = File.new path, "w"
|
78
|
+
f.write JSON.pretty_generate db
|
79
|
+
f.close
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_dir path, &callback
|
85
|
+
begin
|
86
|
+
dir_path = File.dirname path
|
87
|
+
unless Dir.exist? dir_path
|
88
|
+
FileUtils.mkpath dir_path
|
89
|
+
end
|
90
|
+
|
91
|
+
callback.call
|
92
|
+
rescue
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
class OptionParser
|
2
|
+
|
3
|
+
LEFT = 2
|
4
|
+
MIDDLE = 33
|
5
|
+
|
6
|
+
attr_reader :args
|
7
|
+
|
8
|
+
def self.parse(args = ARGV)
|
9
|
+
parser = OptionParser.new
|
10
|
+
yield parser
|
11
|
+
parser.process args
|
12
|
+
parser
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.last_arg(args = ARGV)
|
16
|
+
args.length >= 1 ? args[args.length - 1] : nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(args = ARGV)
|
20
|
+
@args = args
|
21
|
+
@banner = nil
|
22
|
+
@flags = Array.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def banner(banner)
|
26
|
+
@banner = banner
|
27
|
+
end
|
28
|
+
|
29
|
+
def on(short_flag, long_flag, description, &block)
|
30
|
+
@flags << { short_flag: short_flag || '', long_flag: long_flag || '',
|
31
|
+
description: description || '', block: block }
|
32
|
+
end
|
33
|
+
|
34
|
+
def process( args = ARGV )
|
35
|
+
unless args.length == 0
|
36
|
+
args.each_with_index do |arg, i|
|
37
|
+
@flags.each do |flag|
|
38
|
+
name = -> (type_flag) do
|
39
|
+
flag[type_flag].gsub( /[a-z -]/, '' )
|
40
|
+
end
|
41
|
+
|
42
|
+
flag_strip = -> (type_flag) do
|
43
|
+
flag[type_flag].sub( name.(type_flag), '' ).strip()
|
44
|
+
end
|
45
|
+
has_flag = -> (type_flag) { arg == flag_strip.(type_flag) }
|
46
|
+
|
47
|
+
if has_flag.(:short_flag) or
|
48
|
+
has_flag.(:long_flag)
|
49
|
+
|
50
|
+
has_name = -> (type_flag) do
|
51
|
+
name.(type_flag) != ""
|
52
|
+
end
|
53
|
+
value = nil
|
54
|
+
|
55
|
+
if has_name.(:short_flag) or
|
56
|
+
has_name.(:long_flag)
|
57
|
+
value = args[i + 1]
|
58
|
+
end
|
59
|
+
|
60
|
+
flag[:block].call( value )
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
else
|
65
|
+
flag = @flags[0]
|
66
|
+
flag[:block].call
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.get_empty_spaces
|
71
|
+
" " * (MIDDLE + LEFT)
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_s()
|
75
|
+
io = Array.new
|
76
|
+
if banner = @banner
|
77
|
+
io << banner
|
78
|
+
io << "\n"
|
79
|
+
end
|
80
|
+
|
81
|
+
@flags.each do |flag|
|
82
|
+
l_flag = !flag[:long_flag].empty? ? ", #{flag[:long_flag]}" : ""
|
83
|
+
flags = "#{flag[:short_flag]}#{l_flag}".ljust(MIDDLE)
|
84
|
+
desc = flag[:description].gsub("\n", "\n#{OptionParser.get_empty_spaces}")
|
85
|
+
io << "".ljust(LEFT) + flags + desc
|
86
|
+
io << "\n"
|
87
|
+
end
|
88
|
+
|
89
|
+
io.join
|
90
|
+
end
|
91
|
+
end
|
data/lib/string.rb
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cv-tool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Filip Vrba
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The tool will allow communication through a Rest API, enabling the retrieval
|
14
|
+
and transmission of data to and from the database. Additionally, it will provide
|
15
|
+
other necessary functions for launching your own CV website.
|
16
|
+
email: filipvrbaxi@gmail.com
|
17
|
+
executables:
|
18
|
+
- cvtool
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- app/arguments.rb
|
23
|
+
- app/configuration.rb
|
24
|
+
- app/inputs.rb
|
25
|
+
- app/main.rb
|
26
|
+
- app/signals.rb
|
27
|
+
- bin/cvtool
|
28
|
+
- lib/cv_tool.rb
|
29
|
+
- lib/cv_tool/constants.rb
|
30
|
+
- lib/cv_tool/event.rb
|
31
|
+
- lib/cv_tool/files.rb
|
32
|
+
- lib/cv_tool/http.rb
|
33
|
+
- lib/cv_tool/os.rb
|
34
|
+
- lib/cv_tool/rest_api.rb
|
35
|
+
- lib/cv_tool/version.rb
|
36
|
+
- lib/json_parser.rb
|
37
|
+
- lib/option_parser.rb
|
38
|
+
- lib/string.rb
|
39
|
+
homepage: https://cvfv.fly.dev/projects/cv
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
source_code_uri: https://github.com/filipvrba/cv-tool-rb
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.3.7
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: CV-Tool facilitates remote manipulation of CV website.
|
63
|
+
test_files: []
|