Class: Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/magister/profile.rb

Overview

This represents a profile or a user in magister

Instance Method Summary collapse

Constructor Details

#initialize(token, school) ⇒ Profile

Returns a new instance of Profile.

Parameters:

  • token (String)

    the users token

  • school (String)

    the school the user attends

Since:

  • 1.1.0



15
16
17
18
19
20
# File 'lib/magister/profile.rb', line 15

def initialize(token, school)
  @token = token
  @id = 0
  @school = school
  @person = nil
end

Instance Method Details

#authenticatedRequest(endpoint) ⇒ Hash

Note:

if you put +<em>id</em>+ in the endpoint, it will be replaced by the users actual id.

Makes a request that is authenticated on the users behalve.

Examples:

make a request to https://SCHOOL.magister.net/api/sessions/current to get information about the current session (profile is an instance of Profile)

sessionInfo = profile.authenticatedRequest("/sessions/current")

Parameters:

  • endpoint (String)

    the endpoint to request (starting with slash, excluding /api)

Returns:

  • (Hash)

    the response given by the magister api.

Since:

  • 1.0.0



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/magister/profile.rb', line 90

def authenticatedRequest(endpoint)
  if @id == 0
    puts "Not yet authenticated!"
    self.verify()
  end
  uri = URI("https://#{@school}.magister.net/api#{endpoint}".gsub("{*id*}", @id.to_s))
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request['authorization'] = "Bearer #{@token}"

  begin
    response = http.request(request)
    if response.is_a?(Net::HTTPSuccess)
      return JSON.parse(response.body)
    else
      puts "failed to get #{endpoint}, status code #{response.code}"
      return nil
    end
  rescue StandardError => e
    puts "An error occurred #{e}"
  end
end

#idInteger

Get the user id

Returns:

  • (Integer)

    id

Since:

  • 1.0.0



31
32
33
# File 'lib/magister/profile.rb', line 31

def id
  @id
end

#inspectString

Get a summary of the object

Returns:

  • (String)

    the summary

Since:

  • 1.0.0



43
44
45
# File 'lib/magister/profile.rb', line 43

def inspect
  "#<#{self.class}:0x#{object_id} @token=\"[PRIVATE]\", @id=#{@id}, @school=#{@school}>"
end

#personPerson

Get the person of the profile

Returns:

Since:

  • 1.0.0



50
51
52
# File 'lib/magister/profile.rb', line 50

def person
  @person
end

#schoolString

What school the user attends

Returns:

  • (String)

    name of the school

Since:

  • 1.0.0



37
38
39
# File 'lib/magister/profile.rb', line 37

def school
  @school
end

#token=(token) ⇒ Object

Set the user token

Parameters:

  • token (String)

    new token

Since:

  • 1.1.0



25
26
27
# File 'lib/magister/profile.rb', line 25

def token=(token)
  @token=token
end

#verifyObject

Note:

token and school of the #Profile must be defined

Verify the authenticity of the token and obtain user data

Since:

  • 1.0.0



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/magister/profile.rb', line 57

def verify()
  if @school == nil || @token == nil
    puts "Either school or token was not defined!"
  end
  puts "authenticating to #{@school}..."
  uri = URI("https://#{@school}.magister.net/api/account")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request['authorization'] = "Bearer #{@token}"

  begin
    response = http.request(request)
    if response.is_a?(Net::HTTPSuccess)
      res = JSON.parse(response.body)
      @person = Person.new res["Persoon"]
      @id = @person.id.to_i
      puts "Succesfully authenticated as #{@person.firstName} with id #{@person.id}"
    else
      puts "Failed to authenticate, http code #{response.code}"
    end
  rescue StandardError => e
    puts "An error occurred #{e}"
  end
end