ruby_odata icon indicating copy to clipboard operation
ruby_odata copied to clipboard

Collections with dash in the name

Open kukkuk opened this issue 9 years ago • 1 comments

First of all, thank you for creating this gem. It's really useful.

I'm attempting to work with an OData source that has dashes in the collections names:

<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="https://netstockaustraliaptyltd-demo.myobadvanced.com/odata/">
  <workspace>
    <atom:title type="text">Default</atom:title>
    <collection href="BI-Customers">
      <atom:title type="text">BI-Customers</atom:title>
    </collection>
    <collection href="BI-Employees">
      <atom:title type="text">BI-Employees</atom:title>
    </collection>
    <collection href="BI-Opportunities">
      <atom:title type="text">BI-Opportunities</atom:title>
    </collection>
    <collection href="BI-Dates">
      <atom:title type="text">BI-Dates</atom:title>
    </collection>
  </workspace>
</service>

When I try to instantiate a service I get this error:

NameError: wrong constant name BI-Customers
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/ruby_odata-0.1.6/lib/ruby_odata/class_builder.rb:53:in `const_set'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/ruby_odata-0.1.6/lib/ruby_odata/class_builder.rb:53:in `build'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/ruby_odata-0.1.6/lib/ruby_odata/service.rb:281:in `block in build_collections_and_classes'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:187:in `block in each'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:in `upto'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:in `each'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/ruby_odata-0.1.6/lib/ruby_odata/service.rb:276:in `build_collections_and_classes'
    from /home/barry/.rvm/gems/ruby-2.2.2/gems/ruby_odata-0.1.6/lib/ruby_odata/service.rb:20:in `initialize'
    from (irb):2:in `new'
    from (irb):2
    from /home/barry/.rvm/rubies/ruby-2.2.2/bin/irb:11:in `<main>'

I tried making a small change in lib/ruby_odata/service.rb line 349:

  def qualify_class_name(klass_name)
    unless @namespace.nil? || @namespace.blank? || klass_name.include?('::')
      namespaces = @namespace.split(/\.|::/)
      namespaces << klass_name
      klass_name = namespaces.join '::'
    end
    klass_name.camelize.gsub('-','') #### I changed this
  end

Now I can instantiate the service, and select the collection:

svc = OData::Service.new "https://netstockaustraliaptyltd-demo.myobadvanced.com/odata", :username => '[email protected]', :password => 'myob(*&6'
svc.send "BI-Customers"
#<OData::QueryBuilder:0x00000002b4b200 @root="/BI-Customers", @expands=[], @filters=[], @order_bys=[], @navigation_paths=[], @select=[], @skip=nil, @top=nil, @count=nil, @links_navigation_property=nil, @additional_params={}>

It would be nice if I could simply write svc.BICustomers instead of svc.send "BI-Customers"

Any ideas how I can make that heppen?

Thanks so much

Barry

kukkuk avatar Apr 18 '16 08:04 kukkuk

Is everything prefixed the same? If so you could probably add an option for model prefix (or something like that) and have the QueryBuilder add that prefix when it creates the queries. Then you could just do svc.Customers

visoft avatar Apr 19 '16 19:04 visoft