Reputation: 39
When I try to run the following program, the object filter doesn't seem to work. I still get invoices which don't meet the filter criteria. Can someone help me understand what I am doing wrong? Unfortunately there is practically no documentation I could find on using object filters in Ruby
Thanks in advance
require 'rubygems'
require 'softlayer_api'
require 'pp'
begin
date = DateTime.new(2015,11,1)
account_service = SoftLayer::Service.new("SoftLayer_Account",:username => "USER", :api_key => "KEY", :timeout => 999)
latest_invoices = account_service.result_limit(0,10).object_mask("mask[id]").getInvoices(filter={'invoices'=> {'createDate'=> {'>='=> date}}})
pp latest_invoices
rescue Exception => exception
puts "Unable to retrieve the invoice #{exception}"
end
Upvotes: 0
Views: 382
Reputation: 1532
Try this ruby example to filter items using Dates:
require 'rubygems'
require 'softlayer_api'
# Your SoftLayer API username.
SL_API_USERNAME = 'set me'
# Your SoftLayer API key.
SL_API_KEY = 'set me'
account_service = SoftLayer::Service.new('SoftLayer_Account',
:username => SL_API_USERNAME,
:api_key => SL_API_KEY)
begin
object_filter = SoftLayer::ObjectFilter.new
object_filter.set_criteria_for_key_path('invoices.createDate',
'operation' => 'betweenDate',
'options' => [{
'name' => 'startDate',
'value' => ["02/01/2014 0:0:0"]
},
{
'name' => 'endDate',
'value' => ["02/13/2014 0:0:0"]
}
]
)
result = account_service.object_filter(object_filter).getInvoices
puts result.inspect
rescue => e
$stdout.print(e.inspect)
end
Some references:
https://github.com/softlayer/softlayer-ruby/blob/master/lib/softlayer/ObjectFilter.rb
https://www.omniref.com/ruby/gems/softlayer_api/3.0.b1/symbols/SoftLayer::ObjectFilter/set_criteria_for_key_path
https://coveralls.io/files/239537934
http://www.rubydoc.info/github/softlayer/softlayer-api-ruby-client/SoftLayer%2FObjectFilter%3Aset_criteria_for_key_path
https://github.com/softlayer/softlayer-ruby/issues/77
If you are still using the old filter, you can try the below example (But I recommend to update the Softlayer ruby client and ruby versions):
require 'rubygems'
require 'softlayer_api'
$SL_API_USERNAME = 'set me';
$SL_API_KEY = 'set me'
account_service = SoftLayer::Service.new('SoftLayer_Account',
:username => $SL_API_USERNAME,
:api_key => $SL_API_KEY)
begin
filter_instance = {
'invoices'=> {
'createDate'=> {
'operation'=> 'betweenDate',
'options'=> [
{
'name'=> 'startDate',
'value'=> [
'02/01/2014 0:0:0'
]
},
{
'name'=> 'endDate',
'value'=> [
'02/13/2014 0:0:0'
]
}
]
}
}
}
result = account_service.object_filter(filter_instance).getInvoices
puts result.inspect
rescue => e
$stdout.print(e.inspect)
end
I you want to use more filters criteria in your current filter, here is an example:
filter_instance = {
'invoices'=> {
'createDate'=> {
'operation'=> 'betweenDate',
'options'=> [
{
'name'=> 'startDate',
'value'=> [
'11/01/2015 0:0:0'
]
},
{
'name'=> 'endDate',
'value'=> [
'11/30/2015 23:59:59'
]
}
]
},
'typeCode'=> {
'operation'=> 'RECURRING'
}
}
}
Upvotes: 0