1

我正在尝试使用葡萄制作 Rest API。

为此,我在一个目录中创建了一个 config.ru 文件:

require 'rubygems' 
require 'rack'
require './grape_sample'
run Twitter::API

有一个名为grape_sample.rb 的文件:

module Twitter
class API < Grape::API

version 'v1', :using => :header, :vendor => 'twitter'
format :json

helpers do
  def current_user
    @current_user ||= User.authorize!(env)
  end

  def authenticate!
    error!('401 Unauthorized', 401) unless current_user
  end
end

resource :statuses do

  desc "Return a public timeline."
  get :public_timeline do
    Status.limit(20)
  end

  desc "Return a personal timeline."
  get :home_timeline do
    authenticate!
    current_user.statuses.limit(20)
  end

  desc "Return a status."
  params do
    requires :id, :type => Integer, :desc => "Status id."
  end
  get ':id' do
    Status.find(params[:id])
  end

  desc "Create a status."
  params do
    requires :status, :type => String, :desc => "Your status."
  end
  post do
    authenticate!
    Status.create!({
      :user => current_user,
      :text => params[:status]
    })
  end

  desc "Update a status."
  params do
    requires :id, :type => String, :desc => "Status ID."
    requires :status, :type => String, :desc => "Your status."
  end
  put ':id' do
    authenticate!
    current_user.statuses.find(params[:id]).update({
      :user => current_user,
      :text => params[:status]
    })
  end

  desc "Delete a status."
  params do
    requires :id, :type => String, :desc => "Status ID."
  end
  delete ':id' do
    authenticate!
    current_user.statuses.find(params[:id]).destroy
  end

end
end
end

当我运行命令rackup config.ru时,会发生此错误:

/home/ritesh/rails/grape_sample.rb:2:in `<module:Twitter>': uninitialized constant Twitter::Grape (NameError)
    from /home/ritesh/rails/grape_sample.rb:1:in `<top (required)>'
    from /home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/ritesh/rails/config.ru:7:in `block in <main>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:55:in `instance_eval'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:55:in `initialize'
    from /home/ritesh/rails/config.ru:in `new'
    from /home/ritesh/rails/config.ru:in `<main>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:49:in `eval'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:49:in `new_from_string'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/builder.rb:40:in `parse_file'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:199:in `app'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:314:in `wrapped_app'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:250:in `start'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/lib/rack/server.rb:141:in `start'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.5.0/bin/rackup:4:in `<top (required)>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/rackup:19:in `load'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/rackup:19:in `<main>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'

这一次,我在 config.ru 中包含了 require 文件,但仍然看到错误。

4

1 回答 1

3

您不需要实现 Grape 的 ruby​​gem。

添加行

require 'grape'

grape_sample.rb

如果您仍然有问题,请确保您已安装葡萄 gem:

gem install grape

于 2013-01-28T23:35:41.913 回答