HTTParty崩溃了Sinatra

时间:2016-02-29 18:19:29

标签: ruby rubygems sinatra httparty

我有一个小型的模块化Sinatra应用程序,我正在构建它作为单独REST API的前端。一切都在游泳,直到我添加了HTTParty(向我的API发出请求),现在没有页面会加载。

API服务器在localhost端口9393上运行,但即使关闭该服务器,错误仍然存​​在。 (当我在我的路线中注释掉一个HTTParty请求时。

这是我的app.rb文件:

ENV["RACK_ENV"] ||= "development"

require 'bundler' require 'sinatra/base' require 'sinatra/contrib/all' require 'json' require 'sinatra/strong-params' require 'haml' require 'sinatra/partial' require 'sass'
# require 'httparty'

require 'pry'


Bundler.setup Bundler.require(:default, ENV["RACK_ENV"].to_sym)

# DATABASE CONFIG
# dbconfig = YAML.load(File.read('./config/database.yml'))
# ActiveRecord::Base.establish_connection dbconfig["#{settings.environment}"]

# Base URL for API calls API_BASE = 'http://localhost:9393'

class PantryApp < Sinatra::Base   include HTTParty   register Sinatra::Partial

  set :root, File.dirname(__FILE__)   enable :sessions

  # set folder for templates to ../views, but make the path absolute   set :views, File.expand_path('../app/views', __FILE__)

  # don't enable logging when running tests   configure :production, :development do
    enable :logging   end

  # will be used to display 404 error pages   not_found do
    erb :not_found   end

  get '/' do
    haml :login   end

  get '/login' do
    haml :login   end

  post '/login' do
    # api_token = HTTParty.post(API_BASE + '/token', query: {email: params['email'], password: params['password']})
    binding.pry
    haml :user_pantry   end

  # REQUIRE ALL APP FILES   Dir["./app/**/*.rb"].each { |f| require f }

end

当我尝试去/登录时,我得到:

Boot Error

Something went wrong while loading config.ru

Errno::ECONNREFUSED: Connection refused - connect(2) for nil port 80

/Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `initialize' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `open' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `block in connect' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/timeout.rb:73:in `timeout' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:878:in `connect' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:863:in `do_start' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:852:in `start' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:1375:in `request' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/httparty-0.13.7/lib/httparty/request.rb:117:in `perform' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/httparty-0.13.7/lib/httparty.rb:545:in `perform_request' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/httparty-0.13.7/lib/httparty.rb:476:in `get' /Users/rileypants/dev/capstone/ui/app.rb:46:in `<class:PantryApp>' /Users/rileypants/dev/capstone/ui/app.rb:26:in `<top (required)>' config.ru:1:in `require' config.ru:1:in `block in inner_app' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize' config.ru:1:in `new' config.ru:1:in `inner_app' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:113:in `eval' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:113:in `inner_app' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:103:in `assemble_app' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:86:in `proceed_as_child' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:31:in `call!' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:18:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/shotgun-0.9.1/lib/shotgun/favicon.rb:12:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/builder.rb:153:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3@pantry_ui/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

config.ru只是:

require File.dirname(__FILE__) + '/app.rb'

map('/users') { run UsersController }
map('/') { run PantryApp }

在我看来,我在app.rb的第一条路线中的“获取”的内容正在导致httparty而不是Sinatra,但我不知道为什么或要采取什么措施来修复它。

1 个答案:

答案 0 :(得分:1)

我回答了我自己的问题,但是我想把它留在这里,以防它对其他人有用。

在这种情况下,我不需要在类中使用include HTTParty,而是需要在顶部使用它。我认为使用include混淆了get请求。