class PullReview::Coverage::ClientApi

Remote api based implementation for production intent

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/pullreview/coverage/client_api.rb, line 30
def initialize(config)
  @config = config || Config.new
end

Public Instance Methods

publish(payload) click to toggle source

post json payload to pullreview host.

# File lib/pullreview/coverage/client_api.rb, line 35
def publish(payload)
  allow_pullreview_webmock
  allow_pullreview_vcr
  response = post(payload)
  "#{response.code} : #{response.body}"
end

Private Instance Methods

allow_pullreview_vcr() click to toggle source

white list the pullreview host for vcr

# File lib/pullreview/coverage/client_api.rb, line 99
def allow_pullreview_vcr
  if defined?(VCR)
    VCR.send(VCR.version.major < 2 ? :config : :configure) do |c|
      c.ignore_hosts(config.api_host)
    end
  end
end
allow_pullreview_webmock() click to toggle source

white list the pullreview host for webmock

# File lib/pullreview/coverage/client_api.rb, line 92
def allow_pullreview_webmock
  return unless defined?(WebMock)
  allow = WebMock::Config.instance.allow || []
  WebMock::Config.instance.allow = [*allow].push(config.api_host) if allow
end
compress(str) click to toggle source

return gzipped str

# File lib/pullreview/coverage/client_api.rb, line 67
def compress(str)
  sio = StringIO.new('w')
  # don't use the block notation for StringIO
  # https://bugs.ruby-lang.org/issues/8935
  gz = Zlib::GzipWriter.new(sio)
  gz.write(str)
  gz.close
  sio.string
end
http(uri) click to toggle source

Return an Net::HTTP configured with timeout and https certificate verification if necessary

# File lib/pullreview/coverage/client_api.rb, line 78
def http(uri)
  Net::HTTP.new(uri.host, uri.port).tap do |http|
    if uri.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = Certifi.where
      http.verify_depth = 5
    end
    http.open_timeout = config.api_open_timeout_in_seconds
    http.read_timeout = config.api_read_timeout_in_seconds
  end
end
post(payload) click to toggle source

post the payload and return exception if error occured.

# File lib/pullreview/coverage/client_api.rb, line 45
def post(payload)
  response = http(config.api_uri).request(zipped_post(config.api_uri, payload))
  if response.code.to_i >= 200 && response.code.to_i < 300
    response
  else
    raise "HTTP Error: #{response.code} #{config.api_uri}"
  end
end
to_s() click to toggle source
# File lib/pullreview/coverage/client_api.rb, line 107
def to_s
  "ClientApi : #{config.api_uri}"
end
zipped_post(uri, payload) click to toggle source

return Net::HTTP::Post with gzipped json payload

# File lib/pullreview/coverage/client_api.rb, line 55
def zipped_post(uri, payload)
  request = Net::HTTP::Post.new(uri.path)
  request['User-Agent'] = config.user_agent
  request['X-Repo-Token'] = config.repo_token
  request['Content-Type'] = 'application/json'
  request['Content-Encoding'] = 'gzip'
  request.body = compress(payload.to_json)

  request
end