Created
August 16, 2016 16:18
-
-
Save ParkinT/b95d9e697c5b69e1d7cc3b323f00bede to your computer and use it in GitHub Desktop.
Providing guidance (and some example code) in mentoring a new developer in Ruby, I threw together this little sample that calls upon my BusinessSpew API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' #including external libraries | |
require 'json' | |
URL = "http://bs.leveragedsynergies.com/api" #Constant definition | |
#TODO: params can be extracted from input on the command line <== and example of a comment | |
params = { paragraphs: 2, sentences: 4, title: 'Daily BusinessSpew Report' } #Ruby hash for set of parameters | |
uri_string = URI.escape("#{URL}/#{params[:sentences]}/#{params[:paragraphs]}/#{params[:title]}") #illustration of Ruby string interpolation | |
uri = URI(uri_string) | |
request = Net::HTTP.get(uri) #call to "class method" (in external library) | |
bs = JSON.parse(request) | |
title = bs['title'] #manipulating JSON | |
paragraphs = [] #assignment of a variable in order to establish its scope | |
bs['paragraphs'].each do |p| #common Iteration AND Ruby Block | |
paragraphs << p # "shovel" shorthand (syntactic sugar) | |
end | |
puts "#{title}\n#{'=' * (title.size)}\n\n" #the common 'puts' statement and more string interpolation | |
puts paragraphs.collect{ |p| ["\t#{p}\n\n"] } #common ruby iteration operation (inline block) | |
puts # empty 'puts' provides blank line | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could be easily enhanced to accept input parameters (using the Ruby
hash.merge!
command. I will resist the strong temptation to modify this and leave that as an exercise for you - the reader.