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 |
# 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} = 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)
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} = 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 |