Class: OAuth2::Client
- Inherits:
-
Object
- Object
- OAuth2::Client
- Defined in:
- lib/oauth2/client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#id ⇒ Object
Returns the value of attribute id.
-
#json ⇒ Object
writeonly
Sets the attribute json.
-
#options ⇒ Object
Returns the value of attribute options.
-
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#site ⇒ Object
Returns the value of attribute site.
-
#token_method ⇒ Object
Returns the value of attribute token_method.
Instance Method Summary collapse
- #access_token_url(params = nil) ⇒ Object
- #authorize_url(params = nil) ⇒ Object
-
#initialize(client_id, client_secret, opts = {}) ⇒ Client
constructor
Instantiate a new OAuth 2.0 client using the client ID and client secret registered to your application.
- #json? ⇒ Boolean
- #password ⇒ Object
-
#request(verb, url, params = {}, headers = {}) ⇒ Object
Makes a request relative to the specified site root.
- #web_server ⇒ Object
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_method
-
Specify the method to use for token endpoints, can be :get or :post
(note: for Facebook this should be :get and for Google this should be :post)
:access_token_url
-
Specify the full URL of the access token endpoint.
:parse_json
-
If true,
application/json
responses will be automatically parsed. :ssl
-
Specify SSL options for the connection.
:adapter
-
The name of the Faraday::Adapter::* class to use, e.g. :net_http. To pass arguments
to the adapter pass an array here, e.g. [:action_dispatch, my_test_session]
:raise_errors
-
Default true. When false it will then return the error status and response instead of raising an exception.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/oauth2/client.rb', line 26 def initialize(client_id, client_secret, opts={}) self. = opts.dup self.token_method = self..delete(:access_token_method) || :post adapter = self..delete(:adapter) ssl_opts = self..delete(:ssl) || {} connection_opts = ssl_opts ? {:ssl => ssl_opts} : {} self.id = client_id self.secret = client_secret self.site = self..delete(:site) if self.[:site] self.connection = Faraday::Connection.new(site, connection_opts) self.json = self..delete(:parse_json) self.raise_errors = !(self..delete(:raise_errors) == false) if adapter && adapter != :test connection.build do |b| b.adapter(*[adapter].flatten) end end end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def connection @connection end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def id @id end |
#json=(value) ⇒ Object (writeonly)
Sets the attribute json
6 7 8 |
# File 'lib/oauth2/client.rb', line 6 def json=(value) @json = value end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def @options end |
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def raise_errors @raise_errors end |
#secret ⇒ Object
Returns the value of attribute secret.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def site @site end |
#token_method ⇒ Object
Returns the value of attribute token_method.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def token_method @token_method 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 = [:access_token_url] || [: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 (params=nil) path = [:authorize_url] || [:authorize_path] || "/oauth/authorize" connection.build_url(path, params).to_s end |
#json? ⇒ Boolean
90 |
# File 'lib/oauth2/client.rb', line 90 def json?; !!@json end |
#password ⇒ Object
93 |
# File 'lib/oauth2/client.rb', line 93 def password; OAuth2::Strategy::Password.new(self) 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 83 84 85 86 87 88 |
# File 'lib/oauth2/client.rb', line 57 def request(verb, url, params={}, headers={}) if (verb == :get) || (verb == :delete) 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 if raise_errors case resp.status when 200...299 return response_for(resp) when 302 return request(verb, resp.headers['location'], params, headers) when 401 e = OAuth2::AccessDenied.new("Received HTTP 401 during request.") e.response = resp raise e when 409 e = OAuth2::Conflict.new("Received HTTP 409 during request.") e.response = resp raise e else e = OAuth2::HTTPError.new("Received HTTP #{resp.status} during request.") e.response = resp raise e end else response_for resp end end |