Sinatra, MongoMapper, and Builder wrong number of arguments (1 for 0) - ruby

I am getting this error when calling a builder template from sinatra route:
wrong number of arguments (1 for 0)
Sinatra - 1.1.2
Builder - 3.0.0
MongoMapper - 0.8.6
Calling it like this:
get '/current_load.xml' do
#caption = "Load Average"
#sub_caption = "5 minutes"
#time = Performance.where(:NAGIOS_SERVICEDESC => "Current Load").fields(:NAGIOS_LONGDATETIME).all
content_type :xml
builder :current_load
end
the builder file starts off like this:
xml.instruct!
Here's the full trace of the error:
ArgumentError - wrong number of arguments (1 for 0):
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlbase.rb:135:in `to_xs'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlbase.rb:135:in `_escape'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlbase.rb:140:in `_escape_quote'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:320:in `_attr_value'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:308:in `_insert_attributes'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:306:in `each'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:306:in `_insert_attributes'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:284:in `_special'
/usr/lib/ruby/gems/1.8/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:250:in `instruct!'
/opt/nec/www/nec/views/current_load.builder:1:in `evaluate_source'
If I change Builder to 2.1.2, I receive no error
or
If I take MongoMapper out I receive no error
Sounds like an issue between two gems??

Sinatra 1.1.3 successfully fixed this error.
That is the only change in the environment

Related

How to raise timeout error in unittesting

This is first time i am touching ruby, so no sure about correct terminology. I have tried searching for mulitple things, but couldn't find a solution.
I have this code block
domain_response = MyDomain::Api::MyApi::Api.new(parameters: message.to_domain_object, timeout: 1000)
# :nocov:
case (response = domain_response.response)
when MyDomain::Api::MyApi::SuccessResponse
## do something
when Domain::ErrorResponses::TimeoutResponse
## do something.
now i am trying to testing TimeoutResponse, I have written(tried) this
it "when api call timesout" do
expect(MyDomain::Api::MyApi::Api).to{
receive(:new)
} raise_error(MyDomain::ErrorResponses::TimeoutResponse)
end
this gave me error that unexpected identifier.
I have also tried by not providing receive, and it gave me error that block is expected.
Whats the proper way to raise an error that i can test?
Update:
Here is where i am stuck now
it "when api call timesout" do
# 1
expect(MyDomain::Api::MyApi::Api).to(
receive(:new),
).and_return(domain_api_instance)
# 2
expect(domain_api_instance.response).to receive(:response).and_raise(Domain::ErrorResponses::TimeoutResponse)
expect(domain_api_instance.response).to eq(ApiError::Timeout)
end
But with this code i am getting this error
1) Rpc::Package::SubPackage::V1::PackageService#first_test testing when api call timesout
Failure/Error: expect(domain_api_instance.response).to receive(:response).and_raise(Domain::ErrorResponses::TimeoutResponse)
#<InstanceDouble(MyDomain::Api::MyApi::Api) (anonymous)> received unexpected message :response with (no args)

Ruby code in logstash failing with _rubyexception

I would like to get help with the code in ruby filter.
logstash version is 5.0.0 alpha4.
I am testing the code in ruby filter as below but I am getting _rubyexception.
ruby {
code => "
event['newfield'] = 'test'
"
}
ruby filter is defined inside filter { }.
The logstash.log shows as below,
:timestamp=>"2016-08-03T15:26:47.291000+0900", :message=>"Ruby exception occurred: undefined method[]=' for 2016-08-03T06:26:46.829Z test %{message}:LogStash::Event", :level=>:error}
I cant find the reason why ruby filter is unable to use event object.
I appreciate if I could get some help to cope this issue.
Thanks,
Yu
There is a new event API in Logstash 5.0.0 and now you need to set fields on the event as follows:
ruby {
code => "
event.set('newfield', 'test')
"
}
For get the value from field name:
ruby {
code => "
value = (event.get('field_name'))
"
}

AWS SDK Presigned Post Ruby

It's pretty straightforward but I have this issue:
Unexpected error while processing request: undefined method `PresignedPost' for Aws::S3:Module
My objective: Get the presigned URL for an object to which I can perform an upload file.
My gem has
gem 'aws-sdk', '~> 2'
Code:
##aws_creds = Aws::SharedCredentials.new(profile_name: profile)
Aws.config.update({region: 'us-west-2',credentials: ##aws_creds})
s3 = Aws::S3::Resource.new
#bucket = s3.bucket(bucketName)
form = Aws::S3::PresignedPost(:key => key )
if(form)
form.fields
end
you normally don't do a standalone presignedpost. you do it using the bucket method.
something like #bucket.presigned_post(:key=>key)
See doc:
http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/PresignedPost.html
Note: Normally you do not need to construct a PresignedPost yourself.
See Bucket#presigned_post and Object#presigned_post.

ExecJS: keeping the context between two calls

I'm currently trying to use ExecJS to run Handlebars for one of the product I work on (note: I know the handlebars.rb gem which is really cool and I used it for some times but there is issues to get it installed on Windows, so I try another homemade solution).
One of the problem I'm having is that the Javascript context is not kept between each "call" to ExecJS.
Here the code where I instantiate the #js attribute:
class Context
attr_reader :js, :partials, :helpers
def initialize
src = File.open(::Handlebars::Source.bundled_path, 'r').read
#js = ExecJS.compile(src)
end
end
And here's a test showing the issue:
let(:ctx) { Hiptest::Handlebars::Context.new }
it "does not keep context properly (or I'm using the tool wrong" do
ctx.js.eval('my_variable = 42')
expect(ctx.js.eval('my_variable')).to eq(42)
end
And now when I run it:
rspec spec/handlebars_spec.rb:10 1 ↵
I, [2015-02-21T16:57:30.485774 #35939] INFO -- : Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.
Run options: include {:locations=>{"./spec/handlebars_spec.rb"=>[10]}}
F
Failures:
1) Hiptest::Handlebars Context does not keep context properly (or I'm using the tool wrong
Failure/Error: expect(ctx.js.eval('my_variable')).to eq(42)
ExecJS::ProgramError:
ReferenceError: Can't find variable: my_variable
Note: I got the same issue with "exec" instead of "eval".
That is a silly example. What I really want to do it to run "Handlebars.registerPartial" and later on "Handlebars.compile". But when trying to use the partials in the template it fails because the one registered previously is lost.
Note that I've found a workaround but I find it pretty ugly :/
def register_partial(name, content)
#partials[name] = content
end
def call(*args)
#context.js.call([
"(function (partials, helpers, tmpl, args) {",
" Object.keys(partials).forEach(function (key) {",
" Handlebars.registerPartial(key, partials[key]);",
" })",
" return Handlebars.compile(tmpl).apply(null, args);",
"})"].join("\n"), #partials, #template, args)
end
Any idea on how to fix the issue ?
Only the context you create when you call ExecJS.compile is preserved between evals. Anything you want preserved needs to be part of the initial compile.

Jade in ExecJS context

Very simply, I am having trouble running the compiled jade.js from jade-lang file in execjs.
If I include jade.js in an html file
jade.compile
returns a function.
When I do the following:
require 'execjs'
source = <<-src
var window = {};
#{open('jade.js').read}
var jade = window.jade;
src
context = ExecJS.compile source
context.eval "jade.compile"
=> nil
nil gets returned by eval.
Any idea where I'm going wrong? I've searched stack overflow, and even found a few gems that do this the same way as I am doing it, but when I execute on my system, it doesn't work. I've tried with therubyracer and node as my execjs runtimes.
Thanks for your help!
It appears that when the return from execjs is a function, it returns nil. So:
context.eval "jade.compile.toString()"
=> "function....."
Likewile
context.eval "jade.compile(#{template.to_json}).toString()"
=> "function...."
and
context.eval "jade.compile(#{template.to_json})()"
=> "some html"
This makes sense since the extract_result method of execjs/external_runtime.rb (line 62 as of this writing) uses JSON.decode, which will not decode a function. Only a derivative of object.

Resources