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

parse_json, verify_parameters

Constructor Details

#initialize(parameters) ⇒ Number

:nodoc:



9
10
11
# File 'lib/elk/number.rb', line 9

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

Instance Attribute Details

#capabilitiesObject (readonly)

:nodoc:



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

def capabilities
  @capabilities
end

#clientObject (readonly)

:nodoc:



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

def client
  @client
end

#countryObject

:nodoc:



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

def country
  @country
end

#loaded_atObject (readonly)

:nodoc:



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

def loaded_at
  @loaded_at
end

#numberObject (readonly)

:nodoc:



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

def number
  @number
end

#number_idObject (readonly)

:nodoc:



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

def number_id
  @number_id
end

#sms_urlObject

:nodoc:



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

def sms_url
  @sms_url
end

#voice_start_urlObject

:nodoc:



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

def voice_start_url
  @voice_start_url
end

Class Method Details

.all(parameters = {}) ⇒ Object

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

Optional parameters

  • :client - Elk::Client instance



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/elk/number.rb', line 93

def all(parameters = {})
  client = parameters.fetch(:client) { Elk.client }

  response = client.get('/Numbers')

  numbers = Elk::Util.parse_json(response.body).fetch(:data)
  numbers.map do |number|
    number[:client] = client
    self.new(number)
  end
end

.allocate(parameters) ⇒ Object

Allocates a phone number

  • Required parameters: :country

  • Optional parameters: :sms_url, :voice_start_url, :client



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/elk/number.rb', line 73

def allocate(parameters)
  verify_parameters(parameters, [:country])

  client = parameters.fetch(:client) { Elk.client }

  allowed_arguments = [:country, :sms_url, :voice_start_url]
  arguments = parameters.dup.select do |key, _|
    allowed_arguments.include?(key)
  end

  response = client.post('/Numbers', arguments)
  self.new(Elk::Util.parse_json(response.body))
end

Instance Method Details

#deallocate!Object

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



60
61
62
63
64
# File 'lib/elk/number.rb', line 60

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

#reloadObject

Reloads a number from the API server



38
39
40
41
42
# File 'lib/elk/number.rb', line 38

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

#saveObject

Updates or allocates a number



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/elk/number.rb', line 45

def save
  attributes = {
    sms_url:     self.sms_url,
    voice_start: self.voice_start_url
  }

  # If new URL, send country, otherwise not
  unless self.number_id
    attributes[:country] = self.country
  end
  response = @client.post("/Numbers/#{self.number_id}", attributes)
  response.code == 200
end

#set_paramaters(parameters) ⇒ Object

:nodoc:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/elk/number.rb', line 13

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 = Array(parameters[:capabilities]).map(&:to_sym)
  @loaded_at    = Time.now
  @client       = parameters.fetch(:client) { Elk.client }
end

#statusObject

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/elk/number.rb', line 26

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