Syntax error with rubygem elasticsearch - ruby

I have a problem using the rubygem for elasticsearch
When I try to execute the following code :
#!/usr/bin/env ruby
require 'rubygems'
require 'elasticsearch'
client = Elasticsearch::Client.new log: true
I get the following error :
syntax error, unexpected ':', expecting $end
client = Elasticsearch::Client.new log: true
^
Any ideas about this ?
Thanks

You seem to use Ruby prior to 1.9 version. If so, either update Ruby (which is recommended), or use old rocket-hash syntax:
client = Elasticsearch.new :log => true

Related

WEBrick errors on ruby 1.8, works fine on ruby 1.9 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following script which runs a web server and executes a command when a request is received.
#!/usr/bin/env ruby
require 'webrick'
server = WEBrick::HTTPServer.new(port: ARGV.first)
server.mount_proc '/' do |req, res|
r10kstatus = system( "sudo r10k deploy environment -pv warn" )
puts r10kstatus
end
trap 'INT' do server.shutdown end
server.start
Unfortunatly, I need to backport this to work on ruby 1.8 (since scripting rvm to work on distributed systems is a pain).
When I try and run the script on ruby 1.8, I get the following error:
/usr/bin/r10k_gitlab_webhook:19:in `load': /usr/lib/ruby/gems/1.8/gems/r10k_gitlab_webhook-0.1.0/bin/r10k_gitlab_webhook:4: syntax error, unexpected ':', expecting ')' (SyntaxError)
server = WEBrick::HTTPServer.new(Port: ARGV.first)
^
/usr/lib/ruby/gems/1.8/gems/r10k_gitlab_webhook-0.1.0/bin/r10k_gitlab_webhook:4: syntax error, unexpected ')', expecting $end
from /usr/bin/r10k_gitlab_webhook:19
How could this script be modified to work on ruby 1.8?
Update
I've changed 'Port' to 'port'
Update2
Comparing the ruby 1.8 and ruby 1.9 documentation for webrick, shows that there isn't a port parameter for the new method in 1.8
http://www.ruby-doc.org/stdlib-1.8.7/libdoc/webrick/rdoc/WEBrick/HTTPServer.html#method-c-new
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/webrick/rdoc/WEBrick/HTTPServer.html#method-c-new
I've tried changing (Port: ARGV.first) to (port: ARGV.first), yet it still gives the same error:
server = WEBrick::HTTPServer.new(port: ARGV.first)
^
r10k_gitlab_webhook_old.rb:4: syntax error, unexpected ')', expecting $end
The key: value syntax for hashes (where key is a symbol) was introduced in Ruby 1.9. For Ruby 1.8 you need to use the :key => value syntax:
server = WEBrick::HTTPServer.new(:Port => ARGV.first)
(and I think it is :Port).

'cannot load such file -- mongoid' with rspec with ruby 2

Getting this error
cannot load such file -- mongoid
when trying to run RSpec tests.
I have mongo running in another terminal (mongod)
with ruby 2.0
I've trying including gem 'moped' and bundled but got the same error.
Trying to find out how to fix this error and run my tests.
This is for an open-source project that I've forked and am trying to update.
The test starts with:
require 'spec_helper'
begin
require 'will_paginate/mongoid' # <-- this is the issue
rescue LoadError => error
warn "Error running Sequel specs: #{error.message}"
mongoid_loaded = false
else
Mongoid.database = Mongo::Connection.new.db('will_paginate_test')
class MongoidModel
include Mongoid::Document
end
mongoid_loaded = true
end
describe...
I commented out require 'will_paginate/mongoid' but I then got uninitialized constant Mongoid (NameError)

SOAP - Need to understand the error I get

I'm trying to initialize a variable called proxy like this:
proxy = Savon::Client.new "http://192.168.1.1:8080"
The thing is, when I run my code, I only get the error:
NameError: uninitialized constant NameOfTheClass::Savon
Thanks for any help!
PD: I'm using Ruby 1.9.2
PD2: I'm trying to run this from console.
You found probably the documentation for versions < 0.8.x.
Unfortunately the syntax has changed! Have a look here: https://github.com/rubiii/savon/blob/master/README.md
Savon works with blocks now.
Your example should now look like this
require 'savon'
require 'pp'
proxy = Savon::Client.new do
wsdl.document = "http://my.webservices.net/service?wsdl"
end
pp proxy.wsdl.soap_actions

Ruby Sinatra - connect to mongoDB on mongoHQ failed

This is just for my weekend project/study, I am very new to Sinatra and MongoDB.
I've installed the gems for mongoDB, such as: mongo, mongo_mapper and mongoid.
When I tried connecting to my database on MongoHQ from localhost, it encountered such an error:
Mongo::ConnectionFailure at /
failed to connect to any given host:port
* file: connection.rb
* location: connect
* line: 489
I found a similar thread on SO, but frankly speaking, I don't quite understand the answers...
Here is my code snippet:
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongo_mapper'
get '/' do
MongoMapper.connection = Mongo::Connection.new('flame.mongohq.com', 27044)
MongoMapper.database = 'notes'
MongoMapper.database.authenticate('foo', 'bar')
erb :list
end
I took the above code from here, but it seems not working...
Which part is wrong? Is there another way to do this? In the end this test web app will be deployed onto heroku, so I hope the solution can work with both localhost and my heroku server.
Updated:
I just created a minimal code snippet to test the mongodb connection:
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new("flame.mongohq.com", 27044).db("notes")
But still got the error, after timeout:
$ ruby mongodbtest.rb
/Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:489:in
`connect': failed to connect to any given host:port (Mongo::ConnectionFailure)
from /Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:137:in
`initialize'
from mongodbtest.rb:4:in `new'
from mongodbtest.rb:4
The hostname and port are according to mongoHQ documentation, so they must be right.
Thanks for the help in advance.
2nd Update:
I just tested the mongodb connection string using terminal:
mongo mongodb://flame.mongohq.com:27044/notes -u foo -p bar
Unfortunately this would get me an connection failed error, honestly, I don't know why...
I use
uri = URI.parse(ENV['MONGOHQ_URL'])
#mongo_connection = Mongo::Connection.from_uri( uri )
#mongo_db = #mongo_connection.db(uri.path.gsub(/^\//, ''))
#mongo_db.authenticate(uri.user, uri.password)
You can look up your mongo url using the heroku config --long command
Just gave this another try, this time, I was using the ip address taken from ping:
So, if I change :
db = Mongo::Connection.new('flame.mongohq.com', 27060).db("notes")
db.authenticate('fake', 'info')
To:
db = Mongo::Connection.new('184.73.224.5', 27060).db("notes")
db.authenticate('fake', 'info')
That will work...
I still don't understand why the domain name approach won't work, but at least I can finish this off :)

Run sql script from Ruby

Using DBI::DatabaseHandle#execute or DBI::DatabaseHandle#prepare it's not possible to run an sql script (with mutiple sql statments). It fails with the following error :
ERROR: cannot insert multiple commands into a prepared statement
I tried to use the "unprepared" way using DBI::DatabaseHandle#do (the doc says it "goes straight to the DBD‘s implementation") but it keeps throwing the same error.
code snippet:
require 'dbd/pg'
require 'dbi'
DBI.connect("dbi:pg:database=dbname", db_user, db_password, db_params) do |dbh|
schema = IO::read(schema_file)
dbh.do(schema)
end
I'm using
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
dbi-0.4.3
dbd-pg-0.3.9
pg-0.9.0-x86-mswin32
Thank you!
Use either a function or just run multiple prepared queries.
Running multiple queries using DatabaseHandle#do is a missing feature in DBD-Pg. See Ruby/DBI feature 28001.
Note that Pg, the native postgresql Ruby driver on which DBD-Pg is based, allows running multiple queries.
Example:
require 'pg'
require 'dbd/pg'
require 'dbi'
# Pg Succeeds
PGconn.new({:host=>host,:user=>user,:password=>password,:dbname=>dbname}) do |conn|
conn.exec("select 1; select 1;")
end
# DBD-Pg Fails with: ERROR: cannot insert multiple commands ...
DBI::connect("dbi:pg:database=#{dbname};host=#{host};", user, password) do |dbh|
dbh.do("select 1; select 1;")
end

Resources