Class: Profile
- Inherits:
-
Object
- Object
- Profile
- Defined in:
- lib/magister/profile.rb
Overview
This represents a profile or a user in magister
Instance Method Summary collapse
-
#authenticatedRequest(endpoint) ⇒ Hash
Makes a request that is authenticated on the users behalve.
-
#id ⇒ Integer
Get the user id.
-
#initialize(token, school) ⇒ Profile
constructor
Returns a new instance of Profile.
-
#inspect ⇒ String
Get a summary of the object.
-
#person ⇒ Person
Get the person of the profile.
-
#school ⇒ String
What school the user attends.
-
#token=(token) ⇒ Object
Set the user token.
-
#verify ⇒ Object
Verify the authenticity of the token and obtain user data.
Constructor Details
#initialize(token, school) ⇒ Profile
Returns a new instance of Profile.
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.
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 |
#id ⇒ Integer
Get the user id
31 32 33 |
# File 'lib/magister/profile.rb', line 31 def id @id end |
#inspect ⇒ String
Get a summary of the object
43 44 45 |
# File 'lib/magister/profile.rb', line 43 def inspect "#<#{self.class}:0x#{object_id} @token=\"[PRIVATE]\", @id=#{@id}, @school=#{@school}>" end |
#person ⇒ Person
Get the person of the profile
50 51 52 |
# File 'lib/magister/profile.rb', line 50 def person @person end |
#school ⇒ String
What school the user attends
37 38 39 |
# File 'lib/magister/profile.rb', line 37 def school @school end |
#token=(token) ⇒ Object
Set the user token
25 26 27 |
# File 'lib/magister/profile.rb', line 25 def token=(token) @token=token end |
#verify ⇒ Object
Note:
token
and school
of the #Profile must be defined
Verify the authenticity of the token and obtain user data
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 |