Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections with dash in the name #64

Open
kukkuk opened this issue Apr 18, 2016 · 1 comment
Open

Collections with dash in the name #64

kukkuk opened this issue Apr 18, 2016 · 1 comment

Comments

@kukkuk
Copy link

kukkuk commented Apr 18, 2016

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

@visoft
Copy link
Owner

visoft commented Apr 19, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants