Module: CVTool::RestApi

Defined in:
lib/cv_tool/rest_api.rb

Class Method Summary collapse

Class Method Details

.define_method(endpoint, h_get, h_post) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/cv_tool/rest_api.rb', line 13

def define_method(endpoint, h_get, h_post)
  method = endpoint.sub(/\/.*$/, '')
  
  case method
  when Constants::METHODS[0]
    h_get.call()
  when Constants::METHODS[1]
    h_post.call()
  end
end

.defined_data(endpoint, request_body) ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cv_tool/rest_api.rb', line 24

def defined_data(endpoint, request_body)
  result = {}

  if request_body == nil
    h_event_print = lambda do
      Event.print('INPUT', '| The data for the following ' +
        'process is defined by the input process.')
    end
    h_add = lambda do |symbol|
      h_event_print.call()
      result = Event::emit(:input_add, symbol)
    end
    h_free = lambda do
      h_event_print.call()
      result = Event::emit(:input_free)
    end
    h_update = lambda do
      h_event_print.call()
      result = Event::emit(:input_update)
    end

    case endpoint
    # Articles
    when Constants::ENDPOINTS[3]
      h_add.call(:article)
    when Constants::ENDPOINTS[4]
      h_free.call()
    when Constants::ENDPOINTS[5]
      h_update.call()
    # Projects
    when Constants::ENDPOINTS[6]
      h_add.call(:project)
    when Constants::ENDPOINTS[7]
      h_free.call()
    when Constants::ENDPOINTS[8]
      h_update.call()
    # Profiles
    when Constants::ENDPOINTS[9]
      h_add.call(:profile)
    when Constants::ENDPOINTS[10]
      h_free.call()
    when Constants::ENDPOINTS[11]
      h_update.call()
    end
  else
    result = Files.get_json_db(request_body)

    if result == nil
      CVTool::Event.print('ERROR', "| This '#{request_body}' " +
        "file cannot be loaded because of an invalid path.")
      exit
    end

    content_symbol = Constants::SHEMAS[:project][3].to_s
    content = result[content_symbol]
    if content
      content = Files.get_content( content )
      result[content_symbol] = content
    end
  end

  return result
end

.get(args, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/cv_tool/rest_api.rb', line 88

def get(args, &block)
  uri_api = get_uri(args[:endpoint])
  response = http_response(uri_api)

  CVTool::Event.print('RESPONSE', "#{uri_api} #{JSON.pretty_generate(response)}")
  if block
    block.call(response)
  end
end

.get_route(args) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/cv_tool/rest_api.rb', line 152

def get_route(args)
  route = ""
  if args[:is_get]
    route += "get"
  else args[:is_post]
    route += "post"
  end

  return route
end

.get_uri(endpoint) ⇒ Object



9
10
11
# File 'lib/cv_tool/rest_api.rb', line 9

def get_uri(endpoint)
  return "#{Event.emit(:api_url)}/#{ endpoint }"
end

.http_request(uri_api, data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cv_tool/rest_api.rb', line 109

def http_request(uri_api, data)

  header = { 'Content-Type': 'text/json' }
  token = OS.get_token
  data[:token] = token

  begin
    uri = URI.parse(uri_api)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = Event.emit(:is_ssl)

    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = data.to_json

    response = http.request(request)
    CVTool::Event.print('REQUEST', '| Server has received the request and is processing it.')

    obj = if Files.json?(response.body) then JSON.parse(response.body)
            else Http.get_status_code(response.body) end

    return obj
  rescue Errno::ECONNREFUSED => e
    Event.print('ERROR', "| A request from the server is not " +
      "answered by this #{uri_api} url address.")
    exit
  end
end

.http_response(uri_api) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cv_tool/rest_api.rb', line 137

def http_response(uri_api)
  begin
    uri = URI(uri_api)
    response = Net::HTTP.get(uri)
    obj = if Files.json?(response) then JSON.parse(response)
            else Http.get_status_code(response) end

    return obj
  rescue Errno::ECONNREFUSED => e
    Event.print('ERROR', "| A request from the server is not " +
      "answered by this #{uri_api} url address.")
    exit
  end
end

.post(args, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/cv_tool/rest_api.rb', line 98

def post(args, &block)
  uri_api = get_uri(args[:endpoint])
  data = defined_data(args[:endpoint], args[:request_body])
  response = http_request(uri_api, data)

  CVTool::Event.print('RESPONSE', "#{uri_api} #{JSON.pretty_generate(response)}")
  if block
    block.call(response)
  end
end