Class: HungryHub::Owner::Codec
- Inherits:
-
Object
- Object
- HungryHub::Owner::Codec
- Defined in:
- app/my_lib/hungry_hub/owner/codec.rb
Constant Summary collapse
- SEPARATOR =
'::'
Instance Attribute Summary collapse
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#owner ⇒ Object
Returns the value of attribute owner.
Class Method Summary collapse
Instance Method Summary collapse
- #decode ⇒ Object
- #encode ⇒ Object
-
#initialize(owner_id: nil, hash: nil) ⇒ Codec
constructor
A new instance of Codec.
- #old_way ⇒ Object
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
#hash ⇒ Object
Returns the value of attribute hash.
6 7 8 |
# File 'app/my_lib/hungry_hub/owner/codec.rb', line 6 def hash @hash end |
#owner ⇒ Object
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
#decode ⇒ Object
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 |
#encode ⇒ Object
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_way ⇒ Object
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 |