Find usb properties/details using ruby-dbus and udisk

I am doing project in which i need to detect all available usb drives
and its info. Mainly i am interested in USB device (flash drive and
external hard disk), mount point, uuid, total size, available space,
label, and is the usb mounted or not. I thought ruby-dbus would be
better to go with udisk. So installed with out an error. However i am
unable to proceed further. I need to use this on linux. Here is the
sample code

#!/usr/bin/env ruby
require ‘rubygems’
require ‘dbus’
bus = DBus::SystemBus.instance
rb_service = bus.service(“org.freedesktop.UDisks”)
rb_udisk = rb_service.object("/org/freedesktop/UDisks")
rb_udisk.introspect
rb_udisk_iface = rb_udisk[“org.freedesktop.UDisks.Device”]
puts rb_udisk_iface.DeviceIsRemovable

However i get this error
undefined method `DeviceIsRemovable’ for nil:NilClass (NoMethodError)
which i don’t understand. Could some one help me on this?
Useful links:-
ruby-usb
https://github.com/mvidner/ruby-dbus/blob/master/doc/Tutorial.md
udisk
http://udisks.freedesktop.org/docs/1.0.5/Device.html#Device:DeviceFile
python and udisk
http://mindbending.org/en/python-and-udisks-part-5
http://stackoverflow.com/questions/5067005/python-udisks-enumerating-device-information

Any help is appreciated.

Am 28.03.2013 19:49, schrieb sundar sundar:

require ‘dbus’
bus = DBus::SystemBus.instance
rb_service = bus.service(“org.freedesktop.UDisks”)
rb_udisk = rb_service.object("/org/freedesktop/UDisks")
rb_udisk.introspect
rb_udisk_iface = rb_udisk[“org.freedesktop.UDisks.Device”]
puts rb_udisk_iface.DeviceIsRemovable

However i get this error
undefined method `DeviceIsRemovable’ for nil:NilClass (NoMethodError)
which i don’t understand. Could some one help me on this?

It means that rb_udisk_iface' is nil and you are trying to callnil.DeviceIsRemovable’ in your last line, which does not exist.

Seems rb_udisk[“org.freedesktop.UDisks.Device”] returns nil
for some reason. Can’t help you there, though.

Ok.Following code works with “EnumerateDevices”

#!/usr/bin/env ruby
require ‘rubygems’
require ‘dbus’
bus = DBus::SystemBus.instance
rb_service = bus.service(“org.freedesktop.UDisks”)
rb_udisk = rb_service.object("/org/freedesktop/UDisks")
rb_udisk.introspect
rb_udisk.default_iface = “org.freedesktop.UDisks.Device”
puts rb_udisk.EnumerateDevices

and the output is
/org/freedesktop/UDisks/devices/sr0
/org/freedesktop/UDisks/devices/sda
/org/freedesktop/UDisks/devices/sda1
/org/freedesktop/UDisks/devices/sda2
/org/freedesktop/UDisks/devices/sda3
/org/freedesktop/UDisks/devices/sdb
/org/freedesktop/UDisks/devices/sda5
/org/freedesktop/UDisks/devices/sda6
/org/freedesktop/UDisks/devices/sda7
/org/freedesktop/UDisks/devices/sda8
/org/freedesktop/UDisks/devices/sdb1

Howver if i provide other that listed in the following manual results
some error.

Ok. I am very much close to it.

#!/usr/bin/env ruby
require ‘rubygems’
require ‘dbus’
bus = DBus::SystemBus.instance
rb_service = bus.service(“org.freedesktop.UDisks”)
rb_udisk = rb_service.object("/org/freedesktop/UDisks")
rb_udisk.introspect
rb_udisk.default_iface = “org.freedesktop.UDisks.Device”
list_devices = rb_udisk.EnumerateDevices

Now i want to do for loop for available device in list_deviceswith the
following code

bus = DBus::SystemBus.instance
nm_service = bus[“org.freedesktop.UDisks”]
network_manager_object =
nm_service.object("/org/freedesktop/UDisks/devices/sdb1")
#"/org/freedesktop/UDisks/devices/sdb1 data from list_devices
network_manager_object.introspect
nm_iface = network_manager_object[“org.freedesktop.UDisks.Device”]

read a property

puts nm_iface[“DeviceMountPaths”]
puts nm_iface[“DeviceSize”]
puts nm_iface[“IdUuid”]
puts nm_iface[“DevicePresentationName”]

Both the above (different scripts) works properly. I need to connect it
together. Forgive me the code is messy.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs