Class: Elk::Number

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/elk/number.rb

Overview

Allocate and manage numbers used for SMS/MMS/Voice

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

verify_parameters

Constructor Details

#initialize(parameters) ⇒ Number

:nodoc:



7
8
9
# File 'lib/elk/number.rb', line 7

def initialize(parameters) #:nodoc:
  set_paramaters(parameters)
end

Instance Attribute Details

#capabilitiesObject (readonly)

:nodoc:



4
5
6
# File 'lib/elk/number.rb', line 4

def capabilities
  @capabilities
end

#countryObject

:nodoc:



5
6
7
# File 'lib/elk/number.rb', line 5

def country
  @country
end

#loaded_atObject (readonly)

:nodoc:



4
5
6
# File 'lib/elk/number.rb', line 4

def loaded_at
  @loaded_at
end

#numberObject (readonly)

:nodoc:



4
5
6
# File 'lib/elk/number.rb', line 4

def number
  @number
end

#number_idObject (readonly)

:nodoc:



4
5
6
# File 'lib/elk/number.rb', line 4

def number_id
  @number_id
end

#sms_urlObject

:nodoc:



5
6
7
# File 'lib/elk/number.rb', line 5

def sms_url
  @sms_url
end

#voice_start_urlObject

:nodoc:



5
6
7
# File 'lib/elk/number.rb', line 5

def voice_start_url
  @voice_start_url
end

Class Method Details

.allObject

Returns all Elk::Numbers, regardless of status (allocated/deallocated)



73
74
75
76
77
78
79
# File 'lib/elk/number.rb', line 73

def all
  response = Elk.get('/Numbers')

  Elk.parse_json(response.body)[:data].collect do |n|
    self.new(n)
  end
end

.allocate(parameters) ⇒ Object

Allocates a phone number

  • Required parameters: :country

  • Optional parameters: :sms_url, :voice_start_url



66
67
68
69
70
# File 'lib/elk/number.rb', line 66

def allocate(parameters)
  verify_parameters(parameters, [:country])
  response = Elk.post('/Numbers', parameters)
  self.new(Elk.parse_json(response.body))
end

Instance Method Details

#deallocate!Object

Deallocates a number, once allocated, a number cannot be used again, ever!



53
54
55
56
57
# File 'lib/elk/number.rb', line 53

def deallocate!
  response = Elk.post("/Numbers/#{self.number_id}", {:active => 'no'})
  self.set_paramaters(Elk.parse_json(response.body))
  response.code == 200
end

#reloadObject

Reloads a number from the API server



35
36
37
38
39
# File 'lib/elk/number.rb', line 35

def reload
  response = Elk.get("/Numbers/#{self.number_id}")
  self.set_paramaters(Elk.parse_json(response.body))
  response.code == 200
end

#saveObject

Updates or allocates a number



42
43
44
45
46
47
48
49
50
# File 'lib/elk/number.rb', line 42

def save
  attributes = {:sms_url => self.sms_url, :voice_start => self.voice_start_url}
  # If new URL, send country, otherwise not
  if !self.number_id
    attributes[:country] = self.country
  end
  response = Elk.post("/Numbers/#{self.number_id}", attributes)
  response.code == 200
end

#set_paramaters(parameters) ⇒ Object

:nodoc:



11
12
13
14
15
16
17
18
19
20
# File 'lib/elk/number.rb', line 11

def set_paramaters(parameters) #:nodoc:
  @country      = parameters[:country]
  @sms_url      = parameters[:sms_url]
  @voice_start_url = parameters[:voice_start_url]
  @status       = parameters[:active]
  @number_id    = parameters[:id]
  @number       = parameters[:number]
  @capabilities = parameters[:capabilities].collect {|c| c.to_sym }
  @loaded_at    = Time.now
end

#statusObject

Status of a number, if it’s :active or :deallocated



23
24
25
26
27
28
29
30
31
32
# File 'lib/elk/number.rb', line 23

def status
  case @status
  when 'yes'
    :active
  when 'no'
    :deallocated
  else
    nil
  end
end