Class: Wavefront::Paginator::Post

Inherits:
Base
  • Object
show all
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

Methods inherited from Base

#finalize_response, #initialize, #make_lazy_call, #make_recursive_call, #set_limit_and_offset, #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.

Parameters:

  • desired (Class)

    String or Hash, what you want the class of the body element to be

  • args (Array)

    the arguments to the Faraday call method

Returns:



50
51
52
53
54
55
56
57
58
59
# File 'lib/wavefront-sdk/paginator/post.rb', line 50

def body_as(desired, args, index = 1)
  body = args[index]

  return args if body.instance_of?(desired)

  args[index] = body_as_json(body)
  args
rescue JSON::ParserError
  []
end

#body_as_json(body) ⇒ Object



61
62
63
64
65
# File 'lib/wavefront-sdk/paginator/post.rb', line 61

def body_as_json(body)
  return body.to_json unless body.is_a?(String)

  JSON.parse(body, symbolize_names: true)
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.



32
33
34
# File 'lib/wavefront-sdk/paginator/post.rb', line 32

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.



18
19
20
21
# File 'lib/wavefront-sdk/paginator/post.rb', line 18

def set_pagination(offset, limit, args)
  super(offset, limit, body_as(Hash, args))
  body_as(String, args)
end