Here's a series of three new examples, building up from one another, showing the reading of a JSON feed from a remote web site in Ruby, and the analysis and presentation of that data.
The first example -
[here] - shows the loading of extra modules to handle reading from the web and the JSON format, and the use of the methods loaded fro those modules. Keycode is:
require 'json'
require 'open-uri'
source = URI.parse("http://www.wellho.net/services/_.json")
fh = source.open
stuff = fh.read
things = JSON.parse(stuff)
and the example goes on to define and call methods to descend into the resultant object and display it.
The second example -
[here] - extends that initial example by providing an insetting
class to give a flexibility of display.
And the third example -
[here] - implements a second insetting class which outputs the structures loaded from JSON in the form of Ruby source code assignment statements or as a tree depending on a command line option. So the results will look like
WomanWithCat:kingston grahamellis$ ./tj3.rb -c | head
timedat = '1433231853'
lookfor = ''
acc :
0 :
when = '0'
where = '106.186.17.xx'
which = '/mouth/1114_PHP-Image-upload-script.html'
1 :
when = '0'
where = '194.187.171.xx'
or
WomanWithCat:kingston grahamellis$ ./tj3.rb s_name | head
s_name['timedat'] = '1433231872'
s_name['lookfor'] = ''
s_name['acc'] :
s_name['acc'][0] :
s_name['acc'][0]['when'] = '1'
s_name['acc'][0]['where'] = '94.197.121.xx'
s_name['acc'][0]['which'] = '/pix/crpavatar.jpg'
s_name['acc'][1] :
s_name['acc'][1]['when'] = '3'
s_name['acc'][1]['where'] = '94.197.121.xx'
The source code for these examples is available through the links above, and the code is modestly commented - if you feel that you need to learn more about how it's done, please take a look at our
Ruby Training courses, or if you have a group of delegate please enquire about a tailored course at your offices or at our training centre. There are plenty of other examples to help you learn Ruby on our site - see
[here] for a complete module list - but there's nothing like having a tutor explain things to you and help you through the initial concepts to get you started!
(written 2015-06-01, updated 2015-06-02)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R107 - Collections (Arrays and Hashes) in Ruby [991] Adding a member to a Hash in Ruby - (2006-12-16)
[2291] Collection objects (array and hash) in Ruby - (2009-07-16)
[2606] Sorting arrays and hashes in Ruby - (2010-01-30)
[2618] What are Ruby Symbols? - (2010-02-02)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
[3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
[3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[4368] Shuffling a list - Ruby and Python - (2014-12-28)
[4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
R110 - Ruby - Special Variables and Pseudo-Variables [990] Ruby - Totally Topical - (2006-12-16)
[1586] Variable types in Ruby - (2008-03-21)
[1587] Some Ruby programming examples from our course - (2008-03-21)
[1891] Ruby to access web services - (2008-11-16)
[2296] Variable scope - what is it, and how does it Ruby? - (2009-07-18)
[2613] Constants in Ruby - (2010-02-01)
[2623] Object Oriented Ruby - new examples - (2010-02-03)
[4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
R113 - Ruby - Further Input and Output [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
[4553] RUby - loading, using, changing, storing JSON format data - (2015-10-23)
[4676] Running shell (operating system) commands from within Ruby - (2016-05-18)
[4678] Expect with Ruby - a training example to get you started - (2016-05-18)
R105 - Ruby - Classes and Objects [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
[2603] Ruby objects - a primer - (2010-01-29)
[2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
[2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[3421] Ruby off the Rails? - (2011-09-06)
[4009] Clear, concise examples - Ruby classes and objects. - (2013-02-17)
R114 - Ruby on the Web [2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
[2607] Answers on Ruby on Rails - (2010-01-30)
[3431] Ruby at both extremes of your website - (2011-09-10)
[3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
[3623] Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24)
[3773] Ruby on the web - a simple example using CGI - (2012-06-22)
[4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
Some other Articles
Peak weekend - where there are still rooms near MelkshamRegular Expressions for the petrified - in RubyWhere does Ruby load modules from, and how to load from current directorySeparating your code for easier testing, understanding and re-use; example in RubyReading and parsing a JSON object in RubyDefining the behaviour of your web site and testing that it worksThe TransWilts Community Intergrated Transport CorridorRuby - where one statement ends and the next beginsAround the world from Melksham