Skip to content

Instantly share code, notes, and snippets.

@brikis98
Created August 9, 2011 00:25
Show Gist options
  • Select an option

  • Save brikis98/1133133 to your computer and use it in GitHub Desktop.

Select an option

Save brikis98/1133133 to your computer and use it in GitHub Desktop.
Ruby/Sinatra examples
# For a given company id, update the company's location and website
post '/company' do
# ... process the request ...
"id = #{params[:companyId]}, location = #{params[:location]}, website = #{params[:website]}" # echo back the params
end
post '/company' do
# Fetch the current member's profile and group data in parallel from two Frontier Content Services
member, groups = assemble {
GET resource(:profile, :v0), {:memberId => session[:memberId]}
GET resource(:group, :v0), {:memberId => session[:memberId]}
}
# ... Use member and groups data ...
"#{member.firstName} is in #{groups.size()} groups"
end
post '/company' do
# Fetch the current member's profile and group data in parallel from two Frontier Content Services
member, groups = assemble {
GET resource(:profile, :v0), {:memberId => session[:memberId]}
GET resource(:group, :v0), {:memberId => session[:memberId]}
}
# ... Use member and groups data ...
# Now update the company info
company = resource(:company, :v0)
params = {:companyId => params[:companyId], :location => params[:location], :website => params[:website]}
result = assemble {
PUT company, params
}
"#{member.firstName} just updated the company info for company #{params[:companyId]}"
end
# Matches www.linkedin.com and prints "Hello World"
get '/' do
"Hello World"
end
# Matches URLs that start with /hello/ and pulls out a named parameter at the end,
# e.g. www.linkedin.com/hello/Kevin prints "Hello Kevin"
get '/hello/:name' do |name|
"Hello #{name}!"
end
# Matches URLs with a wildcard between hello and world and extracts query/body params,
# e.g. www.linkedin.com/hello/fooooo/world with a body of {"name": "Jim"} prints "Hello Jim"
post '/hello/*/world' do
"Hello #{params[:name]}"
end
@efleming
Copy link

efleming commented Aug 9, 2011

What does the assemble dsl look like? That is a very nice way of doing concurrent remote requests.

@baq
Copy link

baq commented Aug 9, 2011

@efleming:
The assemble DSL is a pretty Ruby costume that was put on a complex internal Java library where it was fairly difficult to come up with nice 4GL abstraction involving parallelism. The basic idea is that each statement inside the 'assemble' block creates a service call object that is stuffed into a queue. Once the assemble block evaluation is complete, the queue contains information for all the service calls that need to be made and then it can call into the underlying library to retrieve the actual data (which it does in parallel). Once the assemble block returns, the developer can assume the data fetch is complete.

This is the beauty of JRuby: combining the concise, expressive power of Ruby with the robustness of Java.

To learn more about creating Ruby DSLs, I highly recommend the book 'Metaprogramming Ruby' by Paolo Perrota.

@brikis98
Copy link
Author

brikis98 commented Aug 9, 2011

@efleming
Copy link

Thanks for the info, I was just more interested in what the basics of the assemble method looked like behind the scenes. I understand if that is proprietary information, thanks for the info. This is great and I really enjoyed the article. I am working on a very similar application architecture for an app I work on that is JRuby on the front end (we're using Rails) and a bunch of individual Java services on the back end.

@baq
Copy link

baq commented Aug 15, 2011

@efleming
It's not really "proprietary" per se, but it does use a lot of internally developed Java libraries so it would be harder to make sense of. You should check out https://github.com/avik-das/asap to see an open-sourced implementation developed here that does something very similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment