Class: Tincan::PhoneNumber

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#codeObject

Returns the value of attribute code.



11
12
13
# File 'lib/tincan/phone_number.rb', line 11

def code
  @code
end

#country_codeObject (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

#e164Object (readonly)

Returns the value of attribute e164.



10
11
12
# File 'lib/tincan/phone_number.rb', line 10

def e164
  @e164
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/tincan/phone_number.rb', line 10

def id
  @id
end

#local_formatObject (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_atObject (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_jsonObject



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

#saveObject



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_jsonObject



84
85
86
# File 'lib/tincan/phone_number.rb', line 84

def to_json
  MultiJson.dump(as_json)
end

#verified?Boolean

Returns:

  • (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