Saturday, 15 February 2014

rest - Sinatra view and restapi -



rest - Sinatra view and restapi -

i'm new here not newbie in ruby.

i've litte question sinatra. if want utilize rest api functionality generate info source (like mysql). how decide utilize rest response (only values in json or other) or view (html.erb) ?

routes.rb

get '/api/movies/:id' film ||= movie.get(params[:id]) || halt(404) format_response(movie, request.accept) end

response_format.rb

def format_response(data, accept) accept.each |type| homecoming data.to_xml if type.downcase.eql? 'text/xml' homecoming data.to_json if type.downcase.eql? 'application/json' homecoming data.to_yaml if type.downcase.eql? 'text/x-yaml' homecoming data.to_csv if type.downcase.eql? 'text/csv' homecoming data.to_json end end

something like: (decide in routes)

get '/api/movies/:id' film ||= movie.get(params[:id]) || halt(404) request.user_agent =~ /mozilla/ ? format_response(movie, request.accept) : erb :view_movie end

or like: (decide in helper)

def format_response(data, accept) accept.each |type| homecoming data.to_xml if type.downcase.eql? 'text/xml' homecoming data.to_json if type.downcase.eql? 'application/json' homecoming data.to_yaml if type.downcase.eql? 'text/x-yaml' homecoming data.to_csv if type.downcase.eql? 'text/csv' erb :view_movie if type.downcase.eql? 'text/html' end end

rest sinatra erb

No comments:

Post a Comment