Class: CFoundry::BaseClient
- Inherits:
-
Object
- Object
- CFoundry::BaseClient
- Defined in:
- lib/cfoundry/baseclient.rb
Overview
:nodoc:
Constant Summary collapse
- LOG_LENGTH =
10
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
Returns the value of attribute backtrace.
-
#log ⇒ Object
Returns the value of attribute log.
-
#trace ⇒ Object
Returns the value of attribute trace.
Instance Method Summary collapse
-
#initialize(target, token = nil) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #request(method, path, options = {}) ⇒ Object
- #request_path(method, path, options = {}) ⇒ Object
- #request_uri(uri, method, options = {}) ⇒ Object
-
#token_data ⇒ Object
grab the metadata from a token that looks like:.
Constructor Details
#initialize(target, token = nil) ⇒ BaseClient
Returns a new instance of BaseClient.
13 14 15 16 17 18 19 |
# File 'lib/cfoundry/baseclient.rb', line 13 def initialize(target, token = nil) @target = target @token = token @trace = false @backtrace = false @log = false end |
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
11 12 13 |
# File 'lib/cfoundry/baseclient.rb', line 11 def backtrace @backtrace end |
#log ⇒ Object
Returns the value of attribute log.
11 12 13 |
# File 'lib/cfoundry/baseclient.rb', line 11 def log @log end |
#trace ⇒ Object
Returns the value of attribute trace.
11 12 13 |
# File 'lib/cfoundry/baseclient.rb', line 11 def trace @trace end |
Instance Method Details
#request(method, path, options = {}) ⇒ Object
40 41 42 |
# File 'lib/cfoundry/baseclient.rb', line 40 def request(method, path, = {}) request_uri(URI.parse(@target + path), method, ) end |
#request_path(method, path, options = {}) ⇒ Object
34 35 36 37 38 |
# File 'lib/cfoundry/baseclient.rb', line 34 def request_path(method, path, = {}) path = url(path) if path.is_a?(Array) request(method, path, ) end |
#request_uri(uri, method, options = {}) ⇒ Object
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 |
# File 'lib/cfoundry/baseclient.rb', line 44 def request_uri(uri, method, = {}) uri = URI.parse(@target + uri.to_s) unless uri.host # keep original options in case there's a redirect to follow = .dup accept = .delete(:accept) content = .delete(:content) payload = .delete(:payload) params = .delete(:params) return_headers = .delete(:return_headers) return_response = .delete(:return_response) headers = {} headers["Authorization"] = @token if @token headers["Proxy-User"] = @proxy if @proxy if accept_type = mimetype(accept) headers["Accept"] = accept_type end if content_type = mimetype(content) headers["Content-Type"] = content_type end unless payload.is_a?(String) case content when :json payload = MultiJson.dump(payload) when :form payload = encode_params(payload) end end if payload.is_a?(String) headers["Content-Length"] = payload.size elsif !payload headers["Content-Length"] = 0 end headers.merge!([:headers]) if [:headers] if params if uri.query uri.query += "&" + encode_params(params) else uri.query = encode_params(params) end end if payload && payload.is_a?(Hash) multipart = method.const_get(:Multipart) request = multipart.new(uri.request_uri, payload) else request = method.new(uri.request_uri) request.body = payload if payload end request["Authorization"] = @token if @token request["Proxy-User"] = @proxy if @proxy headers.each do |k, v| request[k] = v end # TODO: test http proxies http = Net::HTTP.new(uri.host, uri.port) if uri.is_a?(URI::HTTPS) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end print_request(request) if @trace before = Time.now http.start do response = http.request(request) time = Time.now - before print_response(response) if @trace print_backtrace(caller) if @trace log_request(time, request, response) if return_headers sane_headers(response) elsif return_response response elsif response.is_a?(Net::HTTPRedirection) request_uri( URI.parse(response["location"]), Net::HTTP::Get, ) else handle_response(response, accept) end end rescue SocketError, Errno::ECONNREFUSED => e raise TargetRefused, e. end |
#token_data ⇒ Object
grab the metadata from a token that looks like:
bearer (base64 …)
24 25 26 27 28 29 30 31 32 |
# File 'lib/cfoundry/baseclient.rb', line 24 def token_data tok = Base64.decode64(@token.sub(/^bearer\s+/, "")) tok.sub!(/\{.+?\}/, "") # clear algo MultiJson.load(tok[/\{.+?\}/], :symbolize_keys => true) # can't expect all tokens to be the proper format rescue MultiJson::DecodeError {} end |