Class: Elk::Number
Overview
Allocate and manage numbers used for SMS/MMS/Voice
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
:nodoc:.
-
#client ⇒ Object
readonly
:nodoc:.
-
#country ⇒ Object
:nodoc:.
-
#loaded_at ⇒ Object
readonly
:nodoc:.
-
#number ⇒ Object
readonly
:nodoc:.
-
#number_id ⇒ Object
readonly
:nodoc:.
-
#sms_url ⇒ Object
:nodoc:.
-
#voice_start_url ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.all(parameters = {}) ⇒ Object
Returns all Elk::Numbers, regardless of status (allocated/deallocated).
-
.allocate(parameters) ⇒ Object
Allocates a phone number.
Instance Method Summary collapse
-
#deallocate! ⇒ Object
Deallocates a number, once allocated, a number cannot be used again, ever!.
-
#initialize(parameters) ⇒ Number
constructor
:nodoc:.
-
#reload ⇒ Object
Reloads a number from the API server.
-
#save ⇒ Object
Updates or allocates a number.
-
#set_paramaters(parameters) ⇒ Object
:nodoc:.
-
#status ⇒ Object
Status of a number, if it’s :active or :deallocated.
Methods included from Util
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
#capabilities ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/elk/number.rb', line 6 def capabilities @capabilities end |
#client ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/elk/number.rb', line 6 def client @client end |
#country ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/elk/number.rb', line 7 def country @country end |
#loaded_at ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/elk/number.rb', line 6 def loaded_at @loaded_at end |
#number ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/elk/number.rb', line 6 def number @number end |
#number_id ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/elk/number.rb', line 6 def number_id @number_id end |
#sms_url ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/elk/number.rb', line 7 def sms_url @sms_url end |
#voice_start_url ⇒ Object
: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 |
#reload ⇒ Object
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 |
#save ⇒ Object
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 |
#status ⇒ Object
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 |