Class: OAuth2::HttpConnection

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, 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

Instance Attribute Details

#acceptObject

Returns the value of attribute accept.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def accept
  @accept
end

#configObject

Returns the value of attribute config.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def config
  @config
end

#headersObject

Returns the value of attribute headers.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def host
  @host
end

#max_redirectsObject

Returns the value of attribute max_redirects.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def max_redirects
  @max_redirects
end

#portObject

Returns the value of attribute port.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def scheme
  @scheme
end

#sslObject

Returns the value of attribute ssl.



29
30
31
# File 'lib/oauth2/connection.rb', line 29

def ssl
  @ssl
end

#user_agentObject

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

.default_optionsObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/oauth2/connection.rb', line 32

def self.default_options
  {
    :headers => {
      'Accept'     => 'application/json',
      'User-Agent' => "OAuth2 Ruby Gem #{OAuth2::Version}"
    },
    :ssl => {:verify => true},
    :max_redirects => 5
  }
end

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_headersObject



50
51
52
# File 'lib/oauth2/connection.rb', line 50

def default_headers
  self.class.default_options[: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

Returns:

  • (Boolean)


78
79
80
# File 'lib/oauth2/connection.rb', line 78

def ssl?
  scheme == "https" ? true : false
end