Skip to content

Commit

Permalink
Updated README to show chained filter and order_by calls
Browse files Browse the repository at this point in the history
  • Loading branch information
visoft committed Jul 7, 2010
1 parent 3b08533 commit 04a5e77
Showing 1 changed file with 98 additions and 83 deletions.
181 changes: 98 additions & 83 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,67 @@ The <b>Open Data Protocol</b> (OData) is a fantastic way to query and update dat

== Installation
You can install ruby_odata as a gem using:
gem install ruby_odata
gem install ruby_odata

== Usage
As of version 0.0.5, support has been added for ActiveSupport 3.0.0 beta 4 and Ruby 1.9.1
As of version 0.0.5, support has been added for ActiveSupport 3.0.0 beta 4 and Ruby 1.9.1.

As of version 0.0.6, support has been added for batch saves

=== 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 'lib/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
require 'lib/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 'lib/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}"
require 'lib/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 'lib/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}"
require 'lib/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 'lib/ruby_odata'
svc = OData::Service.new "http://127.0.0.1:8888/SampleService/Entities.svc"
svc.Categories
categories = svc.execute
puts categories.to_json
require 'lib/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:

Expand All @@ -81,66 +82,80 @@ 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"
# 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}[http://www.odata.org/developers/protocols/uri-conventions#FilterSystemQueryOption] 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}"

# 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}[http://en.wikipedia.org/wiki/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}"
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

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
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
svc.Products.top(5)
products = svc.execute # => returns only the first 5 items

== Tests
===DATABASE SETUP - DO THIS FIRST
Expand Down

0 comments on commit 04a5e77

Please sign in to comment.