Class: HungryHub::Owner::Codec

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

Constant Summary collapse

SEPARATOR =
'::'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_id: 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/owner/codec.rb', line 9

def initialize(owner_id: nil, hash: nil)
  @owner = ::Owner.find(owner_id) if owner_id
  @hash = ::CGI.unescape(hash) if hash
rescue ::ActiveRecord::RecordNotFound => e
  HH_LOGGER.info e
  @owner = 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/owner/codec.rb', line 6

def hash
  @hash
end

#ownerObject

Returns the value of attribute owner.



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

def owner
  @owner
end

Class Method Details

.decode(hash) ⇒ Object



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

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

.encode(owner_id) ⇒ Object



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

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

Instance Method Details

#decodeObject



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

def decode
  id = ::Owner.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/owner/codec.rb', line 20

def encode
  URI.encode_www_form_component owner.encrypted_id
end

#old_wayObject



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

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