Class: FlexmlsApi::Authentication::OAuth2

Inherits:
BaseAuth
  • Object
show all
Defined in:
lib/flexmls_api/authentication/oauth2.rb

Overview

OAuth2

Implementation the BaseAuth interface for API style authentication

Instance Method Summary collapse

Methods inherited from BaseAuth

#authenticated?, #build_url_parameters

Constructor Details

#initialize(client) ⇒ OAuth2

Returns a new instance of OAuth2.



24
25
26
27
# File 'lib/flexmls_api/authentication/oauth2.rb', line 24

def initialize(client)
  super(client)
  @provider = client.oauth2_provider
end

Instance Method Details

#authenticateObject



36
37
38
39
40
# File 'lib/flexmls_api/authentication/oauth2.rb', line 36

def authenticate
  granter = OAuth2Impl::GrantTypeBase.create(@client, @provider, session)
  self.session = granter.authenticate
  session
end

#authorization_urlObject



63
64
65
66
67
68
69
70
# File 'lib/flexmls_api/authentication/oauth2.rb', line 63

def authorization_url()
  params = {
    "client_id" => @provider.client_id,
    "response_type" => "code",
    "redirect_uri" => @provider.redirect_uri
  }
  "#{@provider.authorization_uri}?#{build_url_parameters(params)}"
end

#logoutObject



59
60
61
# File 'lib/flexmls_api/authentication/oauth2.rb', line 59

def logout
  @provider.save_session(nil)
end

#request(method, path, body, options = {}) ⇒ Object

Perform an HTTP request (no data)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flexmls_api/authentication/oauth2.rb', line 43

def request(method, path, body, options={})
  escaped_path = URI.escape(path)
  connection = @client.connection(true)  # SSL Only!
  connection.headers.merge!(self.auth_header)
  parameter_string = options.size > 0 ? "?#{build_url_parameters(options)}" : ""
  request_path = "#{escaped_path}#{parameter_string}"
  FlexmlsApi.logger.debug("Request: #{request_path}")
  if body.nil?
    response = connection.send(method, request_path)
  else
    FlexmlsApi.logger.debug("Data: #{body}")
    response = connection.send(method, request_path, body)
  end
  response
end

#sessionObject



29
30
31
# File 'lib/flexmls_api/authentication/oauth2.rb', line 29

def session
  @provider.load_session()
end

#session=(s) ⇒ Object



32
33
34
# File 'lib/flexmls_api/authentication/oauth2.rb', line 32

def session=(s)
  @provider.save_session(s)
end