servizehub 1.0.1 → 1.0.3
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/servizehub/client.rb +44 -43
- data/lib/servizehub/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: 77e3e2da8a75ddc0ab89805b772214d58f81415bc264b025c79fb81fb1677af7
|
|
4
|
+
data.tar.gz: 0e3337c741f5ae796d80c6722ac5cf86725a7c46a1333d64ca9a85fa97518f7b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85c96d41f1f00470aff1cf7a74f84d87c8708b5613d5fb21d32f72790740f1cadb68fdcea5ec6e1a5c6ee2ca5fb3f780a1edaa1b9501d3eca9bac47289668e9e
|
|
7
|
+
data.tar.gz: df38414d443644c58e404577026530435d36217e14f50f3f1afa7d20272a6762cbb11fa70550e2d175ae4dcc2ed852512b3fcfc9fb106a7f2800021e87222f6e
|
data/lib/servizehub/client.rb
CHANGED
|
@@ -12,7 +12,7 @@ module Servizehub
|
|
|
12
12
|
@api_key = api_key
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
#
|
|
15
|
+
# Validate date format YYYY-MM-DD
|
|
16
16
|
def valid_date?(date)
|
|
17
17
|
Date.strptime(date, '%Y-%m-%d')
|
|
18
18
|
true
|
|
@@ -21,79 +21,80 @@ module Servizehub
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def send_booking(date:, service_type:, status:)
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
return "All booking details are required" if date.nil? || service_type.nil? || status.nil?
|
|
25
|
+
return "Invalid date format, expected YYYY-MM-DD" unless valid_date?(date)
|
|
26
26
|
|
|
27
27
|
valid_statuses = ["available", "unavailable"]
|
|
28
|
-
|
|
28
|
+
return "Invalid status value" unless valid_statuses.include?(status)
|
|
29
29
|
|
|
30
30
|
uri = URI.parse("#{BASE_URL}/external/capture-availability")
|
|
31
|
+
|
|
31
32
|
request = Net::HTTP::Post.new(uri)
|
|
32
33
|
request["Content-Type"] = "application/json"
|
|
33
34
|
request["servizhub-api-key"] = @api_key
|
|
35
|
+
|
|
34
36
|
request.body = {
|
|
35
37
|
date: date,
|
|
36
38
|
serviceType: service_type,
|
|
37
39
|
status: status
|
|
38
40
|
}.to_json
|
|
39
41
|
|
|
40
|
-
response = Net::HTTP.start(
|
|
42
|
+
response = Net::HTTP.start(
|
|
43
|
+
uri.hostname,
|
|
44
|
+
uri.port,
|
|
45
|
+
use_ssl: uri.scheme == "https"
|
|
46
|
+
) do |http|
|
|
41
47
|
http.request(request)
|
|
42
48
|
end
|
|
43
49
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
rescue
|
|
49
|
-
message = "Booking failed"
|
|
50
|
-
end
|
|
51
|
-
raise message
|
|
50
|
+
begin
|
|
51
|
+
data = JSON.parse(response.body)
|
|
52
|
+
rescue JSON::ParserError
|
|
53
|
+
data = { "message" => "Invalid response from server" }
|
|
52
54
|
end
|
|
53
55
|
|
|
54
|
-
{
|
|
56
|
+
{
|
|
57
|
+
statusCode: response.code.to_i,
|
|
58
|
+
data: data
|
|
59
|
+
}
|
|
60
|
+
rescue => e
|
|
61
|
+
e.message
|
|
55
62
|
end
|
|
56
63
|
|
|
57
|
-
# Bulk bookings
|
|
58
64
|
def send_multiple_bookings(bookings)
|
|
59
|
-
|
|
65
|
+
return "Bookings array is required" unless bookings.is_a?(Array) && !bookings.empty?
|
|
60
66
|
|
|
61
|
-
valid_statuses = ["available", "unavailable"]
|
|
62
67
|
failed_bookings = []
|
|
68
|
+
successful_bookings = []
|
|
63
69
|
|
|
64
70
|
bookings.each do |booking|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
unless valid_date?(date)
|
|
76
|
-
failed_bookings << booking.merge(error: "Invalid date format, expected YYYY-MM-DD")
|
|
77
|
-
next
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
unless valid_statuses.include?(status)
|
|
81
|
-
failed_bookings << booking.merge(error: "Invalid status value")
|
|
71
|
+
result = send_booking(
|
|
72
|
+
date: booking[:date],
|
|
73
|
+
service_type: booking[:service_type],
|
|
74
|
+
status: booking[:status]
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# local validation / network errors
|
|
78
|
+
if result.is_a?(String)
|
|
79
|
+
failed_bookings << booking.merge(error: result)
|
|
82
80
|
next
|
|
83
81
|
end
|
|
84
82
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
# API error response
|
|
84
|
+
if !result[:statusCode] || result[:statusCode] >= 400
|
|
85
|
+
failed_bookings << booking.merge(error: result)
|
|
86
|
+
else
|
|
87
|
+
successful_bookings << result
|
|
89
88
|
end
|
|
90
89
|
end
|
|
91
90
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
{
|
|
92
|
+
message: failed_bookings.empty? ?
|
|
93
|
+
"All bookings sent successfully" :
|
|
94
|
+
"Some bookings failed",
|
|
95
|
+
successfulBookings: successful_bookings,
|
|
96
|
+
failedBookings: failed_bookings
|
|
97
|
+
}
|
|
97
98
|
end
|
|
98
99
|
end
|
|
99
100
|
end
|
data/lib/servizehub/version.rb
CHANGED