Class: OAuth2::Client

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

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.



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.options = opts
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#secretObject

Returns the value of attribute secret.



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

def secret
  @secret
end

#siteObject

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_urlObject



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

def access_token_url
  return options[:access_token_url] if options[:access_token_url]

  uri = URI.parse(site)
  uri.path = options[:access_token_path] || "/oauth/access_token"
  uri.to_s
end

#authorize_urlObject



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

def authorize_url
  return options[:authorize_url] if options[:authorize_url]

  uri = URI.parse(site)
  uri.path = options[: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

#web_serverObject



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

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