MongoMapper save not working - ruby

No idea why this is not working. It's the first time I am working with Mongo, but from all the docs I have read, this should work? Anyone have any idea what I am missing?
require 'rubygems'
require 'sinatra/base'
require 'mongo_mapper'
mongo_server = '127.0.0.1'
mongo_database = 'inone'
MongoMapper.connection = Mongo::Connection.new(mongo_server)
MongoMapper.database = mongo_database
# DB model
class URLstore
include MongoMapper::Document
key :url_key, String
key :url, String
end
class URLnip < Sinatra::Base
get '/testmongo' do
nipurl = URLstore.new(:url_key => "abc", :url => "www.google.com")
nipurl.save
end
end
Opening the Mongo terminal I can see the DB get's created
> show dbs
inone 0.203125GB
but doing this brings back no results at all
> db.inone.find()
>
or
> db.inone.find({url : 'www.google.com'})
>
same thing nothing.

Shouldn't that be db.urlstore.find()? Or however MongoMapper changes the case. What does show collections in a Mongo shell say after you select the right database?

Related

How to display results from database in Sinatra using Sequel?

I have an SQLite3 database called sk.db with a table called Sked that displays a schedule of sports matches with a column date. I am simply trying to display today's matches. It appears as though the connection to the database is not working, though I do not get any errors.
I have tried looking through the Sequel documentation to no avail. How can I display results from an existing database in Sinatra?
.rb
require 'date'
require 'sequel'
require 'sinatra'
DB = Sequel.connect("sqlite://sk.db")
class Sked < Sequel::Model
end
schedule = DB.from(:sked)
get '/' do
todaymatches = schedule.where(:date => Date.today)
erb :games
end
.erb
<h1>Games</h1>
<p><%= #todaymatches %></p>
.where doesn't actually retrieve data, but instead returns a dataset. Add an .all to actually retrieve the data
todaymatches = schedule.where(:date => Date.today).all

how do I read from a Database in ruby sinatra using active record

I want to read entrys from a Database using active_record and keep getting diffrent Errors like: Name error and can't find the database or it can't execute the query.
so my question here is how do i read from a database or execute SQL-queries and for example write the result into a variable?
require 'rubygems'
require 'sinatra'
require 'active_record'
require 'sqlite3'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "test.db"
)
class Article < ActiveRecord::Base
end
#ActiveRecord::Migration.create_table :users do |t|
# t.string :name
#end
class App < Sinatra::Application
end
get '/' do
output = users.select(:all)
f = File.open('name','a'); f.write(output); f.close
#puts User.first
To read the data from the users table, you'd simply do:
class User < ActiveRecord::Base
end
get '/' do
#users = User.all
puts "Grabbed #{#users.size} user(s) from database"
end

Retrieving rows from in sequel gem

i am trying to retrieve a row from a dataset
my model class is
require 'sequel'
class Item < Sequel::Model
end
and one my hello.rb file is
require "rubygems"
require './post'
require "sequel"
require './item'
# connect to an in-memory database
#DB = Sequel.connect('postgres://ritesh:newpassword#localhost')
puts Item.filter(:id =>'1').first
its giving me output
#<Item:0xb693996c>
i want to get all the columns of the row whose id field is 1 what should be the query??
try
puts Item.filter(:id =>'1').first.inspect
or
puts Item.filter(:id =>'1').first.to_yaml
(for better formatting if you have yaml required in your code like so require 'yaml')

In Ruby/Sinatra, Datamapper's .all works but .get doesn't?

I am trying to take data from a path in Sinatra, and use it to look up a particular record using Datamapper. The Datamapper docs seem to indicate that.
get "/test/:test_path" do
test_get = Intake.get( params[:test_path] )
# Do stuff
erb :blah_blah_blah
end
should find any records associated with the symbol :test_path
This does not work. test_get gets nil.
Meanwhile, what does work is
get "/test/:test_path" do
test_all = Intake.all(:test_path => params[:test_path] )
# Do stuff
erb :blah_blah
end
My two questions are:
What am I doing wrong with the .get() call in Datamapper?
Is the .all(:name => value) method slower than .get(), or does it not matter which I use?
Here's a Sinatra script pared down to demonstrate the behavior.
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-timestamps'
DataMapper.setup(:default, {:adapter => 'yaml', :path => 'db'})
class Intake
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :test_path, String
end
get "/test/:test_path" do
test_all = Intake.all(:test_path => params[:test_path] )
puts 'test_all:' test_all.inspect
test_get = Intake.get( params[:test_path] )
puts 'test_get:' test_get.inspect
"Hello World!"
end
#get only does a lookup based on primary key, with is the id. So
Intake.get(params[:test_path])
looks for something with id params[:test_path], which will fail. Use
Intake.first(test_path: params[:test_path])

store and output formatted array contents with sinatra

Environment: Ruby 1.9.2
I am new to Ruby/Sinatra and am creating a proof of concept web application. The purpose is fairly simple: A user inputs a list of domains and the script first checks their mx records, and if they meet the condition, pulls out the domain contact information. I am fairly sure that I am not going about storing the data appropriately and am looking for a more elegant solution that will enable me to style the results such that domain, email, and name are grouped together.
#!/usr/bin/env ruby
require "sinatra/base"
require 'rubygems'
require 'haml'
require 'sinatra'
require 'whois'
get '/' do
haml :index
end
post '/' do
#host = params[:host]
#host.split('\n')
#email = Array.new
#name = Array.new
#domain = Array.new
#host.each_line {|i|
if %x[dig -t mx #{i.chomp.gsub('www.', '')} | grep -i mx | grep -i google].empty?
puts "empty"
else
#domain << i.chomp.gsub('www.','')
#email << (Whois.whois(i.chomp.gsub('www.',''))).technical_contact.email
#name << (Whois.whois(i.chomp.gsub('www.',''))).technical_contact.name
end
}
haml :index
end
__END__
## layout
%html
%head
%title Gcrawl
%body
#header
%h1 Gcrawl
#content
=yield
%footer
## index
%p
Welcome to Gcrawl
%form(action='/' method='POST')
%textarea{:rows => '12', :cols => '40', :name => 'host'}
%input(type='submit')
- if defined?(#email)
%h3= #domain
%h3= #email
%h3= #name
Create a class Record which will contain #name, #domain and #email for a particular entry.
So, each instance of Record will have it's own name, domain and email.
Replace the array implementation with a Class. If you need to store the records in a database, use ActiveRecord.
It's good that you started with Sinatra, but if you are in a hurry, you can get your app running on Rails in an hour.
EDIT
Tutorials/Guides for getting started with Rails:
Rails Guides - Getting Started
rubyonrailstutorials.com
railstutor.com

Resources