Class: Wavefront::Paginator::Post
- Defined in:
- lib/wavefront-sdk/paginator/post.rb
Overview
We need to monkey-patch the Base class to pre-process data for a couple of methods.
Instance Attribute Summary
Attributes inherited from Base
#api_caller, #args, #conn, #initial_limit, #method, #page_size
Instance Method Summary collapse
-
#body_as(desired, args, index = 1) ⇒ Array
Faraday needs the body of the POST to be described as a JSON string, but our methods which modify the body for recursive calls need it as a hash.
-
#limit_and_offset(args) ⇒ Object
super#limit_and_offset requires a hash containing the limit and offset values.
-
#set_pagination(offset, limit, args) ⇒ Object
super#setpagination requires that the args are an array, and that one of the args is a hash containing limit and offset keys.
Methods inherited from Base
#initialize, #make_lazy_call, #make_recursive_call, #setup_vars, #user_page_size
Constructor Details
This class inherits a constructor from Wavefront::Paginator::Base
Instance Method Details
#body_as(desired, args, index = 1) ⇒ Array
Faraday needs the body of the POST to be described as a JSON string, but our methods which modify the body for recursive calls need it as a hash. This method takes an array of args and ensures the body element is either a string or an object. If the body cannot be turned into JSON, which some bodies can’t, return an empty array.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wavefront-sdk/paginator/post.rb', line 48 def body_as(desired, args, index = 1) body = args[index] return args if body.class == desired args[index] = if body.is_a?(String) JSON.parse(body, symbolize_names: true) else body.to_json end args rescue JSON::ParserError [] end |
#limit_and_offset(args) ⇒ Object
super#limit_and_offset requires a hash containing the limit and offset values. In a POST that’s in the body of the request, which at this point has been turned into a JSON string. We have to temporarily turn it back into an object and pass it up to the superclass.
The body is the second argument. We’ll allow for it already being an object, just in case.
30 31 32 |
# File 'lib/wavefront-sdk/paginator/post.rb', line 30 def limit_and_offset(args) super(body_as(Hash, args)) end |
#set_pagination(offset, limit, args) ⇒ Object
super#setpagination requires that the args are an array, and that one of the args is a hash containing limit and offset keys.
16 17 18 19 |
# File 'lib/wavefront-sdk/paginator/post.rb', line 16 def set_pagination(offset, limit, args) super(offset, limit, body_as(Hash, args)) body_as(String, args) end |