Class: Servizehub::Client
- Inherits:
-
Object
- Object
- Servizehub::Client
- Defined in:
- lib/servizehub/client.rb
Constant Summary collapse
- BASE_URL =
"https://servizehubuserdev.notasco.in"
Instance Method Summary collapse
-
#initialize(api_key) ⇒ Client
constructor
A new instance of Client.
- #send_booking(date:, service_type:, status:) ⇒ Object
- #send_multiple_bookings(bookings) ⇒ Object
-
#valid_date?(date) ⇒ Boolean
Validate date format YYYY-MM-DD.
Constructor Details
#initialize(api_key) ⇒ Client
Returns a new instance of Client.
10 11 12 13 |
# File 'lib/servizehub/client.rb', line 10 def initialize(api_key) raise ArgumentError, "API key is required" if api_key.nil? || api_key.empty? @api_key = api_key end |
Instance Method Details
#send_booking(date:, service_type:, status:) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/servizehub/client.rb', line 23 def send_booking(date:, service_type:, status:) return "All booking details are required" if date.nil? || service_type.nil? || status.nil? return "Invalid date format, expected YYYY-MM-DD" unless valid_date?(date) valid_statuses = ["available", "unavailable"] return "Invalid status value" unless valid_statuses.include?(status) uri = URI.parse("#{BASE_URL}/external/capture-availability") request = Net::HTTP::Post.new(uri) request["Content-Type"] = "application/json" request["servizhub-api-key"] = @api_key request.body = { date: date, serviceType: service_type, status: status }.to_json response = Net::HTTP.start( uri.hostname, uri.port, use_ssl: uri.scheme == "https" ) do |http| http.request(request) end begin data = JSON.parse(response.body) rescue JSON::ParserError data = { "message" => "Invalid response from server" } end { statusCode: response.code.to_i, data: data } rescue => e e. end |
#send_multiple_bookings(bookings) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/servizehub/client.rb', line 64 def send_multiple_bookings(bookings) return "Bookings array is required" unless bookings.is_a?(Array) && !bookings.empty? failed_bookings = [] successful_bookings = [] bookings.each do |booking| result = send_booking( date: booking[:date], service_type: booking[:service_type], status: booking[:status] ) # local validation / network errors if result.is_a?(String) failed_bookings << booking.merge(error: result) next end # API error response if !result[:statusCode] || result[:statusCode] >= 400 failed_bookings << booking.merge(error: result) else successful_bookings << result end end { message: failed_bookings.empty? ? "All bookings sent successfully" : "Some bookings failed", successfulBookings: successful_bookings, failedBookings: failed_bookings } end |
#valid_date?(date) ⇒ Boolean
Validate date format YYYY-MM-DD
16 17 18 19 20 21 |
# File 'lib/servizehub/client.rb', line 16 def valid_date?(date) Date.strptime(date, '%Y-%m-%d') true rescue ArgumentError false end |