Class: PureIterator::Base
- Inherits:
-
Object
- Object
- PureIterator::Base
- Defined in:
- lib/pure_iterator/base.rb
Instance Method Summary collapse
-
#accept(accept) ⇒ Object
Set the response Accept type.
- #count(params = {}) ⇒ Object
-
#initialize(config) ⇒ Base
constructor
A new instance of Base.
-
#iterate(params = {}) ⇒ String
Traverse a collection, doing something with each response (which may contain multiple records if size parameter used).
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
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
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} = params.dup .delete :size .delete :page .delete :pageSize response = @http_client.post url, json: default_count_params.merge() 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)
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} = params.dup .delete :page .delete :pageSize query_params = default_query_params.merge() 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 |