Giter Club home page Giter Club logo

grape-rabl's People

Contributors

alovak avatar bitdeli-chef avatar cheef avatar chrisbloom7 avatar dblock avatar hobofan avatar hq063 avatar koko1000ban avatar lte avatar martinezcoder avatar olleolleolle avatar shpakvel avatar tsuwatch avatar yesmeck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grape-rabl's Issues

Fallback rendering should use the default formatter

Setup:

module RailsApp
  class API < Grape::API
    prefix :api
    format :xml
    formatter :xml, Grape::Formatter::Rabl
    content_type :xml, 'application/xml'
    default_format :xml

    resource :client_authorizations do
      post '/' do
        client_authorization = ClientAuthorization.new params[:clientAuthorization]
        if client_authorization.save
          @reference = OpenStruct.new({status: "OK", reference: client_authorization.id})
          render rabl: "client_authorizations"
        else
          client_authorization.errors
        end
      end
    end
  end
end

If the post is successful, it renders the RABL template as expected. If not, it renders the errors as a json string: {"current_date_begins":["is not a valid date"]}. Based on the example in the README (line 152), I was expecting it to render using the default XML formatter. If I comment out the line formatter :xml, Grape::Formatter::Rabl the errors are rendered as XML as expected. Looking at the source, I see it's hardcoded to fallback to the JSON formatter. Ideally, this should pick up the default_format formatter, or should be configurable.

uninitialized constant Grape::Formatter::Rabl

I have upgrade to 0.4.0, but

This line of code in my API controller:

formatter :json, Grape::Formatter::Rabl

Is returning now this error:


uninitialized constant Grape::Formatter::Rabl (NameError)

Use Rack::Config to set 'api.tilt.root' in config.ru

Following up on my note on issue #2, I tried configuring the middleware from Rails application.rb.

class Application < Rails::Application
  config.middleware.use(Rack::Config) do |env|
    env['api.tilt.root'] = Rails.root.join "app", "views", "api"
  end
end

While that works in other environments, it does not work in the test environment. Specifically, Rspec is complaining that it cannot find the root path for Tilt.

Template for Resource

It would be good if you could supply the :rabl option to a resource.

For example, instead of:

resource 'articles' do
  get '/', :rabl => "article" do
    @article = Article.all
  end

  get ':id', :rabl => "article" do
    @article = Article.find(params[:id])
  end
end

You could do:

resource 'articles', :rabl => "article" do
  get do
    @article = Article.all
  end

  get ':id' do
    @article = Article.find(params[:id])
  end
end

Thanks,

Ian

Support for Tilt Cache

Right now, grape-rabl tries loads the template file for each request. It would be great if grape-rabl supports Titl::Cache, such that template files are cached and not reloaded on subsequent requests.

@cache ||= ::Tilt::Cache.new
@cache.fetch(template) { ::Tilt.new(view_path(template), tilt_options) }

Thread safety with Puma

During Puma's phased restart, it often happens that the endpoint context is no longer available to the Rabl template which causes it throw errors. e.g. a helper defined in the Grape endpoint which is usually available to Rabl templates to use is no longer available because the context has been replaced/removed.

Since Grape-Rabl uses the api.endpoint key set in the environment, it is possible that the env is replaced before the rendering is finished. I'm not sure if this is a problem that should be fixed in Grape or Grape::Rabl. I'm only seeing this problem when running Grape with mult-threaded Puma server. It would be great if you can help me out. Thanks!

View Helpers

It doesn't seem to be possible to use view helpers like article_path.

I get:
undefined method `article_path' for #Rabl::Engine:0x000000084a4bd0

Thanks,

Ian

How to use with Rails?

I've got a Rails app in which I've built a simple API using grape. I'd like to use my existing RABL templates for this API, so I've stumbled upon this gem. I can't get it to function with my Rails app, though.

I've got my /app/api/api.rb setup like this:

require 'grape/rabl'

use Rack::Config do |env|
  env['api.tilt.root'] = Rails.root.join('app', 'views')
end

class API < Grape::API
...

but that gives me this error:

app/api/api.rb:3:in `<top (required)>': undefined method `use' for main:Object (NoMethodError)

Any ideas what I'm doing wrong? Or perhaps this wasn't meant to work in a Rails environment? On a related note, you've got this in your README:

object @user => :user # in the RABL template

How does this work? I feel like updating all my RABL templates with lines like this might make other parts of my app that use the templates break; hence my thinking this won't work in a Rails app. ...but feel free to set me straight. hehe

Let me know if you need any other information.

render doesn't work properly

Render from example doesn't work properly. I mean this part:

  get '/user/:id', :rabl => 'user' do
    @user = User.find(params[:id])

    if @user.admin?
      # overwrite the template (and pass locals) with the #render method
      render rabl: 'admin', locals: { details: 'this user is a admin' }
    end
  end

Workaround like following helps and works fine:

  get '/user/:id' do
    @user = User.find(params[:id])

    if @user.admin?
      # overwrite the template (and pass locals) with the #render method
      render rabl: 'admin', locals: { details: 'this user is a admin' }
    else
      render rabl: 'user'
    end
  end

But in your example, after execution with render rabl: 'admin' has happened, it does all following execution with 'admin' template as well.

Using extends on collections

I'm trying to do rendering of a collection via extends and passing in another template like here only I'm not using locals.

I have this in my stories/show.rabl

object @story
attributes :title, :description, :url

and this in my stories/index.rabl

collection @stories
extends 'stories/show'

I get an error ""undefined method `resolve_template' for #Grape::Endpoint:0x007f8f471a1778"".

However, if I change index.rabl to look like

collection @stories

attributes :title, :description, :url

It works fine. Any ideas?

Remove use of Tilt::Cache

I'm planning on deprecating Tilt::Cache in the next release of tilt (2.2.0), as it isn't used internally, isn't thread safe, and doesn't add any real value (faster to use a plain hash). grape-rabl should either copy the Tilt::Cache implementation, or switch to using a plain hash, or if you really need API compatibility for the #fetch method:

class Cache < Hash
  def fetch(*args)
    super(args) do
      self[args] = yield
    end
  end
end

id field now showing as $oid

Just upgraded to the latest grape-rabl and now our id fields are showing like this:

{"id":{"$oid": "516298920fb8600002000001"},

instead of:

{"id": "516298920fb8600002000001",

Any idea what happened?

I guess it's something to do with them being Mongoid BSON object id's:
irb(main):004:0> Article.first._id.class
=> Moped::BSON::ObjectId

It works fine if I remove the :rabl template and let Grape render it using it's own json stuff, but then we loose the control of the rabl file obviously.

How can I fix/get round this? - this didn't happen before in the older version of grape-rabl.

Thanks,

Ian

Use Grape > 0.2.3 formatter

With the next release of Grape, 0.23 (probably going to be 0.3) you no longer should need to monkey-patch the formatter. You should be able to do this:

module RablFormatter
   def self.call(object)
      ...
    end
end

And in the API:

formatter :json, RablFormatter

Changing rabl template dynamically

It would be nice if you could change the :rabl option dynamically depending on a certain condition.

For example, if you have:

resource 'articles' do
  get '/', :rabl => "articles" do
    @article = Article.all
  end

  get ':id', :rabl => "article" do
    @article = Article.find(params[:id])
  end
end

You could do:

resource 'articles' do
  get '/', :rabl => "articles" do
    @article = Article.all
  end

  get ':id', :rabl => "article" do
    @article = Article.find(params[:id])

    if @article.category == "technology"
      render :rabl => "article_with_technology"
    end
  end
end

such that the rendered rabl will depend on one or more specific article object attribute(s).

Is there a way using your gem to perform something close to the above?

Thanks,
Michael

Grape-RABL breaks default_format

Given a Grape App of:

# config.ru
require 'grape/rabl'

use Rack::Config do |env|
  env['api.tilt.root'] = '/path/to/view/root/directory'
end

class UserAPI < Grape::API

  default_format :json

  # use rabl with 'hello.rabl' template
  get '/user', :rabl => 'hello' do
    @user = User.first
  end

  # do not use rabl, normal usage
  get '/user2' do
    { :some => :hash }
  end
end

The non-RABL route respects the default_format, but the RABL one emits text/html instead. Any idea why?

NoMethodError on partials when upgrading to rabl 0.11.2 and above

Hi,

I'm using the following (and other) partial in my rabl views for grape:

  node :images do |partner|
      partial("images", :object => partner.gallery.attachments)
 end

When upgrading to the latest rabl (0.11.4) I noticed that I'm starting to get the following error message:

NoMethodError (undefined method views' for #<Grape::Util::HashStack:0x007ffb4d9d08a0>): app/views/api/partner.rabl:7:inblock in eval_source'

Going backward I saw that this error message started to appear at rabl 0.11.2 (it works fine in 0.11.1).

Thanks,
Ronen

RSpec advice on Rack::Config in rails

With my current setup using spork, rspec, & rails, it seems as though anytime I'm trying to use grape-rabl, it errors with "Use Rack::Config to set 'api.tilt.root' in config.ru" which I have defined in the config.ru as such:

require 'grape/rabl'

use Rack::Config do |env|
  env['api.tilt.root'] = Rails.root.join "app", "views", "api"
end

and in my specs i'm using:

  describe "GET /system/versions" do
    it "returns an array with v1 inside it" do
      get "/system/versions"
      response.status.should eq 200
      JSON.parse(response.body).should == ["v1"]
    end
  end

am I missing som necessary information for using rspec with grape-rabl?

How would you construct rabl to render the correct version of the rabl view?

If I have a user.rabl that is different for two different versions of the grape API, how do I get grape-rabl to render the correct one based on the grape api class setup?

Lets take this for an example:

File paths:

app/views/api/v1/user.rabl
app/views/api/v2/user.rabl

Grape API classes:

class API::V1::Users < Grape::API
  version 'v1', using: :header, vendor: 'me'
  format :json
  formatter :json, Grape::Formatter::Rabl
end

class API::V2::Users < Grape::API
  version 'v2', using: :header, vendor: 'me'
  format :json
  formatter :json, Grape::Formatter::Rabl
end

I have application.rb set up like so:

class Application < Rails::Application
  config.middleware.use(Rack::Config) do |env|
    env['api.tilt.root'] = Rails.root.join "app", "views", "api", 'v1'
    env['api.tilt.root'] = Rails.root.join "app", "views", "api", 'v2'
  end
end

Given this setup, rabl just renders v2 of user.rabl since it was the last one. I would appreciate any help. Thank you!

Re-using Templates

If i've got an articles.rabl, as follows:

collection @articles => :articles
attributes :id, :title, :published_at

child :author do
  attributes :id, :first_name, :surname
end

What I want to do is extract the author attributes into a users.rabl, as these will be the same when including a user into other records so i'd rather not duplicate them, like this:
https://github.com/nesquena/rabl/wiki/Reusing-templates

I created a users.rabl, and changed the above to:

child :author do
  extends "users"
end

but I just keep getting:

undefined method `views' for #<Grape::Util::HashStack:0x00000005cb6948>

Is this a grape-rabl problem?

Thanks,

Ian

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.