class Rumour::Client

Attributes

access_token[RW]

Public Class Methods

new(access_token = nil) click to toggle source
# File lib/rumour-ruby/client.rb, line 13
def initialize(access_token = nil)
  @access_token = access_token || Rumour.configuration.access_token
  raise Rumour::Errors::AuthenticationError.new('Missing access token') if @access_token.nil?
end

Public Instance Methods

send_push_notification(recipients, options= {}) click to toggle source
# File lib/rumour-ruby/client.rb, line 23
def send_push_notification(recipients, options= {})
  recipients = intercept_pn_recipients unless [intercept_pn_recipients].flatten.compact.empty?
  post('/push_notifications', push_notification: { recipients: [recipients].flatten }.merge(options))
end
send_text_message(sender, recipient, body) click to toggle source
# File lib/rumour-ruby/client.rb, line 18
def send_text_message(sender, recipient, body)
  recipient = intercept_tm_recipient || recipient
  post('/text_messages', text_message: { from: sender, recipient: recipient, body: body })
end

Private Instance Methods

auth_header() click to toggle source
# File lib/rumour-ruby/client.rb, line 35
def auth_header
  { 'Authorization' => "Bearer #{credentials}" }
end
credentials() click to toggle source
# File lib/rumour-ruby/client.rb, line 39
def credentials
  Base64.urlsafe_encode64(@access_token)
end
evaluate_response(response) click to toggle source
# File lib/rumour-ruby/client.rb, line 43
def evaluate_response(response)
  response_body = JSON.parse(response.body)
  
  case response.code
  when 201
    response_body
  when 400
    raise Rumour::Errors::RequestError.new response_body['message']
  when 401
    raise Rumour::Errors::AuthenticationError.new response_body['message']
  when 500
    raise Rumour::Errors::AuthenticationError.new response_body['message']
  end
end
intercept_pn_recipients() click to toggle source
# File lib/rumour-ruby/client.rb, line 62
def intercept_pn_recipients
  Rumour.configuration.intercept_push_notification_recipients
end
intercept_tm_recipient() click to toggle source
# File lib/rumour-ruby/client.rb, line 58
def intercept_tm_recipient
  Rumour.configuration.intercept_text_message_recipient
end
post(url, params = {}, headers = {}) click to toggle source
# File lib/rumour-ruby/client.rb, line 30
def post(url, params = {}, headers = {})
  response = self.class.post(url, query: params, headers: headers.merge(auth_header))
  evaluate_response(response)
end