class InfobipApi::InfobipApiClient
Public Class Methods
new(username, password, base_url=nil)
click to toggle source
# File lib/infobipapi/client.rb, line 17 def initialize(username, password, base_url=nil) @username = username @password = password if base_url @base_url = base_url else @base_url = 'https://api.infobip.com/' end if @base_url[-1, 1] != '/' @base_url += '/' end @infobipapi_authentication = nil login() end
Public Instance Methods
convert_from_json(classs, json, is_error)
click to toggle source
# File lib/infobipapi/client.rb, line 159 def convert_from_json(classs, json, is_error) Conversions.from_json(classs, json, is_error) end
execute_GET(url, params=nil)
click to toggle source
# File lib/infobipapi/client.rb, line 94 def execute_GET(url, params=nil) execute_request('GET', url, params) end
execute_POST(url, params=nil)
click to toggle source
# File lib/infobipapi/client.rb, line 98 def execute_POST(url, params=nil) execute_request('POST', url, params) end
execute_request(http_method, url, params)
click to toggle source
# File lib/infobipapi/client.rb, line 102 def execute_request(http_method, url, params) rest_url = get_rest_url(url) uri = URI(URI.encode(rest_url)) if Utils.empty(params) params = {} end if http_method == 'GET' request = Net::HTTP::Get.new("#{uri.request_uri}?#{urlencode(params)}") elsif http_method == 'POST' request = Net::HTTP::Post.new(uri.request_uri) #binding.pry if params.has_key?(:binary) request.body = params.to_json end http = Net::HTTP.new(uri.host, uri.port) use_ssl = rest_url.start_with? "https" if use_ssl http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end prepare_headers(request) response = http.request(request) #puts "" #puts "response: #{response.inspect}" #puts "body: #{response.body}" #puts "" return is_success(response), response.body end
fill_infobipapi_authentication(json, is_success)
click to toggle source
# File lib/infobipapi/client.rb, line 149 def fill_infobipapi_authentication(json, is_success) @infobipapi_authentication = convert_from_json(AuthenticationAnswer, json, !is_success) @infobipapi_authentication = nil if @infobipapi_authentication.token.nil? \ || @infobipapi_authentication.token.length == 0 @infobipapi_authentication end
get_or_create_client_correlator(client_correlator=nil)
click to toggle source
# File lib/infobipapi/client.rb, line 50 def get_or_create_client_correlator(client_correlator=nil) if client_correlator return client_correlator end return Utils.get_random_alphanumeric_string() end
get_rest_url(rest_path)
click to toggle source
# File lib/infobipapi/client.rb, line 137 def get_rest_url(rest_path) if not rest_path return @base_url end if rest_path[0, 1] == '/' return @base_url + rest_path[1, rest_path.length] end @base_url + rest_path end
is_success(response)
click to toggle source
# File lib/infobipapi/client.rb, line 69 def is_success(response) http_code = response.code.to_i is_success = 200 <= http_code && http_code < 300 is_success end
login()
click to toggle source
# File lib/infobipapi/client.rb, line 36 def login() params = { 'username' => @username, 'password' => @password, } is_success, result = execute_POST('auth/1/session', params) filled = fill_infobipapi_authentication(result, is_success) #puts "" #puts "login: #{filled.inspect}" return filled end
prepare_headers(request)
click to toggle source
# File lib/infobipapi/client.rb, line 58 def prepare_headers(request) request["User-Agent"] = "InfobipApi-#{InfobipApi::VERSION}" request["Content-Type"] = "application/json" if @infobipapi_authentication and @infobipapi_authentication.token request['Authorization'] = "IBSSO #{@infobipapi_authentication.token}" else auth_string = Base64.encode64("#{@username}:#{@password}").strip request['Authorization'] = "Basic #{auth_string}" end end
urlencode(params)
click to toggle source
# File lib/infobipapi/client.rb, line 76 def urlencode(params) if Utils.empty(params) return '' end if params.instance_of? String return URI.encode(params) end result = '' params.each_key do |key| if ! Utils.empty(result) result += '&' end result += URI.encode(key.to_s) + '=' + URI.encode(params[key].to_s) end return result end