Class: Telstra::SMS

Inherits:
Object
  • Object
show all
Defined in:
lib/telstra/sms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer_key, consumer_secret) ⇒ SMS

Initialize API Keys



11
12
13
14
# File 'lib/telstra/sms.rb', line 11

def initialize(consumer_key, consumer_secret)
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
end

Instance Attribute Details

#consumer_keyObject

Returns the value of attribute consumer_key.



8
9
10
# File 'lib/telstra/sms.rb', line 8

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



8
9
10
# File 'lib/telstra/sms.rb', line 8

def consumer_secret
  @consumer_secret
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/telstra/sms.rb', line 8

def token
  @token
end

Instance Method Details

#generate_tokenObject

OAuth 2.0 Token. This will expire every hour.



17
18
19
20
# File 'lib/telstra/sms.rb', line 17

def generate_token
  response = HTTParty.get("https://api.telstra.com/v1/oauth/token?client_id=#{@consumer_key}&client_secret=#{@consumer_secret}&grant_type=client_credentials&scope=SMS")
  @token = JSON.parse(response.body)['access_token'] 
end

#get_message_response(message_id) ⇒ Object



53
54
55
56
57
58
# File 'lib/telstra/sms.rb', line 53

def get_message_response(message_id)
  generate_token
  options = { headers: { "Authorization" => "Bearer #{@token}" } }
  repsonse = HTTParty.post("https://api.telstra.com/v1/sms/messages/#{message_id}/response")
  return response.body
end

#get_message_status(message_id) ⇒ Object

Get the status of a previously sent SMS message May return:

PEND -> The message is pending and has not yet been sent SENT -> Message has been send, but not delivered DELIVRD -> Message has been delivered READ -> The message has been read by the intended recipeitn

Note: Some responses are dependent on the phone network of the user. Obviously, more info can be grabbed from those on the Telstra network.



46
47
48
49
50
51
# File 'lib/telstra/sms.rb', line 46

def get_message_status(message_id)
  generate_token
  options = { headers: { "Authorization" => "Bearer #{@token}" } }
  repsonse = HTTParty.post("https://api.telstra.com/v1/sms/messages/#{message_id}")
  return response.body
end

#send_sms(sms_body, sms_to) ⇒ Object

Receipient number should be in the format of 04xxxxxxxx where x is a digit. Authorization header value should be in the format of “Bearer xxx” where xxx is the access token returned from a token request.



25
26
27
28
29
30
31
32
33
34
# File 'lib/telstra/sms.rb', line 25

def send_sms(sms_body, sms_to)
  generate_token
  options = { body: {
                body: sms_body,
                to: sms_to
              }.to_json,
              headers: { "Content-Type" => "application/json", "Authorization" => "Bearer #{@token}" }}
  response = HTTParty.post("https://api.telstra.com/v1/sms/messages", options)
  return response.body
end