Class: OAuth2::Client
- Inherits:
-
Object
- Object
- OAuth2::Client
- Defined in:
- lib/oauth2/client.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#options ⇒ Object
Returns the value of attribute options.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
- #access_token_url ⇒ Object
- #authorize_url ⇒ 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.
- #request(verb, url_or_path, params = {}, headers = {}) ⇒ Object
- #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_url
-
Specify the full URL of the access token endpoint.
18 19 20 21 22 23 |
# File 'lib/oauth2/client.rb', line 18 def initialize(client_id, client_secret, opts = {}) self.id = client_id self.secret = client_secret self.site = opts.delete(:site) if opts[:site] self. = opts end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/oauth2/client.rb', line 5 def @options 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 |
Instance Method Details
#access_token_url ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/oauth2/client.rb', line 33 def access_token_url return [:access_token_url] if [:access_token_url] uri = URI.parse(site) uri.path = [:access_token_path] || "/oauth/access_token" uri.to_s end |
#authorize_url ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/oauth2/client.rb', line 25 def return [:authorize_url] if [:authorize_url] uri = URI.parse(site) uri.path = [:authorize_path] || "/oauth/authorize" uri.to_s end |
#request(verb, url_or_path, params = {}, headers = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/oauth2/client.rb', line 41 def request(verb, url_or_path, params = {}, headers = {}) if url_or_path[0..3] == 'http' uri = URI.parse(url_or_path) path = uri.path else uri = URI.parse(self.site) path = (uri.path + url_or_path).gsub('//','/') end net = Net::HTTP.new(uri.host, uri.port) net.use_ssl = (uri.scheme == 'https') net.start do |http| if verb == :get uri.query_hash = uri.query_hash.merge(params) path += "?#{uri.query}" end req = Net::HTTP.const_get(verb.to_s.capitalize).new(path, headers) unless verb == :get req.set_form_data(params) end response = http.request(req) case response when Net::HTTPSuccess response.body when Net::HTTPUnauthorized e = OAuth2::AccessDenied.new("Received HTTP 401 when retrieving access token.") e.response = response raise e else e = OAuth2::HTTPError.new("Received HTTP #{response.code} when retrieving access token.") e.response = response raise e end end end |