Class: Oxd::OxdConnector
- Inherits:
-
Object
- Object
- Oxd::OxdConnector
- Defined in:
- lib/oxd/oxd_connector.rb
Overview
A class which takes care of the socket communication with oxD Server.
Direct Known Subclasses
Instance Method Summary collapse
-
#getData ⇒ Array
combines command and command parameters for socket request.
-
#getResponseData ⇒ Mixed
extracts ‘data’ parameter from @response_object.
-
#initialize ⇒ OxdConnector
constructor
class constructor.
-
#is_json?(string_to_validate) ⇒ Boolean
checks whether the passed string is in JSON format or not.
-
#logger(args = {}) ⇒ Object
Logs server response and errors to log file.
-
#oxd_socket_request(request, char_count = 8192) ⇒ Object
method to communicate with the oxD server.
-
#request ⇒ JSON
method to send commands to the oxD server and to recieve the response via #oxd_socket_request.
-
#validate_command ⇒ Object
Checks the validity of command that is to be passed to oxd-server.
Constructor Details
#initialize ⇒ OxdConnector
class constructor
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/oxd/oxd_connector.rb', line 12 def initialize @command @response_json @response_object @data = Hash.new @params = Hash.new @response_data = Hash.new @configuration = Oxd.config logger(:log_msg => "Problem with json data : authorization_redirect_uri can't be blank") if @configuration..empty? logger(:log_msg => "#{@configuration.oxd_host_ip} is not a valid IP address") if (IPAddr.new(@configuration.oxd_host_ip) rescue nil).nil? logger(:log_msg => "#{@configuration.oxd_host_port} is not a valid port for socket. Port must be integer and between from 0 to 65535") if (!@configuration.oxd_host_port.is_a?(Integer) || (@configuration.oxd_host_port < 0 && @configuration.oxd_host_port > 65535)) end |
Instance Method Details
#getData ⇒ Array
combines command and command parameters for socket request
104 105 106 107 |
# File 'lib/oxd/oxd_connector.rb', line 104 def getData @data = {'command' => @command, 'params' => @params} return @data end |
#getResponseData ⇒ Mixed
extracts ‘data’ parameter from @response_object
93 94 95 96 97 98 99 100 |
# File 'lib/oxd/oxd_connector.rb', line 93 def getResponseData if (!@response_object) @response_data = 'Data is empty'; else @response_data = @response_object['data'] end return @response_data end |
#is_json?(string_to_validate) ⇒ Boolean
checks whether the passed string is in JSON format or not
112 113 114 115 116 117 118 |
# File 'lib/oxd/oxd_connector.rb', line 112 def is_json? (string_to_validate) begin !!JSON.parse(string_to_validate) rescue false end end |
#logger(args = {}) ⇒ Object
Logs server response and errors to log file
124 125 126 127 128 129 130 131 |
# File 'lib/oxd/oxd_connector.rb', line 124 def logger(args={}) # Initialize Log file # Location : app_root/log/oxd-ruby.log @logger ||= Logger.new("log/oxd-ruby.log") @logger.info(args[:log_msg]) raise (args[:error] || args[:log_msg]) if args[:error] != "" end |
#oxd_socket_request(request, char_count = 8192) ⇒ Object
method to communicate with the oxD server
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/oxd/oxd_connector.rb', line 37 def oxd_socket_request(request, char_count = 8192) host = @configuration.oxd_host_ip # The web server port = @configuration.oxd_host_port # Default HTTP port if(!socket = TCPSocket.new(host, port) ) # Connect to Oxd server logger(:log_msg => "Socket Error : Couldn't connect to socket ") else logger(:log_msg => "Client: socket::socket_connect connected : #{request}", :error => "") end socket.print(request) # Send request response = socket.recv(char_count) # Read response if(response) logger(:log_msg => "Client: oxd_socket_response: #{response}", :error => "") else logger(:log_msg => "Client: oxd_socket_response : Error socket reading process.") end # close connection if(socket.close) logger(:log_msg => "Client: oxd_socket_connection : disconnected.", :error => "") end return response end |
#request ⇒ JSON
method to send commands to the oxD server and to recieve the response via #oxd_socket_request
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/oxd/oxd_connector.rb', line 63 def request validate_command jsondata = getData.to_json if(!is_json? (jsondata)) logger(:log_msg => "Sending parameters must be JSON. Exiting process.") end length = jsondata.length if( length <= 0 ) logger(:log_msg => "JSON data length must be more than zero. Exiting process.") else length = length <= 999 ? sprintf('0%d', length) : length end @response_json = oxd_socket_request((length + jsondata).encode("UTF-8")) @response_json.sub!(@response_json[0..3], "") if (@response_json) response = JSON.parse(@response_json) if (response['status'] == 'error') logger(:log_msg => "OxD Server Error : #{response['data']['error_description']}") elsif (response['status'] == 'ok') @response_object = JSON.parse(@response_json) end else logger(:log_msg => "Response is empty. Exiting process.") end return @response_object end |
#validate_command ⇒ Object
Checks the validity of command that is to be passed to oxd-server
26 27 28 29 30 31 |
# File 'lib/oxd/oxd_connector.rb', line 26 def validate_command command_types = ['get_authorization_url','update_site_registration', 'get_tokens_by_code','get_user_info', 'register_site', 'get_logout_uri','get_authorization_code'] if (!command_types.include?(@command)) logger(:log_msg => "Command: #{@command} does not exist! Exiting process.") end end |