Class: PureIterator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pure_iterator/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.

Parameters:

  • config (Hash)

Options Hash (config):

  • :host (String)

    Pure host

  • :username (String)

    Username of the Pure host account

  • :password (String)

    Password of the Pure host account

  • :api_key (String)

    API key of the Pure host account

  • :api_version (Integer)

    Pure API version



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#accept(accept) ⇒ Object

Set the response Accept type

Parameters:

  • accept (Symbol)


28
29
30
31
32
# 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 = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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 = {}) ⇒ String

Traverse a collection, doing something with each response (which may contain multiple records if size parameter used)

Parameters:

  • params (Hash) (defaults to: {})

    Pure POST parameters (except page and pageSize)

Returns:

  • (String)

    ‘done’ when collection has been traversed



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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