Class: DynoscaleRuby::ApiWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/dynoscale_ruby/api_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(dyno, url, app_name) ⇒ ApiWrapper

Returns a new instance of ApiWrapper.



8
9
10
11
12
# File 'lib/dynoscale_ruby/api_wrapper.rb', line 8

def initialize(dyno, url, app_name)
  @dyno     = dyno
  @url      = URI(url)
  @app_name = app_name
end

Instance Method Details

#publish_reports(reports, current_time = Time.now, http = Net::HTTP.new(@url.host, @url.port), &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dynoscale_ruby/api_wrapper.rb', line 14

def publish_reports(reports, current_time = Time.now, http = Net::HTTP.new(@url.host, @url.port), &block)
  headers = { "Content-Type": "text/csv",
              "User-Agent": "dynoscale-ruby;#{Gem.loaded_specs["dynoscale_ruby"].version.to_s}",
              "X_REQUEST_START": "t=#{current_time.to_i}",
              "X_DYNO": @dyno,
              "X_APP_NAME": @app_name
            }

  body = reports.reduce(""){|t, r| "#{t}#{r.to_csv}"}

  begin
    response = request(http, headers, body)
  rescue Timeout::Error, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    # ignore and let the retry mechanism handle it
  end

  success = (response&.code&.to_i >= 200 && response&.code&.to_i < 300) || false


  config = JSON.parse(response&.body || "{}")["config"] || {}
  published_reports = success ? reports : []

  block.call(success, published_reports, config)
end