Refactor encoding method and using rubocop