Skip to content

Instantly share code, notes, and snippets.

@nateroling
Created August 6, 2013 03:54
Show Gist options
  • Save nateroling/6161877 to your computer and use it in GitHub Desktop.
Save nateroling/6161877 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'json'
names=%w{ ututtza publicstaticmainvoid netscrape getus }
# Use a different method of string interpolation/templating.
# This way I can define the URL template once here, and then insert different
# names into it later.
# See: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-25
uri_template = "http://census.soe.com/json/get/ps2-beta/character/?name.first_lower=%{name}&c:show=name,online_status%%20&c:resolve=online_status"
names.each do |name|
uri = URI(uri_template % {name: name})
# Parse the JSON into a hash.
# Keys are strings, values are hashes, arrays, strings, numbers, or booleans.
status = JSON.parse(Net::HTTP.get(uri))
# So just grab the online status out of the JSON object.
online_status = status["character_list"][0]["online_status"]
# Check if online_status is "1" and save that to a boolean.
online = online_status == "1"
# Use string interpolation and a ternary if statement to print out status.
# See: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#short-if_expression_.28aka_ternary_operator.29
puts "#{name} is #{ online ? 'ONline' : 'OFFline' }"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment