Class: Servizehub::Client
- Inherits:
-
Object
- Object
- Servizehub::Client
- Defined in:
- lib/servizehub/client.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
Production API base URL, used when no base_url is supplied.
"https://service.servizehub.com/external"
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#initialize(api_key, base_url: nil) ⇒ Client
constructor
Omit base_url to hit production; pass it to target another environment, e.g.
- #send_booking(date:, service_type:, status:) ⇒ Object
- #send_multiple_bookings(bookings) ⇒ Object
-
#valid_date?(date) ⇒ Boolean
Validate date format YYYY-MM-DD Date.strptime is lenient (it accepts "15-04-2026"), so the shape is checked with a strict regex before parsing.
Constructor Details
#initialize(api_key, base_url: nil) ⇒ Client
Omit base_url to hit production; pass it to target another environment, e.g. "https://servizehubuserdev.notasco.in/external".
15 16 17 18 19 20 21 |
# File 'lib/servizehub/client.rb', line 15 def initialize(api_key, base_url: nil) raise ArgumentError, "API key is required" if api_key.nil? || api_key.empty? @api_key = api_key resolved = base_url.nil? || base_url.empty? ? DEFAULT_BASE_URL : base_url @base_url = resolved.sub(%r{/+\z}, "") end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/servizehub/client.rb', line 11 def base_url @base_url end |
Instance Method Details
#send_booking(date:, service_type:, status:) ⇒ Object
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 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/servizehub/client.rb', line 35 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}/capture-availability") request = Net::HTTP::Post.new(uri) request["Content-Type"] = "application/json" request["servizehub-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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/servizehub/client.rb', line 76 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 Date.strptime is lenient (it accepts "15-04-2026"), so the shape is checked with a strict regex before parsing.
26 27 28 29 30 31 32 33 |
# File 'lib/servizehub/client.rb', line 26 def valid_date?(date) return false unless date.is_a?(String) && date.match?(/\A\d{4}-\d{2}-\d{2}\z/) Date.strptime(date, '%Y-%m-%d') true rescue ArgumentError false end |