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

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
# File 'lib/pure_iterator/base.rb', line 12

def initialize(config)
  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]
  accept :xml
end

Instance Method Details

#accept(accept) ⇒ Object

Set the response Accept type


24
25
26
27
28
# File 'lib/pure_iterator/base.rb', line 24

def accept(accept)
  supported_accept = [:xml, :json]
  raise "Supported Accept values #{supported_accept}" unless supported_accept.include? accept
  @accept = accept
end

#count(params = {}) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pure_iterator/base.rb', line 49

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)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pure_iterator/base.rb', line 33

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