TDD and JavaScript
I felt that I had to do this for my own good.:) To do TDD, the easiest way is to have nodeJS installed and up and running. What I shortly […]
I felt that I had to do this for my own good.:) To do TDD, the easiest way is to have nodeJS installed and up and running. What I shortly […]
I released this custom WebSocketChatServer gem earlier this month and thought that it may be a good idea to wrap the server’s client in a JS class just to make […]
Relational database management system (RDBMS) are more than creating tables and execute SQL-queries against them. PostgreSQL is one of my favourite RDBMS and if you happened to use it in […]
What I did to perform the task is that i used RNCryptor in my IOS application.
1 2 3 4 5 6 7 8 9 10 11 12 |
+(NSString*) encryptString:(NSString*) stringToEncrypt withKey:(NSString*) encryptionKey{ NSData *data = [stringToEncrypt dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; NSData *encryptedData = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:encryptionKey error:&error]; return [encryptedData base64EncodedStringWithOptions:0]; } |
And I engaged this gem to do the decryption in Rails.
1 2 3 4 5 6 7 8 9 |
require 'ruby_rncryptor' require "base64" def decrypt_string(encrypted_string, secret_key) encrypted = Base64.decode64(encrypted_string) RubyRNCryptor.decrypt(encrypted, secret_key) end |
Can not be easier than this…
1 2 3 4 5 6 7 8 9 10 |
require 'uri' parsed_uri = URI.parse('http://localhost:3000/welcome/index') puts parsed_data.scheme # => http puts parsed_data.host # => localhost puts parsed_data.port # => 3000 puts parsed_data.path # => /welcome/index |
If you do not know how a CSRF-attack work, read here This is a simple class for taking care of generating and validating the tokens.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php /** * A Session token creator class */ class SessionToken { /** * Holds the session token name * @var string */ private $_tokenName; /** * Class constructor * @param string $tokenName The name of the token to be stored in the session */ public function __construct($tokenName = ""){ if(empty($tokenName)) { throw new \Exception("The token name is empty."); } if (session_id() == "") { throw new \Exception("The session is not started."); } $this->_tokenName = $tokenName; } /** * Creates a new token by storing it in the session * @return string the newly created token */ public function createToken() { $tokenValue = substr(md5(rand()), 0, 7); $_SESSION["$this->_tokenName"] = $tokenValue; return $tokenValue; } /** * Checks if the token is valid or not * @param string $tokenValue The token validate * @return boolean true if the token is valid, otherwise false */ public function isValidToken($tokenValue = "") { if(!isset($_SESSION["$this->_tokenName"])) return false; $sessionTokenValue = $_SESSION["$this->_tokenName"]; if($sessionTokenValue != $tokenValue) return false; return true; } } |
Let’s say that you […]
Imagine you have an User < ActiveRecord::Base object and you want to apply filtering implementation to it. The filtering will be based on certain criteria like get me all the […]
The observer design pattern is best demonstrated using the broadcaster listener example. A broadcaster in this example is an object having implementation for attaching listeners to itself. When the broadcaster […]
To demonstrate a yielding method, I created A user-management class containing registered_users method. The method will return an array of the registered user’s hash if no block is given. Otherwise […]
When should I write modules in a Ruby project? The short answer is whenever you have have an implementation that is “something-able”. Abstract classes in C++, Java or Protocols in […]