Class: PetstoreApiClient::Models::Order
- Defined in:
- lib/petstore_api_client/models/order.rb
Overview
Order model representing a store order TODO: Should validate quantity > 0 and pet_id presence, but assignment doc didn’t specify
Constant Summary collapse
- VALID_STATUSES =
%w[placed approved delivered].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#to_h ⇒ Object
Note: API expects snake_case to be converted to camelCase.
Class Method Details
.from_response(data) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/petstore_api_client/models/order.rb', line 31 def self.from_response(data) return nil if data.nil? # Parse ship_date if it's a string ship_date = data["shipDate"] || data[:shipDate] || data[:ship_date] ship_date = parse_datetime(ship_date) if ship_date.is_a?(String) new( id: data["id"] || data[:id], pet_id: data["petId"] || data[:petId] || data[:pet_id], quantity: data["quantity"] || data[:quantity], ship_date: ship_date, status: data["status"] || data[:status], complete: data["complete"] || data[:complete] || false ) end |
.parse_datetime(datetime_string) ⇒ Object
48 49 50 51 52 |
# File 'lib/petstore_api_client/models/order.rb', line 48 def self.parse_datetime(datetime_string) DateTime.parse(datetime_string) rescue ArgumentError nil # Silently fail - API shouldn't send invalid dates anyway end |
Instance Method Details
#to_h ⇒ Object
Note: API expects snake_case to be converted to camelCase
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/petstore_api_client/models/order.rb', line 20 def to_h { id: id, petId: pet_id, quantity: quantity, shipDate: ship_date&.iso8601, status: status, complete: complete }.compact end |