Class: Elk::SMS

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

Overview

Used to send SMS through 46elks SMS-gateway

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

verify_parameters

Constructor Details

#initialize(parameters) ⇒ SMS

:nodoc:



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

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

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#directionObject (readonly)

Returns the value of attribute direction.



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

def direction
  @direction
end

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#loaded_atObject (readonly)

Returns the value of attribute loaded_at.



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

def loaded_at
  @loaded_at
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#message_idObject (readonly)

Returns the value of attribute message_id.



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

def message_id
  @message_id
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#toObject (readonly)

Returns the value of attribute to.



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

def to
  @to
end

Class Method Details

.allObject

Get outgoing and incomming messages. Limited by the API to 100 latest



68
69
70
71
# File 'lib/elk/sms.rb', line 68

def all
  response = Elk.get('/SMS')
  instantiate_multiple(Elk.parse_json(response.body)[:data])
end

.send(parameters) ⇒ Object

Send SMS Required parameters

  • :from - Either the one of the allocated numbers or arbitrary alphanumeric string of at most 11 characters

  • :to - Any phone number capable of receiving SMS. Multiple numbers can be given as Array or comma separated String

  • :message - Any UTF-8 text Splitting and joining multi-part SMS messages are automatically handled by the API

Optional parameters

  • :flash - if set to non-false value SMS is sent as a “Flash SMS”



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elk/sms.rb', line 42

def send(parameters)
  verify_parameters(parameters, [:from, :message, :to])

  parameters[:to] = Array(parameters[:to]).join(',')
  
  if parameters[:flash]
    parameters.delete(:flash)
    parameters[:flashsms] = 'yes'
  end

  # Warn if the from string will be capped by the sms gateway
  if parameters[:from] && parameters[:from].match(/^(\w{11,})$/)
    warn "SMS 'from' value #{parameters[:from]} will be capped at 11 chars"
  end

  response = Elk.post('/SMS', parameters)
  parsed_response = Elk.parse_json(response.body)
  
  if multiple_recipients?(parameters[:to])
    instantiate_multiple(parsed_response)
  else
    self.new(parsed_response)
  end
end

Instance Method Details

#reloadObject

Reloads a SMS from server



23
24
25
26
27
# File 'lib/elk/sms.rb', line 23

def reload
  response = Elk.get("/SMS/#{self.message_id}")
  self.set_parameters(Elk.parse_json(response.body))
  response.code == 200
end

#set_parameters(parameters) ⇒ Object

:nodoc:



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

def set_parameters(parameters) #:nodoc:
  @from       = parameters[:from]
  @to         = parameters[:to]
  @message    = parameters[:message]
  @message_id = parameters[:id]      
  @created_at = Time.parse(parameters[:created]) if parameters[:created]
  @loaded_at  = Time.now
  @direction  = parameters[:direction]
  @status     = parameters[:status]
end