Class: Tincan::PhoneNumber
- Inherits:
-
Object
- Object
- Tincan::PhoneNumber
- Defined in:
- lib/tincan/phone_number.rb
Constant Summary collapse
- REDIS_KEY_PREFIX =
'phone-number_'
- CODE_REDIS_KEY_PREFIX =
'phone-number-code'
- EXPIRATION =
one day of in-cache storage
86400
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#e164 ⇒ Object
readonly
Returns the value of attribute e164.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#local_format ⇒ Object
readonly
Returns the value of attribute local_format.
-
#verified_at ⇒ Object
readonly
Returns the value of attribute verified_at.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(hash) ⇒ PhoneNumber
constructor
A new instance of PhoneNumber.
- #save ⇒ Object
- #to_json ⇒ Object
- #verified? ⇒ Boolean
- #verify! ⇒ Object
Constructor Details
#initialize(hash) ⇒ PhoneNumber
Returns a new instance of PhoneNumber.
52 53 54 55 56 57 58 |
# File 'lib/tincan/phone_number.rb', line 52 def initialize(hash) %w{id e164 country_code local_format}.each do |key| instance_variable_set(:"@#{key}", hash[key]) end @verified_at = Time.at(hash['verified_at']) if hash['verified_at'] end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
11 12 13 |
# File 'lib/tincan/phone_number.rb', line 11 def code @code end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
10 11 12 |
# File 'lib/tincan/phone_number.rb', line 10 def country_code @country_code end |
#e164 ⇒ Object (readonly)
Returns the value of attribute e164.
10 11 12 |
# File 'lib/tincan/phone_number.rb', line 10 def e164 @e164 end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/tincan/phone_number.rb', line 10 def id @id end |
#local_format ⇒ Object (readonly)
Returns the value of attribute local_format.
10 11 12 |
# File 'lib/tincan/phone_number.rb', line 10 def local_format @local_format end |
#verified_at ⇒ Object (readonly)
Returns the value of attribute verified_at.
10 11 12 |
# File 'lib/tincan/phone_number.rb', line 10 def verified_at @verified_at end |
Class Method Details
.create!(e164) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tincan/phone_number.rb', line 14 def create!(e164) # TODO: Make sure there aren't collisions parsed = Fakie.parse(e164) phone = new({ 'id' => Utils.generate_code(32), 'e164' => parsed.e164, 'country_code' => parsed.region_code, 'local_format' => parsed.local_format }) phone.save # Store code phone.code = Utils.generate_code(8) key = "#{CODE_REDIS_KEY_PREFIX}#{phone.code}" Tincan.redis.set(key, phone.id) Tincan.redis.expire(key, EXPIRATION) phone end |
.find(id) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/tincan/phone_number.rb', line 34 def find(id) return nil unless id result = Tincan.redis.get("#{REDIS_KEY_PREFIX}#{id}") return nil unless result new(MultiJson.load(result)) end |
.verify_code!(code) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/tincan/phone_number.rb', line 43 def verify_code!(code) id = Tincan.redis.get("#{CODE_REDIS_KEY_PREFIX}#{code}") return nil unless phone = find(id) phone.verify! phone end |
Instance Method Details
#as_json ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/tincan/phone_number.rb', line 69 def as_json hash = {} # Default attributes %w{id e164 country_code local_format}.each do |key| hash[key] = instance_variable_get(:"@#{key}") end # Add `verified_at` as an integer hash['verified_at'] = verified_at.to_i if verified_at # Return the hash hash end |
#save ⇒ Object
88 89 90 91 92 |
# File 'lib/tincan/phone_number.rb', line 88 def save key = "#{REDIS_KEY_PREFIX}#{id}" Tincan.redis.set(key, to_json) Tincan.redis.expire(key, EXPIRATION) end |
#to_json ⇒ Object
84 85 86 |
# File 'lib/tincan/phone_number.rb', line 84 def to_json MultiJson.dump(as_json) end |
#verified? ⇒ Boolean
60 61 62 |
# File 'lib/tincan/phone_number.rb', line 60 def verified? verified_at && verified_at < Time.now.utc end |
#verify! ⇒ Object
64 65 66 67 |
# File 'lib/tincan/phone_number.rb', line 64 def verify! @verified_at = Time.now.utc save end |