class PureIterator::Base

Public Class Methods

new(config) click to toggle source

@param config [Hash] @option config [String] :host Pure host @option config [String] :username Username of the Pure host account @option config [String] :password Password of the Pure host account @option config [String] :api_key API key of the Pure host account @option config [Integer] :api_version Pure API version

# File lib/pure_iterator/base.rb, line 12
def initialize(config)
  required = [:host, :username, :password, :api_key, :api_version]
  required.each do |r|
    raise ArgumentError, "Pure #{r} not set" unless config[r] && !config[r].to_s.empty?
  end
  http_client = HTTP::Client.new
  http_client = http_client.headers({ 'api-key' => config[:api_key] })
  http_client = http_client.basic_auth({user: config[:username], pass: config[:password]})
  @http_client = http_client
  @host = config[:host]
  @api_version = config[:api_version].to_s
  accept :xml
end

Public Instance Methods

accept(accept) click to toggle source

Set the response Accept type @param accept [Symbol]

# File lib/pure_iterator/base.rb, line 28
def accept(accept)
  supported_accept = [:xml, :json]
  raise "Supported Accept values #{supported_accept}" unless supported_accept.include? accept
  @accept = accept
end
count(params = {}) click to toggle source
# File lib/pure_iterator/base.rb, line 53
def count(params = {})
  @http_client = @http_client.headers({ 'Accept' => "application/xml" })
  default_count_params = {size: 0}
  count_options = params.dup
  count_options.delete :size
  count_options.delete :page
  count_options.delete :pageSize
  response = @http_client.post url, json: default_count_params.merge(count_options)
  if response.code === 200
    count_from_xml response
  else
    raise response
  end
end
iterate(params = {}) click to toggle source

Traverse a collection, doing something with each response (which may contain multiple records if size parameter used) @param params [Hash] Pure POST parameters (except page and pageSize) @return [String] 'done' when collection has been traversed

# File lib/pure_iterator/base.rb, line 37
def iterate(params = {})
  query_record_count = count(params)
  @http_client = @http_client.headers({ 'Accept' => "application/#{@accept}" })
  default_query_params = {size: 1, offset: 0}
  query_options = params.dup
  query_options.delete :page
  query_options.delete :pageSize
  query_params = default_query_params.merge(query_options)
  while query_params[:offset] < query_record_count
    response = @http_client.post url, json: query_params
    act response
    query_params[:offset] += query_params[:size]
  end
  'done'
end

Private Instance Methods

act(response) click to toggle source

@param response [HTTP::Response]

# File lib/pure_iterator/base.rb, line 86
def act(response)
  raise "#{self.class.name}##{__method__} not implemented"
end
count_from_xml(xml) click to toggle source
# File lib/pure_iterator/base.rb, line 74
def count_from_xml(xml)
  doc = Nokogiri::XML xml
  doc.remove_namespaces!
  doc.xpath('/result/count').text.to_i
end
post_endpoint() click to toggle source

@return [String] Pure POST endpoint

# File lib/pure_iterator/base.rb, line 81
def post_endpoint
  raise "#{self.class.name}##{__method__} not implemented"
end
url() click to toggle source
# File lib/pure_iterator/base.rb, line 70
def url
  File.join 'https://', @host, 'ws', 'api', @api_version, post_endpoint
end