Class: Sysdig::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/sysdig/real.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @url = URI.parse(options[:url] || "https://app.sysdigcloud.com")

  adapter            = options[:adapter]            || Faraday.default_adapter
  connection_options = options[:connection_options] || {}
  logger, @logger    = options[:logger]             || Logger.new(nil)

  @username, @password = *options.values_at(:username, :password)

  unless @username && @password
    token = @token = options.fetch(:token)
  end

  @authentication = Mutex.new
  @authenticated  = false

  @connection = Faraday.new({url: @url}.merge(connection_options)) 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.authorization :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

#adapterObject (readonly)

Returns the value of attribute adapter.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def adapter
  @adapter
end

#authenticationObject (readonly)

Returns the value of attribute authentication.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def authentication
  @authentication
end

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def connection
  @connection
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def logger
  @logger
end

#parserObject (readonly)

Returns the value of attribute parser.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def parser
  @parser
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def path
  @path
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/sysdig/real.rb', line 3

def token
  @token
end

#urlObject (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(options={})
  if !@authenticated && token.nil?
    @authentication.synchronize {
      if !@authenticated
        (@username, @password)
        @authenticated = true
      end
    }
  end

  request_without_authentication(options)
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(options={})
  method  = (options[:method] || "get").to_s.downcase.to_sym
  url     = URI.parse(options[:url] || File.join(@url.to_s, options[:path] || "/"))
  params  = options[:params] || {}
  body    = options[:body]
  headers = options[: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