Class: RatingUrlService

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/rating_url_service.rb

Overview

Service object for generating rating/review URLs

This service centralizes the business logic for generating rating URLs for both signed users (members) and guests. It replaces the RatingUrlHelper module to better align with service-oriented architecture principles.

Benefits over helper module:

  • Clear single responsibility

  • Easier to test in isolation

  • Better dependency injection

  • More maintainable and extensible

Usage:

# In mailers, serializers, or other services
service = RatingUrlService.new(reservation)
url = service.url_for

# Or for specific URL types
member_url = service.member_url
guest_url = service.guest_url(rate: 5)

# With rate parameter for pre-selecting star rating
member_url_with_rate = service.member_url(rate: 4)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reservation) ⇒ RatingUrlService

Initializes the service with a reservation

Parameters:



36
37
38
# File 'app/services/rating_url_service.rb', line 36

def initialize(reservation)
  @reservation = reservation
end

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



31
32
33
# File 'app/services/rating_url_service.rb', line 31

def reservation
  @reservation
end

Instance Method Details

#guest_url(rate: nil, only_path: true) ⇒ String

Generates a rating URL for a guest (non-signed user)

Uses the legacy format with encrypted hash: /reservations/rating/hash?rate=rate

Examples:

service.guest_url(rate: 5)
# => "/reservations/rating/abc123def456?rate=5"

service.guest_url(only_path: false)
# => "https://example.com/reservations/rating/abc123def456"

Parameters:

  • rate (Integer, nil) (defaults to: nil)

    The rating value (1-5 stars), optional

  • only_path (Boolean) (defaults to: true)

    Whether to return only the path or full URL (default: true)

Returns:

  • (String)

    The rating URL for the guest



117
118
119
120
121
122
123
124
125
# File 'app/services/rating_url_service.rb', line 117

def guest_url(rate: nil, only_path: true)
  hash = encoded_reservation_hash

  url = build_guest_url(hash, rate, only_path)

  log_guest_url_generation(url, rate, only_path)

  url
end

#member_url(rate: nil) ⇒ String

Generates a rating URL for a signed user (member)

Uses the new rating frontend with format: rating_frontend_host/language/review/reservation_id?rate=rate

Examples:

service.member_url
# => "https://hotfeat-rating.pages.dev/en/review/12345"

service.member_url(rate: 5)
# => "https://hotfeat-rating.pages.dev/en/review/12345?rate=5"

Parameters:

  • rate (Integer, nil) (defaults to: nil)

    The rating value (1-5 stars) for pre-selecting rating, optional

Returns:

  • (String)

    The rating URL for the member



90
91
92
93
94
95
96
97
98
99
# File 'app/services/rating_url_service.rb', line 90

def member_url(rate: nil)
  user = reservation.user
  language = user&.language.presence || MyLocaleManager.default_locale
  base_url = "#{AdminSetting.rating_frontend_host}/#{language}/review/#{reservation.id}"
  url = append_rate_param(base_url, rate)

  log_member_url_generation(url, language, user, rate)

  url
end

#url_for(rate: nil) ⇒ String

Generates the appropriate rating URL based on user type

Automatically determines if the reservation is for a signed user or guest and returns the corresponding URL format.

Routes to member URL only if user association is present and loaded. This prevents routing to member_url for orphaned user_ids.

Examples:

# For signed user
service.url_for
# => "https://hotfeat-rating.pages.dev/en/review/12345"

# For signed user with rate
service.url_for(rate: 5)
# => "https://hotfeat-rating.pages.dev/en/review/12345?rate=5"

# For guest
service.url_for(rate: 4)
# => "/reservations/rating/abc123def456?rate=4"

Parameters:

  • rate (Integer, nil) (defaults to: nil)

    The rating value for pre-selecting star rating

Returns:

  • (String)

    The appropriate rating URL



63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/rating_url_service.rb', line 63

def url_for(rate: nil)
  user_type = reservation.user.present? ? 'member' : 'guest'

  log_routing_decision(user_type, rate)

  if reservation.user.present?
    member_url(rate: rate)
  else
    guest_url(rate: rate)
  end
end