Class: OAuth2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, opts = {}) ⇒ Client

Instantiate a new OAuth 2.0 client using the client ID and client secret registered to your application.

Options:

:site

Specify a base URL for your OAuth 2.0 client.

:authorize_path

Specify the path to the authorization endpoint.

:authorize_url

Specify a full URL of the authorization endpoint.

:access_token_path

Specify the path to the access token endpoint.

:access_token_url

Specify the full URL of the access token endpoint.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/oauth2/client.rb', line 23

def initialize(client_id, client_secret, opts = {})
  adapter         = opts.delete(:adapter) || self.class.default_connection_adapter
  self.id         = client_id
  self.secret     = client_secret
  self.site       = opts.delete(:site) if opts[:site]
  self.options    = opts
  self.connection = Faraday::Connection.new(site)
  if adapter != :test
    connection.build { |b| b.adapter(adapter) }
  end
end

Class Attribute Details

.default_connection_adapterObject

Returns the value of attribute default_connection_adapter.



6
7
8
# File 'lib/oauth2/client.rb', line 6

def default_connection_adapter
  @default_connection_adapter
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/oauth2/client.rb', line 10

def connection
  @connection
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/oauth2/client.rb', line 10

def id
  @id
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/oauth2/client.rb', line 10

def options
  @options
end

#secretObject

Returns the value of attribute secret.



10
11
12
# File 'lib/oauth2/client.rb', line 10

def secret
  @secret
end

#siteObject

Returns the value of attribute site.



10
11
12
# File 'lib/oauth2/client.rb', line 10

def site
  @site
end

Instance Method Details

#access_token_url(params = nil) ⇒ Object



40
41
42
43
# File 'lib/oauth2/client.rb', line 40

def access_token_url(params = nil)
  path = options[:access_token_url] || options[:access_token_path] || "/oauth/access_token"
  connection.build_url(path, params).to_s
end

#authorize_url(params = nil) ⇒ Object



35
36
37
38
# File 'lib/oauth2/client.rb', line 35

def authorize_url(params = nil)
  path = options[:authorize_url] || options[:authorize_path] || "/oauth/authorize"
  connection.build_url(path, params).to_s
end

#request(verb, url, params = {}, headers = {}) ⇒ Object

Makes a request relative to the specified site root.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/oauth2/client.rb', line 46

def request(verb, url, params = {}, headers = {})
  if verb == :get
    resp = connection.run_request(verb, url, nil, headers) do |req|
      req.params.update(params)
    end
  else
    resp = connection.run_request(verb, url, params, headers)
  end
  case resp.status
    when 200...201 then ResponseString.new(resp)
    when 401
      e = OAuth2::AccessDenied.new("Received HTTP 401 during request.")
      e.response = resp
      raise e
    else
      e = OAuth2::HTTPError.new("Received HTTP #{resp.status} during request.")
      e.response = resp
      raise e
  end
end

#web_serverObject



67
# File 'lib/oauth2/client.rb', line 67

def web_server; OAuth2::Strategy::WebServer.new(self) end