With several spawling, monolithic Rails apps under my belt, I had the opportunity to go a different route. Bulging models, obtuse controllers, and views chock full of logic were my world. When I came up for air, all the cool kids were writing thick clients with svelte backends. Perhaps Sinatra and some hip Javascript framework were the way? Here's what I learned...
23. BACK END CRITERIA
• Had to be Ruby
• Building RESTful API
• Had to be easily deployed
• Want “lightweight”
• Had to be actively developed
• Couldn’t be Rails. Just ‘cuz.
Monday, October 14, 13
25. FRONT END CRITERIA
• Active Development
• Strategic choice for company
• Well documented
• Stable API
(Not gonna talk a whole lot about these)
Monday, October 14, 13
44. OPEN SOURCE
You can, you know, actually
read the source when you get stuck.
Monday, October 14, 13
45. ONE OF THE
BIGGEST THINGS
I LEARNED!
•I always had been intimidated by
complexity Rails’ source.
•Intimidated by size and talent of community.
•Hadn’t run into trouble with Rails, no need
to look at the source.
Monday, October 14, 13
48. SEQUEL
Weird (concurrency?) problem we just couldn’t nail down.
Probably wasn’t Sequel, probably was us.
Running out of time so...
Monday, October 14, 13
53. “A Rack application is an Ruby object (not a
class) that responds to call. It takes exactly one
argument, the environment and returns an Array
of exactly three values: The status, the headers,
and the body.” - Rack API docs
Monday, October 14, 13
54. “A Rack application is an Ruby object (not a
class) that responds to call. It takes exactly one
argument, the environment and returns an Array
of exactly three values: The status, the headers,
and the body.” - Rack API docs
require
'rubygems'
require
'rack'
class
HelloWorld
def
call(env)
[200,
{"Content-‐Type"
=>
"text/html"},
"Hello
Rack!"]
end
end
Rack::Handler::Mongrel.run
HelloWorld.new,
:port
=>
9292
Monday, October 14, 13
59. SERIALIZATION
• achiu/rack-parser - I was surprised to
•
learn that converting JSON docs to
Rack parameters wasn’t automatic.
HashWithIndifferentAccess - Don’t
expect your parameter names to be
symbolized for you.
Monday, October 14, 13
61. WE EVEN WROTE
OUR OWN
MIDDLEWARE!
HTTP status handling
for common errors
Monday, October 14, 13
62. class
ErrorMiddleware
def
initialize(app,
options
=
{})
@app
=
app
end
def
call(env)
dup._call(env)
end
def
_call(env)
@app.call
env
rescue
Exception
=>
e
logger.error
"-‐-‐-‐
rescued
#{e.inspect}
at
#{Time.now}
-‐-‐-‐"
e.backtrace.each
do
|line|
logger.error
line
end
handler
=
ExceptionHandler.new(e)
Rack::Response.new(handler.body,
handler.status,
{}).finish
end
end
Monday, October 14, 13
70. VAGRANT
Still doesn’t help w/ the nuisance of
having to commit code into multiple
places to make one feature change.
Monday, October 14, 13
71. VAGRANT
If I had to do it over again, I’d
probably keep one repo/app for the
server-side components and deploy
it differently by task.
Monday, October 14, 13
75. RAILS CONSOLE
Missed it for a while.
Then I realized I could just make a silly setup script.
Monday, October 14, 13
76. RAILS CONSOLE
Missed it for a while.
Then I realized I could just make a silly setup script.
It worked just as well and was a lot faster.
Monday, October 14, 13
79. RERUN
Restarts your server for you when
files change in your app’s directory.
Wouldn’t work with pokey old Rails
but works great with Sinatra.
Monday, October 14, 13
85. CORS Issues
Can’t serve your single
page app from file://
during development
Can’t go across ports
without explicit
permission
Monday, October 14, 13
86. CORS Issues
Did all of this with (you
guessed it) middleware!
github.com/cyu/rack-cors
Monday, October 14, 13
87. SUMMARY
•Don’t be afraid to look at the source code
of your libraries.
•Isolate your app from libraries
•Middleware is easy!
•There are lots of cool tools out there. Learn
about them.
•Figure out your strategy for dealing w/
cross origin requests right away.
Monday, October 14, 13
88. SPENDING TIME AWAY
FROM RAILS DE-MYSTIFIED
IT FOR ME AND MADE ME
MORE COMFORTABLE WITH
RUBY.
Monday, October 14, 13
89. SPENDING TIME AWAY
FROM RAILS DE-MYSTIFIED
IT FOR ME AND MADE ME
MORE COMFORTABLE WITH
RUBY.
It was totally worth it.
Monday, October 14, 13
90. HOWEVER...
If I were starting all over again,
I probably would just use
Rails API.
Monday, October 14, 13