demo_api 0.1.0 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2eaf0961d88c28b984171f293da80f479171bdcb0e8c14f7e56047d7a273893e
4
- data.tar.gz: 6de767222e975bee1da83983e7b5422386c808015d1966ea7bfe219665545713
3
+ metadata.gz: f8d020c180eee5b2934657cace1ac5915589ec0f49b8d651555b5d4b25d5716b
4
+ data.tar.gz: 6cbef2ebc1ca75003f692cd74652bbd7214375d6ec700aa83106d22d2be1219e
5
5
  SHA512:
6
- metadata.gz: 545c8b7ced3f5e8a31b5614f0078dc2c00491015250a5783ae79efdd814f81ec17abfd000ffdf89199f16942b37203b2e5257a88c00b2864a8a2e9c795678623
7
- data.tar.gz: 2f6976d158ee7431e3d3ad793e43c91d7dce2aa1011eb0468efbefe5360d4193140141ce132ce2aaf79f326f9491bd1babdf7dd9d8edf4ff2a5771fa4e0d5621
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
- "Hello world!!!!!!!!!!!!!!!!"
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
@@ -1,3 +1,3 @@
1
1
  module DemoApi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: demo_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShaimaKaraki