Class: HungryHub::Reservation::Codec

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/hungry_hub/reservation/codec.rb

Overview

Hash Reservation ID to not be opened for public

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reservation_id: nil, res_hash: nil) ⇒ Codec

Returns a new instance of Codec.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 12

def initialize(reservation_id: nil, res_hash: nil)
  business_context = {}
  if reservation_id
    business_context[:reservation_id] = reservation_id
    @reservation = begin
      ::Reservation.fetch(reservation_id)
    rescue StandardError
      nil
    end
  end
  if res_hash
    business_context[:res_hash] = res_hash
    @res_hash = begin
      ::URI.decode_www_form_component(res_hash)
    rescue StandardError
      nil
    end
  end

  CUSTOM_LOGGERS.each do |logger|
    logger.set_business_context(business_context)
  end
end

Instance Attribute Details

#res_hashObject

Returns the value of attribute res_hash.



8
9
10
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 8

def res_hash
  @res_hash
end

#reservationObject

Returns the value of attribute reservation.



8
9
10
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 8

def reservation
  @reservation
end

Class Method Details

.decode(res_hash) ⇒ Object



67
68
69
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 67

def self.decode(res_hash)
  new(res_hash: res_hash).decode
end

.encode(reservation_id) ⇒ Object



63
64
65
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 63

def self.encode(reservation_id)
  new(reservation_id: reservation_id).encode
end

Instance Method Details

#decodeObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 40

def decode
  id = ::Reservation.decrypt_id(URI.decode_www_form_component(res_hash))
  if id.present?
    CUSTOM_LOGGERS.each do |logger|
      logger.set_business_context(reservation_id: id)
    end
    return id
  end
  old_way
rescue Hashids::InputError
  old_way
end

#encodeObject



36
37
38
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 36

def encode
  URI.encode_www_form_component reservation.encrypted_id
end

#old_wayObject



53
54
55
56
57
58
59
60
61
# File 'app/my_lib/hungry_hub/reservation/codec.rb', line 53

def old_way
  id = Base64.decode64(URI.decode_www_form_component(res_hash)).to_s.split(SEPARATOR).first.to_i
  return false if id.zero?

  CUSTOM_LOGGERS.each do |logger|
    logger.set_business_context(reservation_id: id)
  end
  id
end