Module: SevenRooms::ApiHelpers
- Includes:
- MoneyRails::ActionViewExtension
- Included in:
- Authentication, SevenRooms::Availabilities::Availability, Blockages::Create, Reservations::Cancel, Reservations::Create, Reservations::Detail, Reservations::Update, Restaurants::Detail
- Defined in:
- app/services/seven_rooms/api_helpers.rb
Overview
This module contains helper methods for interacting with the SevenRooms API.
Instance Method Summary collapse
- #api_uri ⇒ Object
-
#auth_headers ⇒ Hash
Returns the authentication headers for API requests.
-
#common_headers(auth_token) ⇒ Hash
Generates common headers for API requests.
- #find_seven_rooms_vendor_application ⇒ Object
-
#parse_user_or_guest_full_name(reservation = nil) ⇒ Hash
Parses the full name of a user or guest and returns a hash containing the first name and last name.
- #parsed_vendor_name(reservation) ⇒ Object
- #send_error_availability_notification_to_staff(reservation, error_message, date, start_time, party_size, operation_type = :update) ⇒ Object
-
#sevenrooms_credit_card_error?(error_message) ⇒ Boolean
Checks if an error message is related to credit card requirements.
-
#standardize_or_default_phone(phone_number = nil) ⇒ String
Standardizes a given phone number to E.164 format or returns a default support phone number in E.164 format.
Instance Method Details
#api_uri ⇒ Object
13 14 15 |
# File 'app/services/seven_rooms/api_helpers.rb', line 13 def api_uri AdminSetting.seven_rooms_api_host.to_s end |
#auth_headers ⇒ Hash
Returns the authentication headers for API requests.
20 21 22 23 24 25 |
# File 'app/services/seven_rooms/api_helpers.rb', line 20 def auth_headers { 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json', } end |
#common_headers(auth_token) ⇒ Hash
Generates common headers for API requests.
31 32 33 34 35 36 |
# File 'app/services/seven_rooms/api_helpers.rb', line 31 def common_headers(auth_token) { 'Accept' => 'application/json', 'Authorization' => auth_token, } end |
#find_seven_rooms_vendor_application ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'app/services/seven_rooms/api_helpers.rb', line 71 def find_seven_rooms_vendor_application inv_source_name = ApiVendorV1::Constants::SEVEN_ROOMS_INV_SOURCE_NAME if reservation.third_party_indicator.present? Doorkeeper::Application.find_by(name: reservation.booking_channel.name) else Doorkeeper::Application.find_by(name: inv_source_name) end end |
#parse_user_or_guest_full_name(reservation = nil) ⇒ Hash
Parses the full name of a user or guest and returns a hash containing the first name and last name.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/services/seven_rooms/api_helpers.rb', line 42 def parse_user_or_guest_full_name(reservation = nil) reservation_name = { first_name: 'HungryHub', last_name: 'Customer', } return reservation_name if reservation.blank? parsed_name = reservation.name.split reservation_name[:first_name] = parsed_name.first reservation_name[:last_name] = parsed_name.length > 1 ? parsed_name[1..].join(' ') : '' reservation_name end |
#parsed_vendor_name(reservation) ⇒ Object
129 130 131 132 133 134 |
# File 'app/services/seven_rooms/api_helpers.rb', line 129 def parsed_vendor_name(reservation) vendor_name = reservation&.vendor_reservation&.oauth_application&.name return '' if vendor_name.blank? vendor_name.to_s.split('HH x ').last end |
#send_error_availability_notification_to_staff(reservation, error_message, date, start_time, party_size, operation_type = :update) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/services/seven_rooms/api_helpers.rb', line 93 def send_error_availability_notification_to_staff( reservation, , date, start_time, party_size, operation_type = :update ) is_credit_card_error = sevenrooms_credit_card_error?() # Send notifications for inventory errors and credit card requirement errors return unless SevenRooms::ErrorMessages::INVENTORY_ERRORS.include?() || is_credit_card_error # Use different subject for credit card errors subject = if is_credit_card_error "Booking Rejected – Credit Card Required (SevenRooms #{reservation.restaurant_id})" else "SevenRooms #{operation_type.capitalize}: #{}" end body = <<~BODY #{if reservation.old_reservation_id.present? "Old Reservation ID: #{reservation.old_reservation_id}<br>New Reservation ID: #{reservation.id}<br>" else "Reservation ID: #{reservation.id}<br>" end} Restaurant: #{reservation.restaurant&.name_en}<br> Date: #{date}<br> Time: #{start_time}<br> Party Size: #{party_size}<br> Message: #{} BODY # Get account manager email if available account_manager_email = reservation.restaurant&.user&.email receivers = [::MERCHANT_EMAIL, ::VENDOR_TEAM_EMAIL] receivers << account_manager_email if account_manager_email.present? StaffMailer.notify_error_staff(subject, body, { receivers: receivers }).deliver_later! end |
#sevenrooms_credit_card_error?(error_message) ⇒ Boolean
Checks if an error message is related to credit card requirements
84 85 86 87 88 89 90 91 |
# File 'app/services/seven_rooms/api_helpers.rb', line 84 def sevenrooms_credit_card_error?() return false if .blank? = .downcase SevenRooms::ErrorMessages::CREDIT_CARD_ERRORS.any? do |error| == error || .include?(error.downcase) end end |
#standardize_or_default_phone(phone_number = nil) ⇒ String
Standardizes a given phone number to E.164 format or returns a default support phone number in E.164 format.
61 62 63 64 65 66 67 68 69 |
# File 'app/services/seven_rooms/api_helpers.rb', line 61 def standardize_or_default_phone(phone_number = nil) parsed_phone = Phonelib.parse(phone_number) if parsed_phone.valid? parsed_phone.full_e164 else Phonelib.parse(AdminSetting.support_phone).full_e164 end end |