Class: Clavem::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/clavem/server.rb

Overview

A class to handle oAuth callbacks on the browser via HTTP.

Constant Summary collapse

TEMPLATE =

The template to send to the browser.

<<~EOTEMPLATE
.freeze

Instance Method Summary collapse

Constructor Details

#initialize(authorizer) ⇒ Server

Creates a new server.

Parameters:

  • authorizer (Authorizer)

    The authorizer of this server.



26
27
28
29
30
31
# File 'lib/clavem/server.rb', line 26

def initialize(authorizer)
  @authorizer = authorizer
  @i18n = Bovem::I18n.new(root: "clavem", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")

  process_http_request
end

Instance Method Details

#process_http_requestObject

Save the token and sends a response back to the user.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clavem/server.rb', line 34

def process_http_request
  server = create_server
  socket = server.accept

  # Get the request
  request = socket.gets.gsub(/^[A-Z]+\s(.+)\sHTTP.+$/, "\\1")
  querystring = Addressable::URI.parse(("%s%s" % [@authorizer.callback_url, request]).strip).query_values

  # Send the response and close the socket
  send_response(socket)

  # Handle the token
  token = @authorizer.response_handler.call(querystring)

  update_authorizer(token)
  server.close
end