fortytwo
This ruby gem provides a simple interface to the 42 API. Consuming data from the API is now easier and more readable.
Install
Add this to your application's Gemifle:
gem 'fortytwo'
And then execute:
bundle install
Or install it yourself as:
gem install fortytwo
NOTE: IF YOU'RE INSTALLING IN THE CLUSTER'S MACS, SETUP LOCALLY LIKE THIS:
gem install --user-install fortytwo
Get your API key and secret
This gem uses a client model to query against the API. You need to create and configure a 42 App containing your API key and API secret and make requests through that. Go here (https://profile.intra.42.fr/oauth/applications/new) to create the app and get the keys. You can name it whatever you want and redirect it wherever you want.
Usage
Initialize
Once you have you're key's you can initialize it like this:
require 'fortytwo'
client = FortyTwo::Client.new({ api_key: YOUR_API_KEY, api_secret: YOUR_API_SECRET })
Responses
Responses from the API are all parsed and converted into Ruby objects. This way, you're able to access information using dot notation.
response = client.user("bsaget")
user = response.user
user.id #=>
user.first_name #=> Bob
user.last_name #=> Saget
user.cursus_users.first.level #=> 12.3
user.instance_variables
# [:@id, :@email, :@login, :@first_name, :@last_name, :@url, :@phone, :@displayname, :@image_url,
# :@staff, :@correction_point, :@pool_month, :@pool_year, :@location, :@wallet, :@groups,
# :@cursus_users, :@projects_users, :@achievements, :@titles, :@partnerships, :@patroned,
# :@patroning, :@expertises_users, :@campus]
user.cursus_users.first.instance_variables
# [:@id, :@begin_at, :@end_at, :@grade, :@level, :@skills, :@cursus_id, :@user, :@cursus]
user.projects_users.first.instance_variables
# [:@id, :@occurrence, :@final_mark, :@status, :@validated, :@current_team_id, :@project, :@cursus_ids]
user.expertises_users.first.instance_variables
# [:@id, :@expertise_id, :@interested, :@value, :@contact_me, :@created_at, :@user_id]
user.campus.first.instance_variables
# [:@id, :@name, :@time_zone, :@language, :@users_count, :@vogsphere_id]
That's it! You have access to all the info obtained from a User request to the 42 API.