-
-
Save tobynet/1397927 to your computer and use it in GitHub Desktop.
Simple and Stupid Ruby API for Coderwall.com
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
# Simple and Stupid Ruby API for Coderwall.com | |
# Vivien Didelot <[email protected]> | |
require "open-uri" | |
require "json" | |
module CoderWall | |
class Achievement | |
attr_reader :name, :badge, :description | |
def initialize(hashed_badge) | |
@name, @badge, @description = hashed_badge.values | |
end | |
end | |
module_function | |
def achievements_of(username) | |
raise(ArgumentError, "Invalid username") if username.empty? | |
url = URI.escape("http://coderwall.com/#{username}.json") | |
begin | |
response = JSON.load(open(url)) | |
rescue OpenURI::HTTPError | |
raise(ArgumentError, "Invalid username") | |
end | |
response["badges"].map { |badge| Achievement.new(badge) } | |
end | |
end |
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
#!/bin/env ruby | |
require "coderwall" | |
begin | |
achievements = CoderWall.achievements_of("v0n") | |
puts achievements.class # => Array | |
puts achievements.count # => 5 | |
puts achievements.first.class # => CoderWall::Achievement | |
puts achievements.first.name # => Walrus | |
puts achievements.first.badge # => http://coderwall.com/images/badges/walrus.png | |
puts achievements.first.description # => "The walrus is no stranger to variety..." | |
CoderWall.achievements_of("slkfjsldkfj") # => fail: Invalid username | |
rescue => e | |
puts "fail: " + e.message | |
end | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment