Skip to content

Instantly share code, notes, and snippets.

@Gerg
Last active August 5, 2020 21:53
Show Gist options
  • Save Gerg/c158a8d9aa097a7278f28b4750b17993 to your computer and use it in GitHub Desktop.
Save Gerg/c158a8d9aa097a7278f28b4750b17993 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'date'
require 'open3'
class Main
class << self
def resources
%w[apps]
end
def per_page
5
end
def operators
%w[lt gt lte gte]
end
def operator_map(operator)
{
'lt' => :<,
'gt' => :>,
'lte' => :<=,
'gte' => :>=,
}[operator]
end
def filters
%w[created_ats updated_ats]
end
def filters_map(filter)
{
'created_ats' => 'created_at',
'updated_ats' => 'updated_at',
}[filter]
end
def main
resources.each do |resource|
filters.each do |filter|
puts "Testing #{filter} filter for #{resource}.\n\n"
field = filters_map(filter)
request = "/v3/#{resource}?per_page=#{per_page}&order_by=#{field}"
parsed_response = make_request(request, field)
actual_resources = parsed_response.length
raise "Expected #{per_page} resources, got #{actual_resources}" unless actual_resources == per_page
pivot = parsed_response[2]
operators.each do |operator|
filtered_resources = filter_request(resource, operator, pivot, filter)
raise "Expected #{filtered_resources} to not be empty" unless filtered_resources.length
filtered_resources.each do |filtered_resource|
mathmatical_operator = operator_map(operator)
raise "Expected #{filtered_resource} to be #{operator} #{pivot}" unless DateTime.parse(filtered_resource).public_send(mathmatical_operator, DateTime.parse(pivot))
end
end
end
end
end
def filter_request(resource, operator, pivot, filter)
field = filters_map(filter)
request = "/v3/#{resource}?per_page=#{per_page}&order_by=#{field}&#{filter}[#{operator}]=#{pivot}"
make_request(request, field)
end
def make_request(request, field)
p request
response, status = Open3.capture2("cf curl '#{request}' | jq .resources[].#{field}")
raise response unless status == 0
parsed_response = response.split("\n").map { |string| string.tr('\"','') }
p parsed_response
puts "\n"
parsed_response
end
end
end
Main.main
# Copyright 2020 Greg Cobb
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment