Class: Sysdig::Real
- Inherits:
-
Object
- Object
- Sysdig::Real
- Defined in:
- lib/sysdig/real.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#authentication ⇒ Object
readonly
Returns the value of attribute authentication.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Real
constructor
A new instance of Real.
- #request_with_authentication(options = {}) ⇒ Object (also: #request)
- #request_without_authentication(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sysdig/real.rb', line 5 def initialize(={}) @url = URI.parse([:url] || "https://app.sysdigcloud.com") adapter = [:adapter] || Faraday.default_adapter = [:connection_options] || {} logger, @logger = [:logger] || Logger.new(nil) @username, @password = *.values_at(:username, :password) unless @username && @password token = @token = .fetch(:token) end @authentication = Mutex.new @authenticated = false @connection = Faraday.new({url: @url}.merge()) do |builder| # response builder.response :json # request builder.request :retry, :max => 30, :interval => 1, :interval_randomness => 0.05, :backoff_factor => 2 builder.request :multipart builder.request :json if token builder. :Bearer, token else builder.use :cookie_jar end builder.use Faraday::Response::RaiseError builder.use Ey::Logger::Faraday, format: :machine, device: logger builder.adapter(*adapter) end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def adapter @adapter end |
#authentication ⇒ Object (readonly)
Returns the value of attribute authentication.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def authentication @authentication end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def connection @connection end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def logger @logger end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def parser @parser end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def path @path end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
3 4 5 |
# File 'lib/sysdig/real.rb', line 3 def url @url end |
Instance Method Details
#request_with_authentication(options = {}) ⇒ Object Also known as: request
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sysdig/real.rb', line 47 def request_with_authentication(={}) if !@authenticated && token.nil? @authentication.synchronize { if !@authenticated login(@username, @password) @authenticated = true end } end request_without_authentication() end |
#request_without_authentication(options = {}) ⇒ Object
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 |
# File 'lib/sysdig/real.rb', line 60 def request_without_authentication(={}) method = ([:method] || "get").to_s.downcase.to_sym url = URI.parse([:url] || File.join(@url.to_s, [:path] || "/")) params = [:params] || {} body = [:body] headers = [:headers] || {} headers["Content-Type"] ||= if body.nil? if !params.empty? "application/x-www-form-urlencoded" else # Rails infers a Content-Type and we must set it here for the canonical string to match "text/plain" end end response = @connection.send(method) do |req| req.url(url.to_s) req.headers.merge!(headers) req.params.merge!(params) req.body = body end Sysdig::Response.new( :status => response.status, :headers => response.headers, :body => response.body, :request => { :method => method, :url => url, :headers => headers, :body => body, :params => params, } ).raise! end |