0

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
Saeid
  • 4,147
  • 7
  • 27
  • 43
haunm
  • 39
  • 6
  • What version of Ruby are you using? We haven't had to use `require 'rubygems'` since Ruby 1.9. Also, don't catch generic `Exception`, catch specific Exceptions. There are lots of pages out there about why but this is a good start: http://stackoverflow.com/q/10048173/128421 – the Tin Man Dec 09 '15 at 17:51

1 Answers1

0

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'
          }
      }
  }
mcruz
  • 1,534
  • 2
  • 11
  • 14
  • mcruz, do you know why I get multiple invoices per day when I run this? I got a lot more results than I expected. Thank you. – haunm Dec 09 '15 at 20:06
  • The amount of items should not be affected when using the filters. I executed some tests using a short range and it is working well. I suggest you to reduce the range of date in order to see if the amount of items being affected. for example my range of dates are: startDate = '02/04/2014 0:0:0' and the endDate = '02/04/2014 23:59:59' (1 day) and verify if are the same values using this script and using only "getInvoices" without filters. Regards. – mcruz Dec 09 '15 at 20:33
  • Thank you again. However, I may have been too vague in my question. I made the call with the filter you showed but I'm getting multiple invoices per day. Either with a typeCode of "NEW" or "ONE-TIME-CHARGE". I'm not sure I understand why I'm seeing all of these. Is there some place this is document with more detail? Honestly, I was just expecting one invoice per month. Thank you again! – haunm Dec 09 '15 at 22:38
  • I was reviewing some filters via portal; I can see some cases that the filter retrieves more than one item in a range of 1 month. The case that the filter retrieves 1 item per month is when we select typeCode =“recurring”. Is this item with typeCode =“recurring” that you want to get? – mcruz Dec 10 '15 at 18:06
  • @haunm, I added other filter criteria in the previous filter (typeCode = "RECURRING"): – mcruz Dec 10 '15 at 21:07