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 your 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("mfernand")
user     = response.user

user.id                            #=> 19619
user.email                         #=> "[email protected]"
user.                         #=> "mfernand"
user.first_name                    #=> "Matias"
user.last_name                     #=> "Fernandez"
user.url                           #=> "https://api.intra.42.fr/v2/users/mfernand"
user.phone                         #=> nil
user.url                           #=> "https://api.intra.42.fr/v2/users/mfernand"
user.displayname                   #=> "Matias Fernandez"
user.image_url                     #=> "https://cdn.intra.42.fr/users/mfernand.jpg"
user.staff                         #=> false
user.correction_point              #=> 2
user.pool_month                    #=> "july"
user.pool_year                     #=> "2016"
user.location                      #=> nil
user.wallet                        #=> 0
user.groups                        #=> []

user.cursus_users.each do |user_cursus|
  user_cursus.id                   #=> 14121
  user_cursus.begin_at             #=> "2016-09-19T22:00:00.000Z"
  user_cursus.end_at               #=> nil
  user_cursus.grade                #=> "Midshipman"
  user_cursus.level                #=> 3.91
  user_cursus.cursus_id            #=> 1

  user_cursus.skills.each do |skill|
    skill.id                       #=> 3
    skill.name                     #=> "Rigor"
    skill.level                    #=> 2.88
  end

  user_cursus.cursus.id            #=> 1
  user_cursus.cursus.created_at    #=> "2014-11-02T16:43:38.480Z"
  user_cursus.cursus.name          #=> "42"
  user_cursus.cursus.slug          #=> "42"
end

user.projects_users.each do |user_project|
  user_project.id                  #=> 405588
  user_project.occurence           #=> nil
  user_project.final_mark          #=> 115
  user_project.status              #=> "finished"
  user_project.validated           #=> true
  user_project.current_team_id     #=> 1434665
  user_project.cursus_ids          #=> [1]

  user_project.project.id          #=> 1
  user_project.project.name        #=> "Libft"
  user_project.project.slug        #=> "libft"
end

user.expertises_users.each do |expertise|
  expertise.id                     #=> 7696
  expertise.expertise_id           #=> 29
  expertise.interested             #=> true
  expertise.value                  #=> 2
  expertise.contact_me             #=> false
  expertise.created_at             #=> "2016-10-15T02:05:37.861Z"
  expertise.user_id                #=> 19619
end

user.achievements.each do |achievement|
  achievement.id                   #=> 41
  achievement.name                 #=> "In the name of Nicolas"
  achievement.description          #=> "Etre logué 90h sur une semaine. (à bosser, comme Nicolas vous l'a conseillé !)"
  achievement.tier                 #=> "easy"
  achievement.kind                 #=> "scolarity"
  achievement.visible              #=> true
  achievement.image                #=> "/uploads/achievement/image/41/SCO001.svg"
  achievement.nbr_of_success       #=> nil
  achievement.users_url            #=> "https://api.intra.42.fr/v2/achievements/41/users"
end

response      = client.user_sessions("mfernand")
user_sessions = response.user_sessions

user_sessions.each do |session|
  session.id                       #=> 5017063
  session.begin_at                 #=> "2016-11-14T09:57:19"
  session.end_at                   #=> "2016-11-14T10:07:19"
  session.primary                  #-> true
  session.floor                    #=> nil
  session.row                      #=> nil
  session.post                     #=> nil
  session.host                     #=> "e1z2r7p18"
  session.campus_id                #=> 7
end

Contribute to FortyTwo

There's lots of ways you can contribute. Hit me up on slack, fork it, play around with the implementation, or improve the docs.