class OAuth2Client::HttpConnection
Constants
- NET_HTTP_EXCEPTIONS
Attributes
accept[RW]
config[RW]
headers[RW]
host[RW]
max_redirects[RW]
port[RW]
scheme[RW]
ssl[RW]
user_agent[RW]
Public Class Methods
default_options()
click to toggle source
# File lib/oauth2-client/connection.rb, line 32 def self.default_options { :headers => { 'Accept' => 'application/json', 'User-Agent' => "OAuth2 Ruby Gem #{OAuth2Client::Version}" }, :ssl => {:verify => true}, :max_redirects => 5 } end
new(url, options={})
click to toggle source
# File lib/oauth2-client/connection.rb, line 43 def initialize(url, options={}) @uri = Addressable::URI.parse(url) self.class.default_options.keys.each do |key| instance_variable_set(:"@#{key}", options.fetch(key, self.class.default_options[key])) end end
Public Instance Methods
absolute_url(path='')
click to toggle source
# File lib/oauth2-client/connection.rb, line 74 def absolute_url(path='') "#{scheme}://#{host}#{path}" end
default_headers()
click to toggle source
# File lib/oauth2-client/connection.rb, line 50 def default_headers self.class.default_options[:headers] end
http_connection(opts={})
click to toggle source
# File lib/oauth2-client/connection.rb, line 87 def http_connection(opts={}) _host = opts[:host] || host _port = opts[:port] || port _scheme = opts[:scheme] || scheme @http_client = Net::HTTP.new(_host, _port) configure_ssl(@http_client) if _scheme == 'https' @http_client end
scheme=(scheme)
click to toggle source
# File lib/oauth2-client/connection.rb, line 54 def scheme=(scheme) unless ['http', 'https'].include? scheme raise UnsupportedSchemeError.new "#{scheme} is not supported, only http and https" end @scheme = scheme end
send_request(method, path, opts={})
click to toggle source
# File lib/oauth2-client/connection.rb, line 99 def send_request(method, path, opts={}) headers = @headers.merge(opts.fetch(:headers, {})) params = opts[:params] || {} query = Addressable::URI.form_encode(params) method = method.to_sym normalized_path = query.empty? ? path : [path, query].join("?") client = http_connection(opts.fetch(:connection_options, {})) if (method == :post || method == :put) headers['Content-Type'] ||= 'application/x-www-form-urlencoded' end case method when :get, :delete response = client.send(method, normalized_path, headers) when :post, :put response = client.send(method, path, query, headers) else raise UnhandledHTTPMethodError.new("Unsupported HTTP method, #{method}") end status = response.code.to_i case status when 301, 302, 303, 307 unless redirect_limit_reached? if status == 303 method = :get params = nil headers.delete('Content-Type') end redirect_uri = Addressable::URI.parse(response.header['Location']) conn = { :scheme => redirect_uri.scheme, :host => redirect_uri.host, :port => redirect_uri.port } return send_request(method, redirect_uri.path, :params => params, :headers => headers, :connection_options => conn) end when 100..599 @redirect_count = 0 else raise "Unhandled status code value of #{response.code}" end response rescue *NET_HTTP_EXCEPTIONS raise "Error::ConnectionFailed, $!" end
ssl=(opts)
click to toggle source
# File lib/oauth2-client/connection.rb, line 82 def ssl=(opts) raise "Expected Hash but got #{opts.class.name}" unless opts.is_a?(Hash) @ssl.merge!(opts) end
ssl?()
click to toggle source
# File lib/oauth2-client/connection.rb, line 78 def ssl? scheme == "https" ? true : false end
Private Instance Methods
configure_ssl(http)
click to toggle source
# File lib/oauth2-client/connection.rb, line 150 def configure_ssl(http) http.use_ssl = true http.verify_mode = ssl_verify_mode http.cert_store = ssl_cert_store http.cert = ssl[:client_cert] if ssl[:client_cert] http.key = ssl[:client_key] if ssl[:client_key] http.ca_file = ssl[:ca_file] if ssl[:ca_file] http.ca_path = ssl[:ca_path] if ssl[:ca_path] http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth] http.ssl_version = ssl[:version] if ssl[:version] end
redirect_limit_reached?()
click to toggle source
# File lib/oauth2-client/connection.rb, line 178 def redirect_limit_reached? @redirect_count ||= 0 @redirect_count += 1 @redirect_count > @max_redirects end
ssl_cert_store()
click to toggle source
# File lib/oauth2-client/connection.rb, line 171 def ssl_cert_store return ssl[:cert_store] if ssl[:cert_store] cert_store = OpenSSL::X509::Store.new cert_store.set_default_paths cert_store end
ssl_verify_mode()
click to toggle source
# File lib/oauth2-client/connection.rb, line 163 def ssl_verify_mode if ssl.fetch(:verify, true) OpenSSL::SSL::VERIFY_PEER else OpenSSL::SSL::VERIFY_NONE end end