Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2014 21:31
Show Gist options
  • Save anonymous/cb3f32b3ab336f2af4fe to your computer and use it in GitHub Desktop.
Save anonymous/cb3f32b3ab336f2af4fe to your computer and use it in GitHub Desktop.
DIY API in Ruby
#!/usr/bin/ruby
require 'rubygems'
require 'net/http'
require 'io/console'
require 'json'
puts "Welcome to the DIY Person Watcher."
puts "Put an empty string as the User's URL to exit."
puts
while true # Do forever
puts
print "User's URL: "
userurl = gets.chomp
if userurl.nil? or userurl.empty?
puts "Goodbye!"
break
end
puts
if userurl =~ URI::regexp # If userurl is actually a URL
userurl = URI(userurl).path[1..-1]
# Convert userurl to a URI object, get the path, remove the first character (leading slash), and use the output to replace userurl
end
patches = JSON.parse(Net::HTTP.get_response(URI("http://api.diy.org/makers/#{userurl}/patches?limit=-1")).body)
# Get the API's list of patches that the maker has, parse them into a hash, and store them in the patches variable.
if patches["head"]["code"].to_s.start_with? "4" # Something went wrong on our end.
if patches["head"]["code"].to_s == "404"
puts "User does not exist, or you typed it incorrectly."
puts "Please make sure you are copying either the user's full profile url, or are copying the text that comes after the slash."
redo
end
puts "Unknown error occured."
puts "Please make sure you are copying either the user's full profile url, or are copying the text that comes after the slash."
redo
end
if patches["head"]["code"].to_s.start_with? "5" # Something went wrong on DIY's end.
puts "Something went wrong. Is DIY up?"
puts "Try again later."
redo
end
patches["response"].each {|key, value|
puts "#{key["skill"]["title"]}"
}
end
#This is free and unencumbered software released into the public domain.
#Anyone is free to copy, modify, publish, use, compile, sell, or
#distribute this software, either in source code form or as a compiled
#binary, for any purpose, commercial or non-commercial, and by any
#means.
#In jurisdictions that recognize copyright laws, the author or authors
#of this software dedicate any and all copyright interest in the
#software to the public domain. We make this dedication for the benefit
#of the public at large and to the detriment of our heirs and
#successors. We intend this dedication to be an overt act of
#relinquishment in perpetuity of all present and future rights to this
#software under copyright law.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.
#For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment