Warning Older Docs! - You are viewing documentation for a previous released version of RhoMobile Suite.

Extending Rhoconnect Application with custom routes

Extending Rhoconnect Application with custom routes

You can provide custom routes support in your Rhoconnect application while you still fully utilize the powerful features provided by the default Rhoconnect server implementation. To do this, create a custom server class inherited from Rhoconnect::Server and define all of your custom routes in this extended server class.

The following example illustrates how to add a sample `my_custom_route' to the existing Rhoconnect application.

1) create my_server.rb class in your Rhoconnect application’s root directory:

class MyServer < Rhoconnect::Server
  get '/my_custom_route' do
    if current_user
      send_file 'public/my_file.png'

The above custom route implementation will respond to client’s GET request, verifies the current_user (which will be extracted from the Rhoconnect session cookie) and returns a static PNG file (which can be later used in BLOB syncs)

2) Replace the default server instance with the new extended one in the config.ru file of your Rhoconnect application:

# Setup the url map
run Rack::URLMap.new \
    "/"         => MyServer.new,
    "/resque"   => Resque::Server.new, # If you don't want resque frontend, disable it here
    "/console"  => RhoconnectConsole::Server.new # If you don't want rhoconnect frontend, disable it here

Using the New Custom route in BLOB syncs

After you create the new custom routes and replace the default server instance with the new extended one, you can reference and use them from standard Rhodes applications. For example, suppose you have a Rhodes app with a Product model. To use a custom route in BLOB syncs, define the following property in the Rhodes Product model (file: app/Product/product.rb):

# The model has already been created by the framework, and extends Rhom::RhomObject
# You can add more methods here
class Product
  include Rhom::PropertyBag

  # Uncomment the following line to enable sync with Product.
  enable :sync

  #add model specific code here
  property :my_custom_blob_field, :blob, :overwrite 

To see the static image, modify the app/Product/show.erb file for the Product model in your Rhodes application:

    <div class="itemLabel">My Static Field Image</div>
    <div class="itemValue"><img src="<%=Rho::RhoApplication::get_blob_path(@product.my_custom_field)%>"></img></div>

Then, modify your Rhoconnect source adapter (in rhoconnect_app/sources/product.rb) to provide BLOB’s url for the my_custom_blob_field:

def query(params=nil)
    parsed = JSON.parse(RestClient.get("#{@base}.json").body)

    parsed.each do |item|
      item["product"]["my_custom_field-rhoblob"] = "http://localhost:9292/my_custom_route"
      @result[item["product"]["id"].to_s] = item["product"]
    end if parsed    

This way, your my_custom_field would be getting the BLOB image data from the custom route defined in your Rhoconnect extension server.

For more information on BLOB syncs, see this section.

Back to Top