Class: HungryHub::RestaurantGroup::Codec

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

Constant Summary collapse

SEPARATOR =
'::'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restaurant_group: nil, hash: nil) ⇒ Codec

Returns a new instance of Codec.



9
10
11
12
13
14
15
16
17
18
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 9

def initialize(restaurant_group: nil, hash: nil)
  @restaurant_group = ::RestaurantGroup.find(restaurant_group) if restaurant_group
  @hash = ::CGI.unescape(hash) if hash
rescue ::ActiveRecord::RecordNotFound => e
  HH_LOGGER.info e
  @restaurant_group = nil
rescue ::NoMethodError => e
  HH_LOGGER.info e
  return false
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



6
7
8
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 6

def hash
  @hash
end

#restaurant_groupObject

Returns the value of attribute restaurant_group.



6
7
8
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 6

def restaurant_group
  @restaurant_group
end

Class Method Details

.decode(hash) ⇒ Object



42
43
44
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 42

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

.encode(restaurant_group) ⇒ Object



38
39
40
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 38

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

Instance Method Details

#decodeObject



24
25
26
27
28
29
30
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 24

def decode
  id = ::RestaurantGroup.decrypt_id(::CGI.unescape(hash))
  return id unless id.nil?
  old_way
rescue Hashids::InputError
  old_way
end

#encodeObject



20
21
22
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 20

def encode
  URI.encode_www_form_component restaurant_group.encrypted_id
end

#old_wayObject



32
33
34
35
36
# File 'app/my_lib/hungry_hub/restaurant_group/codec.rb', line 32

def old_way
  id = Base64.decode64(::CGI.unescape(hash)).to_s.split(SEPARATOR).first.to_i
  return false if id.zero?
  id
end