Skip to content

Instantly share code, notes, and snippets.

@ParkinT
Created August 16, 2016 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ParkinT/b95d9e697c5b69e1d7cc3b323f00bede to your computer and use it in GitHub Desktop.
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.
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
@ParkinT
Copy link
Author

ParkinT commented Aug 16, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment