Class: OAuth2::HttpConnection
- Inherits:
-
Object
- Object
- OAuth2::HttpConnection
- Defined in:
- lib/oauth2/connection.rb
Defined Under Namespace
Classes: UnhandledHTTPMethodError, UnsupportedSchemeError
Constant Summary collapse
- NET_HTTP_EXCEPTIONS =
[ EOFError, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EINVAL, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError, Zlib::GzipFile::Error, ]
Instance Attribute Summary collapse
-
#accept ⇒ Object
Returns the value of attribute accept.
-
#config ⇒ Object
Returns the value of attribute config.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
-
#port ⇒ Object
Returns the value of attribute port.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_url(path = '') ⇒ Object
- #default_headers ⇒ Object
- #http_connection(opts = {}) ⇒ Object
-
#initialize(url, options = {}) ⇒ HttpConnection
constructor
A new instance of HttpConnection.
- #send_request(method, path, opts = {}) ⇒ Object
- #ssl? ⇒ Boolean
Constructor Details
#initialize(url, options = {}) ⇒ HttpConnection
Returns a new instance of HttpConnection.
43 44 45 46 47 48 |
# File 'lib/oauth2/connection.rb', line 43 def initialize(url, ={}) @uri = Addressable::URI.parse(url) self.class..keys.each do |key| instance_variable_set(:"@#{key}", .fetch(key, self.class.[key])) end end |
Instance Attribute Details
#accept ⇒ Object
Returns the value of attribute accept.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def accept @accept end |
#config ⇒ Object
Returns the value of attribute config.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def config @config end |
#headers ⇒ Object
Returns the value of attribute headers.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def host @host end |
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def max_redirects @max_redirects end |
#port ⇒ Object
Returns the value of attribute port.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def scheme @scheme end |
#ssl ⇒ Object
Returns the value of attribute ssl.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def ssl @ssl end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
29 30 31 |
# File 'lib/oauth2/connection.rb', line 29 def user_agent @user_agent end |
Class Method Details
Instance Method Details
#absolute_url(path = '') ⇒ Object
74 75 76 |
# File 'lib/oauth2/connection.rb', line 74 def absolute_url(path='') "#{scheme}://#{host}#{path}" end |
#default_headers ⇒ Object
50 51 52 |
# File 'lib/oauth2/connection.rb', line 50 def default_headers self.class.[:headers] end |
#http_connection(opts = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/oauth2/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 |
#send_request(method, path, opts = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/oauth2/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? ⇒ Boolean
78 79 80 |
# File 'lib/oauth2/connection.rb', line 78 def ssl? scheme == "https" ? true : false end |