class Client

Attributes

input_1[R]
input_2[R]
input_3[R]
input_4[R]
token[R]

Public Class Methods

new(input_1, input_2 = nil, input_3 = nil, input_4 = nil) click to toggle source
# File lib/ft_42.rb, line 83
def initialize(input_1, input_2 = nil, input_3 = nil, input_4 = nil)
  @input_1   = input_1
  @input_2   = input_2
  @input_3   = input_3
  @input_4   = input_4
  @token     = Token.new.token
end

Public Instance Methods

campus() click to toggle source
# File lib/ft_42.rb, line 103
def campus
  token.get("/v2/campus/#{input_2}").parsed
end
project() click to toggle source
# File lib/ft_42.rb, line 99
def project
  token.get("/v2/projects/#{input_1}").parsed
end
project_users() click to toggle source
# File lib/ft_42.rb, line 107
def project_users
  user_projects = []
  i = 1
  loop do
    begin
      tries ||= 3
      if input_3
        response = token.get("/v2/projects_users?filter[campus]=#{campus['id']}&filter[project_id]=#{project['id']}&range[created_at]=#{after},#{before}", params: { page: i, per_page: 100 }).parsed
      else
        response = token.get("/v2/projects_users?filter[campus]=#{campus['id']}&filter[project_id]=#{project['id']}", params: { page: i, per_page: 100 }).parsed
      end
    rescue
      puts "Something went wrong..."
      puts "REFRESHING API TOKEN... wait 8 sec"
      sleep 8
      client = OAuth2::Client.new(ENV.fetch("UID42"), ENV.fetch("SECRET42"), site: ENV.fetch("API42"))
      token  = client.client_credentials.get_token
      puts "Retrying request..."
      retry unless (tries -= 1).zero?
    else
      break if response.empty?
      user_projects << response
      i += 1
    end
  end
  user_projects
end
user() click to toggle source
# File lib/ft_42.rb, line 91
def user
  token.get("/v2/users/#{input_1}", params: { per_page: 100 }).parsed
end
user_sessions() click to toggle source
# File lib/ft_42.rb, line 95
def user_sessions
  token.get("/v2/users/#{input_1}/locations?range[end_at]=#{time_ago},#{right_now}", params: { per_page: 100 }).parsed
end

Private Instance Methods

after() click to toggle source
# File lib/ft_42.rb, line 137
def after
  input_3.to_time.to_s.split(" ")[0...-1].join("T")
end
before() click to toggle source
# File lib/ft_42.rb, line 141
def before
  if input_4
    return input_4.to_time.to_s.split(" ")[0...-1].join("T")
  else
    return Time.current.to_s.split(" ")[0...-1].join("T")
  end
end
right_now() click to toggle source
# File lib/ft_42.rb, line 158
def right_now
  Time.current.to_s.split(" ")[0...-1].join("T")
end
time_ago() click to toggle source
# File lib/ft_42.rb, line 149
def time_ago
  if input_2
    time = Time.current - (input_2.to_i * 7).days
    return time.to_s.split(" ")[0...-1].join("T")
  else
    return Time.current.beginning_of_week.to_s.split(" ")[0...-1].join("T")
  end
end