demo_api 0.1.0 → 0.1.1
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 +4 -4
- data/lib/demo_api.rb +45 -1
- data/lib/demo_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2795aef5413bd122105e08229d2cd67656627ca981b5cea22f909c71c7957f4b
|
4
|
+
data.tar.gz: 588d0c6be6e901d39cf2d2030040fd21d8c544a383ee2f7d90a9703e099439b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 365099f1e8e8cdbf53008f836aa265e75cd7075160202646658f1614a9df7e1e2777239506344f69b54208931787090d4ac3eef3ba7034e32217dd5b22397aa6
|
7
|
+
data.tar.gz: 7acb10692d0456ba7d6e70ce2323b5ada47f0e058adfbc4ae0888041b94bcff969314189e4a4abe2c9b773ce79f9caaa1a457b4350576900c6c2ea806f4960d6
|
data/lib/demo_api.rb
CHANGED
@@ -1,9 +1,53 @@
|
|
1
1
|
require_relative "demo_api/version"
|
2
2
|
|
3
3
|
module DemoApi
|
4
|
+
REQUEST_HEADER = {'Content-Type' => 'application/json'}
|
5
|
+
#SABEQ_URL = "https://sabeq.ps"
|
6
|
+
SABEQ_URL = "https://localhost:3001/"
|
7
|
+
|
4
8
|
class << self
|
5
9
|
def print_hello
|
6
|
-
|
10
|
+
SABEQ_URL
|
11
|
+
end
|
12
|
+
|
13
|
+
# Authorize the request
|
14
|
+
# parameters: login_token
|
15
|
+
# return: true, auth_token in case of success
|
16
|
+
# false, errors in case of failure, errors contain code and message
|
17
|
+
def authorize(login_token)
|
18
|
+
auth_link = SABEQ_URL + "/api/v1/auth"
|
19
|
+
auth_json = { login_token: login_token }
|
20
|
+
json_response = make_post_request(auth_link, auth_json)
|
21
|
+
|
22
|
+
result, the_response = get_error_or_returned_value(json_response)
|
23
|
+
if result
|
24
|
+
return true, the_response["auth_token"]
|
25
|
+
else
|
26
|
+
return false, the_response
|
27
|
+
end
|
7
28
|
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def make_post_request(url_link, json_content)
|
32
|
+
uri = URI.parse(url_link)
|
33
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
34
|
+
http.use_ssl = false
|
35
|
+
a_request = Net::HTTP::Post.new(uri.request_uri, REQUEST_HEADER)
|
36
|
+
a_request.body = json_content.to_json
|
37
|
+
a_response = http.request(a_request)
|
38
|
+
return a_response.body
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_error_or_returned_value(json_response)
|
42
|
+
# check if the response has errors
|
43
|
+
hashed_response = JSON.parse(json_response)
|
44
|
+
errors = hashed_response["errors"]
|
45
|
+
|
46
|
+
if errors.present?
|
47
|
+
return false, errors
|
48
|
+
else
|
49
|
+
return true, hashed_response
|
50
|
+
end
|
51
|
+
end
|
8
52
|
end
|
9
53
|
end
|
data/lib/demo_api/version.rb
CHANGED