Skip to content

JNUC2019 Lab Session F Fetching Objects

Chris Lasell edited this page Oct 31, 2019 · 8 revisions

Hands On, Real-time Classic API Using ruby-jss

(Home)

Lab Session Contents - 5 - Fetching Objects

Previous           TOC           Next


  • Let's get a report of all network segments with specified distribution points

  • Unfortunately, the summary-list data from NetworkSegment.all only includes their name, id, and start/end address

pp JSS::NetworkSegment.all ;0
# [Array of Hashes]
# => 0

Fetching all network segments

  • For any other info about a network segment, we must fetch the full object from the API and look at it

  • This is slower, but gives us far more info to work with than the summary-lists

JSS::NetworkSegment.all_ids.each do |ns_id|
  net_seg = JSS::NetworkSegment.fetch id: ns_id
  next unless net_seg.distribution_point
  puts "Name: '#{net_seg.name}'\n  Dist. Point: '#{net_seg.distribution_point}'"
  sleep (rand + 1)
end ;0
# [lines of output]
# => 0
  • Here we're using each to loop thru the ids of all network segments

  • Each time thru, we use the id to fetch the full network segment

    • We store it in the variable net_seg
  • net_seg contains an object, an 'instance' of the class JSS::NetworkSegment

    • Instances of classes have their own methods, separate from the methods of the class itself
  • NetworkSegment objects have a distribution_point method that returns the name of the distribution point, or nil if none is assigned

  • So the next line skips those that have no distribution point set

  • Then we print out two lines with the network segment name and the distribution point

    • As with echo -n in bash, inside double-quotes, \n means a return character
  • And finally, we pause for some random amount of time between zero and two seconds before going to the next

    • Just for this lab, because otherwise we might swamp the API server
      • Which is a special, public API that has more limits than a private one

Previous           TOC           Next

Clone this wiki locally