在SoftLayer Ruby API中使用对象过滤器 [英] Using object filters with SoftLayer Ruby API

查看:40
本文介绍了在SoftLayer Ruby API中使用对象过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行以下程序时,对象过滤器似乎不起作用.我仍然收到不符合过滤条件的发票.有人可以帮助我了解我在做什么错吗?不幸的是,我几乎找不到关于在Ruby中使用对象过滤器的文档

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

预先感谢

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

推荐答案

尝试以下ruby示例以使用日期过滤项目:

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

一些参考文献:

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

如果您仍在使用旧过滤器,则可以尝试以下示例(但我建议更新Softlayer ruby​​客户端和ruby版本):

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'
          }
      }
  }

这篇关于在SoftLayer Ruby API中使用对象过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆