Last active
December 20, 2015 16:29
-
-
Save nateroling/6161980 to your computer and use it in GitHub Desktop.
Doing some decidedly advanced and ruby-specific things here, don't worry if they don't make sense yet. Let me know what you have questions on.
This file contains 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' | |
require 'json' | |
# Q: | |
# I kind of understand how this works. Each class is like a user made method | |
# (or function as I would call it). So whenever you call the player class it | |
# will run the parameters through. I read something about you can use that to | |
# make it more manageable. And you can mage gems out of it (or something - not | |
# quite sure on that part). | |
# A: | |
# Ok, first thing first. In Ruby, everything is an Object. Strings are | |
# objects, Numbers are objects, URIs are objects, etc. Everything. | |
# | |
# An object is a group of properties and methods. Properties are data, and | |
# methods are functions which work on that data. | |
# | |
# A Class is a definition of an object. So here I'm creating a Player class, | |
# which defines two methods. The first method is initialize, which is special | |
# in Ruby, because it gets called automatically whenever we create a new | |
# Player. The second is online?, which is not special at all. | |
# | |
# Ending a method name with a question mark is just a Ruby convention, that | |
# signifies the method returns true or false. It's just nicer than writing | |
# is_online. | |
# Q: | |
# Is the @variable syntax making that variable only available within that | |
# class? So if you used "name" within another class or anywhere outside that | |
# class it wouldn't hold the same value? | |
# A: | |
# @variable is how you declare properties. A property is data that is unique | |
# to an Object. In this case, we can create many Player objects (with | |
# Player.new('PlayerName'), and each one wiill have its own name and data | |
# properites. | |
# | |
# You can then access those properties with dot notation. | |
# bob = new Player('bob'); | |
# mike = new Player('mike'); | |
# | |
# bob.name would be 'bob' | |
# mike.name would be 'mike' | |
class Player | |
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" | |
def initialize(name) | |
@name = name | |
# I'm going to rewrite this below. | |
# @data = JSON.parse(Net::HTTP.get(URI(URI_TEMPLATE % {name: name}))) | |
# Q: | |
# this is kind of confusing. You're using the json parse method within | |
# json object, required from the first few lines not sure what | |
# .get(URI(.... does exactly. Do you need to do that because you can't | |
# directly use get on the template? i looked up the % and it says it | |
# divides the first half by the second half, so I don't know what's | |
# happening there | |
# A: | |
# The percent sign means different things in different contexts. | |
# When applied to numbers it means modulo, which is the remainder when | |
# dividing them. When applied to strings, it means to replace certain | |
# placeholders with values provided. Let's rewrite the above, using | |
# some intermediate variables. | |
# Note that I'm not using @variable here. These are temporary | |
# variables, I don't need them to be accessible on the object as | |
# properties. | |
# First, I make a hash, with one key and one value. The key is 'name', | |
# and the value is whatever name got passed into the initialize | |
# function. | |
template_values = {name: name} | |
# Then I use that hash with the % operator on the URI_TEMPLATE. That | |
# replaces the placeholder "%{name}" token with the value from the | |
# hash. | |
uri_string = URI_TEMPLATE % template_values | |
# uri_string is just a String object at this point, however. We need an | |
# actual URI object. URI is a builtin class. | |
uri = URI(uri_string) | |
# Now we're going to use the get method from the Net::HTTP module to | |
# get our JSON-formatted data string. A module is a special kind of object. | |
json_data_string= Net::HTTP.get(uri) | |
# Finally we use the parse method from the JSON module to transform | |
# our JSON-formatted string into a hash. Here we WILL use the | |
# @variable syntax, because we want @data to be a property that we can | |
# access later. | |
@data = JSON.parse(json_data_string) | |
# So that's the exact same thing as before, except using some | |
# temporary variables to break it out. | |
# | |
# In general, this: | |
# | |
# a = first_function() | |
# b = second_function(a) | |
# | |
# Can always be written like this: | |
# | |
# b = second_function(first_function())) | |
end | |
def online? | |
return @data["character_list"][0]["online_status"] == "1" | |
# Q: | |
# character_list is from the json return, and online_status is in the first | |
# part (0 is first) then see if online status is equal to 1 ---makes | |
# it true/false | |
# A: | |
# Yup. | |
end | |
attr_reader :name | |
end | |
%w{ ututtza publicstaticmainvoid netscrape getus }.each do |name| | |
player = Player.new(name) | |
puts "#{ player.name } is #{ player.online? ? 'ONline' : 'OFFline' }" | |
end | |
# Q: | |
# loops through array of each name, calls player and creates a new instance, | |
# using value of that 1 array field. | |
# A: | |
# Yes. | |
# Q: | |
# that value goes through the player class code. Then the last part is just | |
# the putting of the string where #{} inserts the text value of that variable. | |
# player.name would be the name from the array. and the player.online would be | |
# the online status of that player. So you created a custom class (object?) | |
# and name and online are the 2 methods? | |
# A: | |
# name is a property (it's just a string), online is a method (which looks up | |
# the online status in @data). But yes, pretty much | |
# Q: | |
# player.online? returns a boolean? then the second ? tells that 'ONline' is | |
# the true value and 'OFFline' is the false value? | |
# A: | |
# Yeah, it's called a 'ternary if statement', and it's kind of tricky. That | |
# whole line is tricky and kind of ugly, actually... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I'm not sure if this applies to you, but if you remove %%20 in online_status%%20&c:resolve the , it reduces the amount of information you get back to just what you need. I think when I copied my portion over it kept that which broke the restricting of data retrieval.
URI_TEMPLATE = "http://census.soe.com/json/get/ps2-beta/character/?name.first_lower=%{name}&c:show=name,online_status&c:resolve=online_status"