Reputation: 1352
I do a mysql query in ruby, and it gives me a string like that:
O:8:"stdClass":4:{s:7:"updates";a:1:{i:0;O:8:"stdClass":10:{s:8:"response";s:6:"latest";s:8:"download";s:65:"https://downloads.wordpress.org/release/fr_FR/wordpress-4.2.4.zip";s:6:"locale";s:5:"fr_FR";s:8:"packages";O:8:"stdClass":5:{s:4:"full";s:65:"https://downloads.wordpress.org/release/fr_FR/wordpress-4.2.4.zip";s:10:"no_content";b:0;s:11:"new_bundled";b:0;s:7:"partial";b:0;s:8:"rollback";b:0;}s:7:"current";s:5:"4.2.4";s:7:"version";s:5:"4.2.4";s:11:"php_version";s:5:"5.2.4";s:13:"mysql_version";s:3:"5.0";s:11:"new_bundled";s:3:"4.1";s:15:"partial_version";s:0:"";}}s:12:"last_checked";i:1439805713;s:15:"version_checked";s:5:"4.2.4";s:12:"translations";a:0:{}}
I need to parse this part: s:6:"latest" to know if it's the latest version or an upgrade is available.
Which method could I use to do it in ruby? I just started this language and it's my first object oriented language, I only do C habitually.
Upvotes: 2
Views: 95
Reputation: 27543
This is string serialized with PHP. There is no simple, and stable way to parse that in Ruby. Regular expressions will get you only so far and will be fragile, since the serialized string is not regular.
There is, however, a gem php-serialize that allows unserialize and serialize in the PHP-way from Ruby. If you look into the gem, you'll see how complex the parser really is.
With that gem you could do:
require 'php-serialize'
string = 'O:8:"stdClass":4:{s:7:"updates";a:1:{i:0;O:8:"stdClass":10:{s:8:"response";s:6:"latest";s:8:"download";s:65:"https://downloads.wordpress.org/release/fr_FR/wordpress-4.2.4.zip";s:6:"locale";s:5:"fr_FR";s:8:"packages";O:8:"stdClass":5:{s:4:"full";s:65:"https://downloads.wordpress.org/release/fr_FR/wordpress-4.2.4.zip";s:10:"no_content";b:0;s:11:"new_bundled";b:0;s:7:"partial";b:0;s:8:"rollback";b:0;}s:7:"current";s:5:"4.2.4";s:7:"version";s:5:"4.2.4";s:11:"php_version";s:5:"5.2.4";s:13:"mysql_version";s:3:"5.0";s:11:"new_bundled";s:3:"4.1";s:15:"partial_version";s:0:"";}}s:12:"last_checked";i:1439805713;s:15:"version_checked";s:5:"4.2.4";s:12:"translations";a:0:{}}'
wp_settings = PHP.unserialize(string)
puts wp_settings.updates.inspect
Upvotes: 8