Skip to content
This repository

OData Consumer for Ruby

README.rdoc

ruby_odata

The Open Data Protocol (OData) is a fantastic way to query and update data over standard Web technologies. The ruby_odata library acts as a consumer of OData services.

Resources

Installation

You can install ruby_odata as a gem using:

gem install ruby_odata

Usage

Adding

When you point at a service, an AddTo<EntityName> method is created for you. This method takes in the new entity to create. To commit the change, you need to call the save_changes method on the service. To add a new category for example, you would simply do the following:

require 'ruby_odata'

svc = OData::Service.new "http://127.0.0.1:8888/SampleService/Entities.svc"
new_category = Category.new
new_category.Name = "Sample Category"
svc.AddToCategories(new_category)
category = svc.save_changes
puts category.to_json

Updating

To update an object, simply pass the modified object to the update_object method on the service. Updating, like adding requires you to call save_changes in order to persist the change. For example:

require 'ruby_odata'

svc = OData::Service.new "http://127.0.0.1:8888/SampleService/Entities.svc"
new_category = Category.new
new_category.Name = "Sample Category"
svc.AddToCategories(new_category)
category = svc.save_changes
puts category.to_json

category.Name = 'Updated Category'
svc.update_object(category)
result = svc.save_changes
puts "Was the category updated? #{result}"

Deleting

Deleting an object involves passing the tracked object to the delete_object method on the service. Deleting is another function that involves the save_changes method (to commit the change back to the server). In this example, we'll add a category and then delete it.

require 'ruby_odata'

svc = OData::Service.new "http://127.0.0.1:8888/SampleService/Entities.svc"
new_category = Category.new
new_category.Name = "Sample Category"
svc.AddToCategories(new_category)
category = svc.save_changes
puts category.to_json

svc.delete_object(category)
result = svc.save_changes
puts "Was the category deleted? #{result}"

Querying

Querying is easy, for example to pull all the categories from the SampleService, you simply can run:

require 'ruby_odata'

svc = OData::Service.new "http://127.0.0.1:8888/SampleService/Entities.svc"
svc.Categories
categories = svc.execute
puts categories.to_json

You can also expand, add filters, order, skip records, and take only the top X records to the query before executing it. For example:

Expanding

Expanding allows you to eagerly load other objects that are children of the root. You can use more than one expand on a query. For expanding grandchild and lower entities, you must pass in the full path from the root, for example +Products.expand('Orders').expand('Orders/LineItems')+

# Without expanding the query
svc.Products(1)
prod1 = svc.execute
puts "Without expanding the query"
puts "#{prod1.to_json}\n"

# With expanding the query
svc.Products(1).expand('Category')
prod1 = svc.execute
puts "Without expanding the query"
puts "#{prod1.to_json}\n"

Filtering

The syntax for filtering can be found on the OData Protocol URI Conventions page. You can use more than one filter, if you call the filter method multiple times it will before an AND.

# You can access by ID (but that isn't is a filter)
# The syntax is just svc.ENTITYNAME(ID) which is shown in the expanding examples above

svc.Products.filter("Name eq 'Product 2'")
prod = svc.execute
puts "Filtering on Name eq 'Product 2'"
puts "#{prod.to_json}"

Note you can pass more than one filter in the string, for example (querying Netflix):

svc.Titles.filter("Rating eq 'PG' and ReleaseYear eq 1980")

Filters can also be chained, by doing this you will create an “and” filter (just like the last example) when it is passed to the server.

svc.Titles.filter("Rating eq 'PG'").filter("ReleaseYear eq 1980")

Combining Expanding and Filtering

The query operations follow a fluent interface, although they can be added by themselves as well as chained

svc.Products.filter("Name eq 'Product 2'").expand("Category")
prod = svc.execute
puts "Filtering on Name eq 'Product 2' and expanding"
puts "#{prod.to_json}"

Order By

You can order the results by properties of your choice, either ascending or descending. Order by are similar to +expand+s in that you can use more than one of them on a query. For expanding grandchild and lower entities, you must pass in the full path from the root like would do on an expand

svc.Products.order_by("Name")
products = svc.execute

# Specifically requesting descending
svc.Products.order_by("Name desc")
products = svc.execute

# Specifically requesting ascending
svc.Products.order_by("Name asc")
products = svc.execute

Like the fiter method, order_by statements can also be chained like so:

svc.Products.order_by("Name asc").order_by("Price desc")

Skip

Skip allows you to skip a number of records when querying. This is often used for paging along with top.

svc.Products.skip(5)
products = svc.execute # => skips the first 5 items

Top

Top allows you only retrieve the top X number of records when querying. This is often used for paging along with skip.

svc.Products.top(5)
products = svc.execute # => returns only the first 5 items

Authentication

Basic HTTP Authentication is supported via sending a username and password as service constructor arguments:

require 'ruby_odata'

svc = OData::Service.new "http://127.0.0.1:8888/SampleService/Entities.svc", { :username => "bob", :password=> "12345" }

SSL/https Certificate Verification

The certificate verification mode can be passed in the options hash via the :verify_ssl key. For example, to ignore verification in order to use a self-signed certificate:

require 'ruby_odata'

svc = OData::Service.new "https://127.0.0.1:44300/SampleService/Entities.svc", { :verify_ssl => false }

Or an OpenSSL integer constant can be passed as well:

require 'ruby_odata'

svc = OData::Service.new "https://127.0.0.1:44300/SampleService/Entities.svc", { :verify_ssl => OpenSSL::SSL::VERIFY_PEER }

Default verification is OpenSSL::SSL::VERIFY_PEER. Note due to the way Ruby's Request object implements certificate checking, you CAN NOT pass OpenSSL::SSL::VERIFY_NONE, you must instead pass a boolean false.

Tests

All of the tests are written using Cucumber going against a sample service (Found in /test/SampleService/*). The SampleService is an ASP.NET Web Site running a SQLEXPRESS 2008 R2 Database (TestDB), as well as the ADO.NET Entity Framework and a WCF Data Service. In order to run the tests, you need to spin up the SampleService and have it running on port 8888 (localhost:8888/SampleService).

One way to do this is to open the SampleService within Visual Studio 2010 and run it from there (must use IIS instead of development web server in order for basic auth tests to pass, however). Another option is to use IIS Express. It is a free download from Microsoft. Once installed, there is a batch file found in /test called “iisExpress x64.bat”, you can run the batch file and just close the command window. There is a also an “iisExpress x86.bat” file for those of you running a 32-bit machine. The only difference is the path to the Program Files directory. Once you run the batch file, the web server will spin up and you can find the instance in your systray just like if Visual Studio ran it for you. To stop the server, right click on the icon in your systray and tell it to stop.

Note there are also Cassini batch files that you can use as well, but the basic auth tests will fail if using this server (which doesn't support basic http authentication).

If you are testing on a Windows machine, you may encounter a problem with using cucumber and Ruby 1.9.2. You will get a message when you fire up cucumber about missing msvcrt-ruby18.dll. The fix for this is to make sure you have the RubyInstaller DevKit installed, then do the following:

gem uninstall json
gem install json --platform=ruby -v 1.4.6

From the BASE ruby_odata directory, run cucumber, which will execute all of the tests.

Something went wrong with that request. Please try again.