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

parse_json, verify_parameters

Constructor Details

#initialize(parameters) ⇒ SMS

:nodoc:



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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#directionObject (readonly)

Returns the value of attribute direction.



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

def direction
  @direction
end

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#loaded_atObject (readonly)

Returns the value of attribute loaded_at.



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

def loaded_at
  @loaded_at
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#message_idObject (readonly)

Returns the value of attribute message_id.



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

def message_id
  @message_id
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#toObject (readonly)

Returns the value of attribute to.



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

def to
  @to
end

Class Method Details

.all(parameters = {}) ⇒ Object

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

Optional parameters

  • :client - Elk::Client instance



85
86
87
88
89
90
# File 'lib/elk/sms.rb', line 85

def all(parameters = {})
  client = parameters.fetch(:client) { Elk.client }
  response = client.get("/SMS")
  messages = Elk::Util.parse_json(response.body).fetch(:data).each { |m| m[:client] = client }
  instantiate_multiple(messages)
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”

  • :flashsms - alias of :flash

  • :client - ‘Elk::Client` instance

  • :whendelivered - Callback URL that will receive a POST after delivery



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elk/sms.rb', line 48

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

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

  arguments = {}
  arguments[:from]     = parameters.fetch(:from)
  arguments[:to]       = Array(parameters.fetch(:to)).join(",")
  arguments[:message]  = parameters.fetch(:message)
  
  if parameters.values_at(:flash, :flashsms).any?
    arguments[:flashsms] = "yes"
  end

  if parameters.key?(:whendelivered)
    arguments[:whendelivered] = parameters.fetch(:whendelivered)
  end

  check_sender_limit(arguments[:from])

  response = client.post("/SMS", arguments)
  parsed_response = Elk::Util.parse_json(response.body)

  if multiple_recipients?(arguments[:to])
    parsed_response.each { |m| m[:client] = client }
    instantiate_multiple(parsed_response)
  else
    parsed_response[:client] = client
    self.new(parsed_response)
  end
end

Instance Method Details

#reloadObject

Reloads a SMS from server



26
27
28
29
30
# File 'lib/elk/sms.rb', line 26

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

#set_parameters(parameters) ⇒ Object

:nodoc:



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

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]
  @client     = parameters.fetch(:client) { Elk.client }
end