couchbase / couchbase-ruby-model

The Active Model implementation for Couchbase Server built on couchbase-ruby-client
61 stars 23 forks source link

Issue integrating with Sinatra #24

Open jtjenkins opened 10 years ago

jtjenkins commented 10 years ago

I'm trying to do a Sinatra API around Couchbase. I was able to connect with a a config file in config/couchbase.yml and a single ruby file no problem.

Now that I'm trying to wrap it in Sinatra, I'm using the same config file, and the same model definition (now in a separate file that's being pulled in)

Now it's telling me it can't find the bucket, which doesn't make sense since I'm able to connect without Sinatra.

I'm not using any local views yet, I'm just testing the writing.

Is there any way to figure out where it's unable to connect?

Controller I'm using to test with:

get '/test' do

    start = Time.now()
    i = 1
    while ( i < 10 ) do
        p = Ad.new(:name => "test")
        p.save
        i += 1
    end
    stop = Time.now()
    time = stop - start
    json :message => "Writes done in " + time.to_s + " milliseconds"

end

Model Declaration:

class Ad < Couchbase::Model
  attribute :name
  attribute :company_id
  attribute :url
  attribute :click_url
  attribute :location
  attribute :dimensions
  attribute :category
  attribute :content_type
  attribute :type, :default => "ad"
end

Config file (config/couchbase.yml)

common: &common
  hostname: localhost
  port: 8091
  username: Administrator
  password: <password>
  pool: default

development:
  <<: *common
  bucket: <bucket>

Stack Trace:

Couchbase::Error::BucketNotFound -  (error=0x19):
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-1.3.6/lib/couchbase.rb:63:in `initialize'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-1.3.6/lib/couchbase.rb:63:in `new'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-1.3.6/lib/couchbase.rb:63:in `connect'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-1.3.6/lib/couchbase.rb:138:in `bucket'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-model-0.5.3/lib/couchbase/model.rb:752:in `bucket'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-model-0.5.3/lib/couchbase/model.rb:524:in `create'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/couchbase-model-0.5.3/lib/couchbase/model.rb:559:in `save'
    /Users/Meatshield/Airvirtise/test/controllers/test.rb:7:in `block in <top (required)>'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `block in compile!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `[]'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (3 levels) in route!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:976:in `route_eval'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (2 levels) in route!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:997:in `block in process_route'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `catch'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `process_route'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:955:in `block in route!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `each'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `route!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1067:in `block in dispatch!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1064:in `dispatch!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `block in call!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `call!'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:877:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-protection-1.5.2/lib/rack/protection/xss_header.rb:18:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-protection-1.5.2/lib/rack/protection/path_traversal.rb:16:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-protection-1.5.2/lib/rack/protection/json_csrf.rb:18:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-protection-1.5.2/lib/rack/protection/frame_options.rb:31:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:217:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:210:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/show_exceptions.rb:21:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:180:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:2004:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `block in call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1778:in `synchronize'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/thin-1.6.1/lib/thin/connection.rb:82:in `block in pre_process'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/thin-1.6.1/lib/thin/connection.rb:80:in `catch'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/thin-1.6.1/lib/thin/connection.rb:80:in `pre_process'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `call'
    /Users/Meatshield/.rvm/gems/ruby-2.0.0-p195/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `block in spawn_threadpool'
127.0.0.1 - - [23/Feb/2014 18:34:45] "GET /__sinatra__/500.png HTTP/1.1" 304 - 0.0040
avsej commented 10 years ago

I was able to connect with a a config file in config/couchbase.yml and a single ruby file no problem.

What do you mean here, you wrote your own function which loads config from YAML file? Because automatically it gets loaded only in rails environment using railtie. https://github.com/couchbase/couchbase-ruby-model/blob/master/lib/couchbase/railtie.rb#L64-L94

Could try to add this lines to your configure hook?

filename = "config/couchbase.yml"
config = YAML.load(ERB.new(File.read(filename)).result)[environment]
::Couchbase.connection_options = config