Last active
April 4, 2020 18:19
-
-
Save SergioCC14/38f9d8456fe2d40530a8b65bf2cefb8f to your computer and use it in GitHub Desktop.
A Codewars API wrapper
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
# frozen_string_literal: true | |
require 'dotenv' | |
require 'net/http' | |
require 'json' | |
module Codewars | |
Dotenv.load('.env') | |
ENDPOINT = 'https://www.codewars.com/api/v1/' | |
API_KEY = ENV['CW_API_KEY'] | |
class User | |
def initialize(id_or_username) | |
@id_or_username = id_or_username | |
end | |
# GET User | |
# https://www.codewars.com/api/v1/users/:id_or_username | |
def info | |
@info ||= Codewars.cw_request("users/#{@id_or_username}") | |
end | |
# GET User: Completed Challenges | |
# https://www.codewars.com/api/v1/users/:id_or_username/code-challenges/completed?page=0 | |
def challenges_completed | |
@challenges_completed ||= Codewars.cw_request("users/#{@id_or_username}/code-challenges/completed") | |
end | |
# GET User: Authored Challenges | |
# https://www.codewars.com/api/v1/users/:id_or_username/code-challenges/authored | |
def challenges_authored | |
@challenges_authored ||= Codewars.cw_request("users/#{@id_or_username}/code-challenges/authored") | |
end | |
end | |
class Challenge | |
def initialize(id_or_slug) | |
@id_or_slug = id_or_slug | |
end | |
# GET Code Challenge | |
# https://www.codewars.com/api/v1/code-challenges/:id_or_slug | |
def info | |
@info ||= Codewars.cw_request("code-challenges/#{@id_or_slug}") | |
end | |
end | |
# private | |
def self.cw_request(path) | |
res = Net::HTTP.get URI(ENDPOINT + path + '?access_key=' + API_KEY) | |
JSON.parse res | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would suggest don't include path (/api/v1) in the
ENDPOINT
variable. What if there is a API with version 2 (/api/v2)?Better way to use it: