class LaunchDarkly::Requestor

@private

Constants

CacheEntry

Public Class Methods

new(sdk_key, config) click to toggle source
# File lib/ldclient-rb/requestor.rb, line 25
def initialize(sdk_key, config)
  @sdk_key = sdk_key
  @config = config
  @http_client = LaunchDarkly::Util.new_http_client(config.base_uri, config)
  @cache = @config.cache_store
end

Public Instance Methods

request_all_data() click to toggle source
# File lib/ldclient-rb/requestor.rb, line 32
def request_all_data()
  all_data = JSON.parse(make_request("/sdk/latest-all"), symbolize_names: true)
  Impl::Model.make_all_store_data(all_data)
end
stop() click to toggle source
# File lib/ldclient-rb/requestor.rb, line 37
def stop
  begin
    @http_client.close
  rescue
  end
end

Private Instance Methods

fix_encoding(body, content_type) click to toggle source
# File lib/ldclient-rb/requestor.rb, line 80
def fix_encoding(body, content_type)
  return body if content_type.nil?
  media_type, charset = parse_content_type(content_type)
  return body if charset.nil?
  body.force_encoding(Encoding::find(charset)).encode(Encoding::UTF_8)
end
make_request(path) click to toggle source
# File lib/ldclient-rb/requestor.rb, line 50
def make_request(path)
  uri = URI(@config.base_uri + path)
  headers = {}
  Impl::Util.default_http_headers(@sdk_key, @config).each { |k, v| headers[k] = v }
  headers["Connection"] = "keep-alive"
  cached = @cache.read(uri)
  if !cached.nil?
    headers["If-None-Match"] = cached.etag
  end
  response = @http_client.request("GET", uri, {
    headers: headers
  })
  status = response.status.code
  # must fully read body for persistent connections
  body = response.to_s
  @config.logger.debug { "[LDClient] Got response from uri: #{uri}\n\tstatus code: #{status}\n\theaders: #{response.headers.to_h}\n\tbody: #{body}" }
  if status == 304 && !cached.nil?
    body = cached.body
  else
    @cache.delete(uri)
    if status < 200 || status >= 300
      raise UnexpectedResponseError.new(status)
    end
    body = fix_encoding(body, response.headers["content-type"])
    etag = response.headers["etag"]
    @cache.write(uri, CacheEntry.new(etag, body)) if !etag.nil?
  end
  body
end
parse_content_type(value) click to toggle source
# File lib/ldclient-rb/requestor.rb, line 87
def parse_content_type(value)
  return [nil, nil] if value.nil? || value == ''
  parts = value.split(/; */)
  return [value, nil] if parts.count < 2
  charset = nil
  parts.each do |part|
    fields = part.split('=')
    if fields.count >= 2 && fields[0] == 'charset'
      charset = fields[1]
      break
    end
  end
  return [parts[0], charset]
end
request_single_item(kind, path) click to toggle source
# File lib/ldclient-rb/requestor.rb, line 46
def request_single_item(kind, path)
  Impl::Model.deserialize(kind, make_request(path))
end