Class: OAuth2::Client

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

Instance Attribute Summary collapse

Class Method 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.

:parse_json

If true, application/json responses will be automatically parsed.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oauth2/client.rb', line 32

def initialize(client_id, client_secret, opts = {})
  adapter         = opts.delete(: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)
  self.json       = opts.delete(:parse_json)

  if adapter && adapter != :test
    connection.build { |b| b.adapter(adapter) }
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



17
18
19
# File 'lib/oauth2/client.rb', line 17

def connection
  @connection
end

#idObject

Returns the value of attribute id.



17
18
19
# File 'lib/oauth2/client.rb', line 17

def id
  @id
end

#json=(value) ⇒ Object (writeonly)

Sets the attribute json

Parameters:

  • value

    the value to set the attribute json to.



18
19
20
# File 'lib/oauth2/client.rb', line 18

def json=(value)
  @json = value
end

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/oauth2/client.rb', line 17

def options
  @options
end

#secretObject

Returns the value of attribute secret.



17
18
19
# File 'lib/oauth2/client.rb', line 17

def secret
  @secret
end

#siteObject

Returns the value of attribute site.



17
18
19
# File 'lib/oauth2/client.rb', line 17

def site
  @site
end

Class Method Details

.default_connection_adapterObject



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

def default_connection_adapter
  warn '[DEPRECATED] OAuth2::Client#default_connection_adapter is deprecated, use Faraday.default_adapter instead. Will be removed in 0.1.0'
  Faraday.default_adapter
end

.default_connection_adapter=(adapter) ⇒ Object



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

def default_connection_adapter=(adapter)
  warn '[DEPRECATED] OAuth2::Client#default_connection_adapter is deprecated, use Faraday.default_adapter instead. Will be removed in 0.1.0'
  Faraday.default_adapter = adapter
end

Instance Method Details

#access_token_url(params = nil) ⇒ Object



51
52
53
54
# File 'lib/oauth2/client.rb', line 51

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



46
47
48
49
# File 'lib/oauth2/client.rb', line 46

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

#json?Boolean

Returns:

  • (Boolean)


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

def json?; !!@json end

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

Makes a request relative to the specified site root.



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
82
# File 'lib/oauth2/client.rb', line 57

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..299
      if json?
        return ResponseObject.from(resp)
      else
        return ResponseString.new(resp)
      end
    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



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

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