class MessagebusSDK::MessagebusBase

Constants

DEFAULT
DEFAULT_API_ENDPOINT
HEADER_SESSION_KEY
HTTP_DELETE
HTTP_GET
HTTP_POST
HTTP_PUT
HTTP_READ_TIMEOUT
MAX_TEMPLATE_MESSAGES
SCOPE_ALL
TRUE_VALUE

Public Class Methods

new(api_key, api_endpoint = DEFAULT_API_ENDPOINT) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 36
def initialize(api_key, api_endpoint = DEFAULT_API_ENDPOINT)
  @api_endpoint = api_endpoint
  @api_key = api_key
  @http = nil
  @http_read_timeout = HTTP_READ_TIMEOUT
  init_http_connection(@api_endpoint)

  @results = base_response_params
  @rest_http_errors = define_rest_http_errors
  @return_json = true
  @file_handle = nil
end

Public Instance Methods

api_version() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 49
def api_version
  make_api_request("/api/version")
end
cacert_info(cert_file) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 53
def cacert_info(cert_file)
  @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  if !File.exists?(cert_file)
    raise MessagebusSDK::MissingFileError.new("Unable to read file #{cert_file}")
  end
  @http.ca_file = File.join(cert_file)
end
format_iso_time(time) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 61
def format_iso_time(time)
  time.strftime("%Y-%m-%dT%H:%M:%SZ")
end
read_timeout(s) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 65
def read_timeout(s)
  if s > 0 and s < 600
    @http_read_timeout = s
  end
end

Private Instance Methods

base_message_params() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 265
def base_message_params
  {:toEmail => '',
   :fromEmail => '',
   :subject => '',
   :toName => '',
   :fromName => '',
   :plaintextBody => '',
   :htmlBody => '',
   :sessionKey => DEFAULT,
   :customHeaders => {} }
end
base_response_params() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 259
def base_response_params
  {:statusCode => 0,
   :statusMessage => "",
   :statusTime => "1970-01-01T00:00:00.000Z"}
end
check_response(response, symbolize_names=true) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 153
def check_response(response, symbolize_names=true)
  return response.body if !@return_json
  case response
    when Net::HTTPSuccess
      begin
        return JSON.parse(response.body, :symbolize_names => symbolize_names)
      rescue JSON::ParserError => e
        raise MessagebusSDK::RemoteServerError.new("JSON parsing error.  Response started with #{response.body.slice(0..9)}")
      end
    when Net::HTTPClientError, Net::HTTPServerError
      if (response.body && response.body.size > 0)
        result = begin
          JSON.parse(response.body, :symbolize_names => symbolize_names)
        rescue JSON::ParserError
          nil
        end
        raise MessagebusSDK::RemoteServerError.new("#{response.code.to_s}:#{rest_http_error_message(response)}")
      else
        raise MessagebusSDK::RemoteServerError.new("#{response.code.to_s}:#{rest_http_error_message(response)}")
      end
    else
      raise "Unexpected HTTP Response: #{response.class.name}"
  end
  raise "Could not determine response"
end
common_http_headers() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 83
def common_http_headers
  {'User-Agent' => MessagebusSDK::Info.get_user_agent, 'X-MessageBus-Key' => @api_key}
end
date_range(start_date, end_date) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 91
def date_range(start_date, end_date)
  date_range_str=""
  if (start_date!="")
    date_range_str+="startDate=#{start_date}"
  end
  if (end_date!="")
    if (date_range_str!="")
      date_range_str+="&"
    end
    date_range_str+="endDate=#{end_date}"
  end
  date_range_str
end
date_str_for_time_range(days_ago) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 112
def date_str_for_time_range(days_ago)
  format_iso_time(Time.now.utc - (days_ago*86400))
end
define_rest_http_errors() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 238
def define_rest_http_errors
  {
    "400" => "Invalid Request",
    "401" => "Unauthorized-Missing API Key",
    "403" => "Unauthorized-Invalid API Key",
    "404" => "Incorrect URL",
    "405" => "Method not allowed",
    "406" => "Format not acceptable",
    "408" => "Request Timeout",
    "409" => "Conflict",
    "410" => "Object missing or deleted",
    "413" => "Too many messages in request",
    "415" => "POST JSON data invalid",
    "422" => "Unprocessable Entity",
    "500" => "Internal Server Error",
    "501" => "Not Implemented",
    "503" => "Service Unavailable",
    "507" => "Insufficient Storage"
  }
end
feedback_query_args(start_date, end_date, use_send_time, scope) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 216
def feedback_query_args(start_date, end_date, use_send_time, scope)
  query_string_parts = ["useSendTime=#{use_send_time}", "scope=#{scope}"]
  date_range = "#{date_range(start_date, end_date)}"
  query_string_parts << date_range if date_range != ""
  "?" + query_string_parts.join("&")
end
init_http_connection(target_server) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 73
def init_http_connection(target_server)
  if (@http and @http.started?)
    @http.finish
  end
  @last_init_time = Time.now.utc
  endpoint_url = URI.parse(target_server)
  @http = Net::HTTP.start(endpoint_url.host, endpoint_url.port, {:use_ssl => true, :read_timeout => @http_read_timeout})
  @http
end
make_api_request(path, request_type=HTTP_GET, data='', return_json = true, file_name='') click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 120
def make_api_request(path, request_type=HTTP_GET, data='', return_json = true, file_name='')
  if (@last_init_time < Time.now.utc - 60)
    init_http_connection(@api_endpoint)
  end

  @return_json = return_json
  headers = common_http_headers
  case request_type
    when HTTP_GET
      if !@return_json && file_name != ''
        response_file = open(file_name, 'w')
        @http.request_get(path, headers) do |response|
          response.read_body do |segment|
            response_file.write(segment)
          end
        end
        response_file.close
        return true
      else
        response = @http.request_get(path, headers)
      end
    when HTTP_PUT
      headers = common_http_headers.merge(rest_post_headers)
      response = @http.request_put(path, data, headers)
    when HTTP_POST
      headers = common_http_headers.merge(rest_post_headers)
      response = @http.request_post(path, data, headers)
    when HTTP_DELETE
      response = @http.delete(path, headers)
  end
  check_response(response)
end
replace_channel_and_session_key(path, channel_key, session_key) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 204
def replace_channel_and_session_key(path, channel_key, session_key)
  replace_channel_key(replace_token_with_key(path, "%SESSION_KEY%", session_key), channel_key)
end
replace_channel_key(path, channel_key) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 200
def replace_channel_key(path, channel_key)
  replace_token_with_key(path, "%CHANNEL_KEY%", channel_key)
end
replace_report_key(path, report_key) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 212
def replace_report_key(path, report_key)
  replace_token_with_key(path, "%REPORT_KEY%", report_key)
end
replace_token_with_key(path, token, key) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 196
def replace_token_with_key(path, token, key)
  path.gsub(token, key)
end
replace_webhook_key(path, webhook_key) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 208
def replace_webhook_key(path, webhook_key)
  replace_token_with_key(path, "%WEBHOOK_KEY%", webhook_key)
end
rest_http_error?(status_code) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 179
def rest_http_error?(status_code)
  @rest_http_errors.key?(status_code)
end
rest_http_error_message(response) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 183
def rest_http_error_message(response)
  message = "Unknown Error Code"
  message = @rest_http_errors[response.code.to_s] if rest_http_error?(response.code.to_s)
  if (response.body.size > 0)
    values = JSON.parse(response.body)

    if (values['statusMessage'])
      message += " - " + values['statusMessage']
    end
  end
  message
end
rest_post_headers() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 87
def rest_post_headers
  {"Content-Type" => "application/json; charset=utf-8"}
end
set_date(date_string, days_ago) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 105
def set_date(date_string, days_ago)
  if date_string.length == 0
    return date_str_for_time_range(days_ago)
  end
  date_string
end
snake_case() click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 231
def snake_case
  return downcase if match(/\A[A-Z]+\z/)
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
  gsub(/([a-z])([A-Z])/, '\1_\2').
  downcase
end
underscore(camel_cased_word) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 223
def underscore(camel_cased_word)
   camel_cased_word.to_s.gsub(/::/, '/').
     gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
     gsub(/([a-z\d])([A-Z])/,'\1_\2').
     tr("-", "_").
     downcase
 end
well_formed_address?(address) click to toggle source
# File lib/messagebus-sdk/messagebus_base.rb, line 116
def well_formed_address?(address)
  !address.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i).nil?
end