demo_api 0.1.0 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/demo_api.rb +79 -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: f8d020c180eee5b2934657cace1ac5915589ec0f49b8d651555b5d4b25d5716b
|
4
|
+
data.tar.gz: 6cbef2ebc1ca75003f692cd74652bbd7214375d6ec700aa83106d22d2be1219e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b34ad7e69d6665d126057276db88467d1bf302db865efe62cc4156b7ab97eff9e7b82999821683141241ea63ac148b613427817a33c5138288a901170f609ca5
|
7
|
+
data.tar.gz: f3e418969dab3991b1a23e6be13388e64974952d63f8d87431728225a4d3a3fb2bb031c091b49992952060ec3020e231ea9f343665aa563f475ad229f93072bc
|
data/lib/demo_api.rb
CHANGED
@@ -1,9 +1,87 @@
|
|
1
1
|
require_relative "demo_api/version"
|
2
|
+
require "net/http"
|
3
|
+
require "net/https"
|
2
4
|
|
3
5
|
module DemoApi
|
6
|
+
REQUEST_HEADER = {'Content-Type' => 'application/json'}
|
7
|
+
#SABEQ_URL = "https://sabeq.ps"
|
8
|
+
SABEQ_URL = "https://localhost:3001/"
|
9
|
+
|
4
10
|
class << self
|
5
11
|
def print_hello
|
6
|
-
|
12
|
+
SABEQ_URL
|
13
|
+
end
|
14
|
+
|
15
|
+
# Authorize the request
|
16
|
+
# parameters: login_token
|
17
|
+
# return: true, auth_token in case of success
|
18
|
+
# false, errors in case of failure, errors contain code and message
|
19
|
+
def authorize(login_token)
|
20
|
+
auth_link = SABEQ_URL + "/api/v1/auth"
|
21
|
+
auth_json = { login_token: login_token }
|
22
|
+
json_response = make_post_request(auth_link, auth_json)
|
23
|
+
|
24
|
+
result, the_response = get_error_or_returned_value(json_response)
|
25
|
+
if result
|
26
|
+
return true, the_response["auth_token"]
|
27
|
+
else
|
28
|
+
return false, the_response
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Verify the business profile
|
33
|
+
def verify_profile(auth_token, profile_id, api_key)
|
34
|
+
auth_link = SABEQ_URL + "/api/v1/verify_business"
|
35
|
+
auth_json = { auth_token: auth_token, profile_id: profile_id, api_key: api_key }
|
36
|
+
json_response = make_post_request(auth_link, auth_json)
|
37
|
+
|
38
|
+
result, the_response = get_error_or_returned_value(json_response)
|
39
|
+
if result
|
40
|
+
return true, the_response["verification_token"]
|
41
|
+
else
|
42
|
+
return false, the_response
|
43
|
+
end
|
7
44
|
end
|
45
|
+
|
46
|
+
def query_parcel(verification_token, parcel_number)
|
47
|
+
auth_link = SABEQ_URL + "/api/v1/parcels/#{parcel_number}"
|
48
|
+
auth_json = { verification_token: verification_token }
|
49
|
+
json_response = make_get_request(auth_link, auth_json)
|
50
|
+
return json_response
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def make_post_request(url_link, json_content)
|
55
|
+
uri = URI.parse(url_link)
|
56
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
57
|
+
http.use_ssl = false
|
58
|
+
a_request = Net::HTTP::Post.new(uri.request_uri, REQUEST_HEADER)
|
59
|
+
a_request.body = json_content.to_json
|
60
|
+
a_response = http.request(a_request)
|
61
|
+
return a_response.body
|
62
|
+
end
|
63
|
+
|
64
|
+
def make_get_request(url_link, json_content)
|
65
|
+
uri = URI.parse(url_link)
|
66
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
67
|
+
http.use_ssl = false
|
68
|
+
the_params = json_content
|
69
|
+
uri.query = URI.encode_www_form(the_params)
|
70
|
+
a_request = Net::HTTP::Get.new(uri.request_uri)
|
71
|
+
a_response = http.request(a_request)
|
72
|
+
return a_response.body
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_error_or_returned_value(json_response)
|
76
|
+
# check if the response has errors
|
77
|
+
hashed_response = JSON.parse(json_response)
|
78
|
+
errors = hashed_response["errors"]
|
79
|
+
|
80
|
+
if errors.present?
|
81
|
+
return false, errors
|
82
|
+
else
|
83
|
+
return true, hashed_response
|
84
|
+
end
|
85
|
+
end
|
8
86
|
end
|
9
87
|
end
|
data/lib/demo_api/version.rb
CHANGED