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

end



21
22
23
24
# File 'lib/telstra/sms.rb', line 21

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

#send(sms_body, sms_to) ⇒ Object



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

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