api_demo_v1 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ca0dd6133796d16de45fa0959f86f5d6b1cfb360598b6346540770e9b28fa21
4
+ data.tar.gz: bad4c424848ab4209538cef6def480ff060d11ec196272576f0167e9d7219395
5
+ SHA512:
6
+ metadata.gz: 49459f298c5ce9d2e300027eabbd2decb4e677a8c1221a46e13cf790d42582a2d2bd37a96ea5b50099dc8df16480883dbc88bfad066893e104b2d76f4dc8e1fa
7
+ data.tar.gz: dc355a60430486eba6803572e73266ee444a4c7df1cff5722b95b50119beb2a45753894cea524595d1279da41fb535579bf32bb10b6ec0416badecc6f3c155b7
@@ -0,0 +1,3 @@
1
+ require "api_demo_v1/version"
2
+ require "api_demo_v1/sabeq_helpers"
3
+ require "api_demo_v1/railtie" if defined? Rails
@@ -0,0 +1,9 @@
1
+ module ApiDemoV1
2
+ class Railtie < Rails::Railtie
3
+ initializer "controllers_include" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include ApiDemoV1::SabeqHelpers
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,101 @@
1
+ require "net/http"
2
+ require "net/https"
3
+
4
+ module ApiDemoV1
5
+ module SabeqHelpers
6
+ REQUEST_HEADER = {'Content-Type' => 'application/json'}
7
+ SABEQ_URL = "https://localhost:3001/"
8
+
9
+ # Authorize the request
10
+ # parameters: login_token
11
+ # return: true, auth_token in case of success
12
+ # false, errors in case of failure, errors contain code and message
13
+ def sabeq_api_v1_authorize(login_token)
14
+ auth_link = SABEQ_URL + "/api/v1/auth"
15
+ auth_json = { login_token: login_token }
16
+ json_response = make_post_request(auth_link, auth_json)
17
+
18
+ result, the_response = get_error_or_returned_value(json_response)
19
+ if result
20
+ return true, the_response["auth_token"]
21
+ else
22
+ return false, the_response
23
+ end
24
+ end
25
+
26
+ # Verify the business profile
27
+ def sabeq_api_v1_verify_profile(auth_token, profile_id, api_key)
28
+ auth_link = SABEQ_URL + "/api/v1/verify_business"
29
+ auth_json = { auth_token: auth_token, profile_id: profile_id, api_key: api_key }
30
+ json_response = make_post_request(auth_link, auth_json)
31
+
32
+ result, the_response = get_error_or_returned_value(json_response)
33
+ if result
34
+ return true, the_response["verification_token"]
35
+ else
36
+ return false, the_response
37
+ end
38
+ end
39
+
40
+ def sabeq_api_v1_query_parcel(verification_token, parcel_number)
41
+ auth_link = SABEQ_URL + "/api/v1/parcels/#{parcel_number}"
42
+ auth_json = { verification_token: verification_token }
43
+ json_response = make_get_request(auth_link, auth_json)
44
+ return json_response
45
+ end
46
+
47
+ def sabeq_api_v1_get_areas(verification_token)
48
+ auth_link = SABEQ_URL + "/api/v1/parcels/get_areas"
49
+ auth_json = { verification_token: verification_token }
50
+ json_response = make_get_request(auth_link, auth_json)
51
+ return json_response
52
+ end
53
+
54
+ def sabeq_api_v1_create_parcel(verification_token, name, phone1, phone2, content,
55
+ payment_amount, area_id, address, delivery_notes)
56
+ auth_link = SABEQ_URL + "/api/v1/parcels"
57
+ auth_json = { verification_token: verification_token,
58
+ name: name, phone1: phone1, phone2: phone2,
59
+ content: content, payment_amount: payment_amount,
60
+ area_id: area_id, address: address,
61
+ delivery_notes: delivery_notes }
62
+ json_response = make_post_request(auth_link, auth_json)
63
+
64
+ return json_response
65
+ end
66
+
67
+ private
68
+ def make_post_request(url_link, json_content)
69
+ uri = URI.parse(url_link)
70
+ http = Net::HTTP.new(uri.host, uri.port)
71
+ http.use_ssl = false
72
+ a_request = Net::HTTP::Post.new(uri.request_uri, REQUEST_HEADER)
73
+ a_request.body = json_content.to_json
74
+ a_response = http.request(a_request)
75
+ return a_response.body
76
+ end
77
+
78
+ def make_get_request(url_link, json_content)
79
+ uri = URI.parse(url_link)
80
+ http = Net::HTTP.new(uri.host, uri.port)
81
+ http.use_ssl = false
82
+ the_params = json_content
83
+ uri.query = URI.encode_www_form(the_params)
84
+ a_request = Net::HTTP::Get.new(uri.request_uri)
85
+ a_response = http.request(a_request)
86
+ return a_response.body
87
+ end
88
+
89
+ def get_error_or_returned_value(json_response)
90
+ # check if the response has errors
91
+ hashed_response = JSON.parse(json_response)
92
+ errors = hashed_response["errors"]
93
+
94
+ if errors.present?
95
+ return false, errors
96
+ else
97
+ return true, hashed_response
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module ApiDemoV1
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_demo_v1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ShaimaKaraki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Its a gem for sabeq.ps API helper functionalities
14
+ email:
15
+ - shaimakaraki@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/api_demo_v1.rb
21
+ - lib/api_demo_v1/railtie.rb
22
+ - lib/api_demo_v1/sabeq_helpers.rb
23
+ - lib/api_demo_v1/version.rb
24
+ homepage: https://github.com/ShaimaKaraki/s_api.git
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.4.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.0.8
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: gem for sabeq.ps API helper functionalities
47
+ test_files: []