class Tyntec::REST::Client

Rest client for communication with api endpoints Could be configured by api key and logger or use global configuration

Example: client = Tyntec::REST::Client.new('api_key') client.messaging.sms.send('+700000000', 'Message', 'Sender name')

Constants

TYNTEC_API_URI

Attributes

api_key[RW]

Public Class Methods

new(api_key = nil, logger = nil) click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 16
def initialize(api_key = nil, logger = nil)
  @api_key = api_key || Tyntec.api_key
  @logger = logger || Tyntec.logger
end

Public Instance Methods

messaging() click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 21
def messaging
  @messaging ||= Messaging.new self
end
request(method, uri, params = {}) click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 25
def request(method, uri, params = {})
  raise ClientError 'Tyntec client not configured' unless configured?

  target_uri = absolute_url(uri)

  begin
    result = send_request(method, target_uri, params)
  rescue RestClient::ExceptionWithResponse => e
    @logger&.error(e)
    raise RequestError, e
  end

  JSON.parse(result)
end

Protected Instance Methods

absolute_url(uri) click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 46
def absolute_url(uri)
  build_uri(TYNTEC_API_URI, uri)
end
configured?() click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 42
def configured?
  !api_key.nil?
end
get(uri, params) click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 65
def get(uri, params)
  RestClient.get(uri, params: params, headers: headers)
end
headers() click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 69
def headers
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'apikey' => @api_key
  }
end
post(uri, params) click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 61
def post(uri, params)
  RestClient.post(uri, params.to_json, headers)
end
send_request(method, uri, params) click to toggle source
# File lib/tyntec-ruby/rest/client.rb, line 50
def send_request(method, uri, params)
  case method.to_sym
  when :post
    post(uri, params)
  when :get
    get(uri, params)
  else
    raise ClientError, "Unknown method #{method}"
  end
end