I need to build the following xml data
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7a.services.chrome.com">
<soapenv:Header/>
<soapenv:Body>
<urn:VehicleDescriptionRequest>
<urn:accountInfo number="test" secret="test" country="US" language="en" behalfOf="test"/>
<!--You have a CHOICE of the next 3 items at this level-->
<!--Optional:-->
<urn:styleId>test</urn:styleId>
<urn:switch>ShowAvailableEquipment</urn:switch>
<urn:includeMediaGallery>Both</urn:includeMediaGallery>
</urn:VehicleDescriptionRequest>
</soapenv:Body>
</soapenv:Envelope>
I am using the following code to build it
def self.xml_build(style_id,number,secret,behalfOf)
builder = Nokogiri::XML::Builder.new do |xml|
xml.send("soapenv:Envelope"){
xml.send("soapenv:Header")
xml.send("soapenv:Body") {
xml.send("urn:VehicleDescriptionRequest"){
xml.send("urn:accountInfo")("number"=>"#{number}" "secret"=>"#{secret}" "country"=>"US" "language"=>"en" "behalfOf"=>"#{behalfOf}")
xml.send("urn:styleId"){xml.text "#{style_id}"}
xml.send("urn:switch"){xml.text "ShowAvailableEquipment"}
xml.send("urn:includeMediaGallery"){xml.text "Both"}
}
}
}
end
file_test=File.new("dummy_file", 'w')
file_test.puts builder.to_xml
builder.to_xml
end
I am getting the following error
SyntaxError: /home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected '(', expecting '}'
... xml.send("urn:accountInfo")("number"=>"#{number}" "secret...
... ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...d("urn:accountInfo")("number"=>"#{number}" "secret"=>"#{secr...
... ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...number"=>"#{number}" "secret"=>"#{secret}" "country"=>"US" "...
... ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...ecret"=>"#{secret}" "country"=>"US" "language"=>"en" "behalf...
... ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...}" "country"=>"US" "language"=>"en" "behalfOf"=>"#{behalfOf}...
... ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
..." "language"=>"en" "behalfOf"=>"#{behalfOf}")
Also I am not sure how to build this part of the xml:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7a.services.chrome.com">
The following will generate your xml:
require 'nokogiri'
def self.xml_build(style_id,number,secret,behalfOf)
builder = Nokogiri::XML::Builder.new do |xml|
xml.send("soapenv:Envelope", 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:urn' => "urn:description7a.services.chrome.com"){
xml.send("soapenv:Header")
xml.send("soapenv:Body") {
xml.send("urn:VehicleDescriptionRequest"){
xml.send("urn:accountInfo", "number"=>"#{number}", "secret"=>"#{secret}", "country"=>"US", "language"=>"en", "behalfOf"=>"#{behalfOf}")
xml.comment('You have a CHOICE of the next 3 items at this level')
xml.comment('Optional:')
xml.send("urn:styleId"){xml.text "#{style_id}"}
xml.send("urn:switch"){xml.text "ShowAvailableEquipment"}
xml.send("urn:includeMediaGallery"){xml.text "Both"}
}
}
}
end
end
puts xml_build('test', 'test', 'test', 'test').to_xml
#=> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7a.services.chrome.com">
#=> <soapenv:Header/>
#=> <soapenv:Body>
#=> <urn:VehicleDescriptionRequest>
#=> <urn:accountInfo number="test" secret="test" country="US" language="en" behalfOf="test"/>
#=> <!--You have a CHOICE of the next 3 items at this level-->
#=> <!--Optional:-->
#=> <urn:styleId>test</urn:styleId>
#=> <urn:switch>ShowAvailableEquipment</urn:switch>
#=> <urn:includeMediaGallery>Both</urn:includeMediaGallery>
#=> </urn:VehicleDescriptionRequest>
#=> </soapenv:Body>
#=> </soapenv:Envelope>
The exception you were seeing was due to how you were trying to create the attributes of the accountInfo node. Note that the attributes should be passed as arguments to the send method:
xml.send("urn:accountInfo", "number"=>"#{number}", "secret"=>"#{secret}", "country"=>"US", "language"=>"en", "behalfOf"=>"#{behalfOf}")
The same can be done to get the attributes on the Envelope node:
xml.send("soapenv:Envelope", 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:urn' => "urn:description7a.services.chrome.com")
Finally, to get the comments, use the comment method:
xml.comment('You have a CHOICE of the next 3 items at this level')
xml.comment('Optional:')
Related
I'm attempting to seed my database using the Faker gem, but am getting some error messages and can't see where I'm going wrong. My seeds.rb is:
10.times do
List.create! (
name: Faker::Company.buzzword,
shared_with: Faker::Internet.email,
user_id: 3
)
end
50.times do
Item.create! (
name: Faker::Company.bs,
list_id: Faker::Number.between(1, 10),
delegated_to: Faker::Internet.email,
user_id: 3
)
end
puts "Seed finished"
puts "#{List.count} lists created"
puts "#{Item.count} items created"
And the error messages are:
rake aborted!
SyntaxError: /Users/.../db/seeds.rb:3: syntax error, unexpected tLABEL
name: Faker::Company.buzzword,
^
/Users/.../db/seeds.rb:4: syntax error, unexpected tLABEL, expecting '='
shared_with: Faker::Internet.email,
^
/Users/.../db/seeds.rb:5: syntax error, unexpected tLABEL, expecting '='
user_id: 3
^
/Users/.../db/seeds.rb:11: syntax error, unexpected tLABEL
name: Faker::Company.bs,
^
/Users/.../db/seeds.rb:12: syntax error, unexpected tLABEL, expecting '='
list_id: Faker::Number.between(1, 10),
^
/Users/.../db/seeds.rb:12: syntax error, unexpected ',', expecting keyword_end
/Users/.../db/seeds.rb:14: syntax error, unexpected tLABEL, expecting '='
user_id: 3
^
/Users/.../db/seeds.rb:20: syntax error, unexpected end-of-input, expecting keyword_end
Can anyone clue me in to where I'm going wrong?
In Ruby you should never put whitespace between a method name and the opening parenthesis.
# Syntax error
List.create! (
# Correct
List.create!(
So to expand, your code should look like:
10.times do
List.create!(
name: Faker::Company.buzzword,
shared_with: Faker::Internet.email,
user_id: 3
)
end
I would like to check if the xml is valid. So, here is my code
require 'rexml/document'
begin
def valid_xml?(xml)
REXML::Document.new(xml)
rescue REXML::ParseException
return nil
end
bad_xml_2=%{aasdasdasd}
if(valid_xml?(bad_xml_2) == nil)
puts("bad xml")
raise "bad xml"
end
puts("good_xml")
rescue Exception => e
puts("exception" + e.message)
end
and it returns good_xml as result. Did I do something wrong? It will return bad_xml if the string is
bad_xml = %{
<tasks>
<pending>
<entry>Grocery Shopping</entry>
<done>
<entry>Dry Cleaning</entry>
</tasks>}
Personally, I'd recommend using Nokogiri, as it's the defacto standard for XML/HTML parsing in Ruby. Using it to parse a malformed document:
require 'nokogiri'
doc = Nokogiri::XML('<xml><foo><bar></xml>')
doc.errors # => [#<Nokogiri::XML::SyntaxError: Opening and ending tag mismatch: bar line 1 and xml>, #<Nokogiri::XML::SyntaxError: Premature end of data in tag foo line 1>, #<Nokogiri::XML::SyntaxError: Premature end of data in tag xml line 1>]
If I parse a document that is well-formed:
doc = Nokogiri::XML('<xml><foo/><bar/></xml>')
doc.errors # => []
REXML treats a simple string as a valid XML with no root node:
xml = REXML::Document.new('aasdasdasd')
# => <UNDEFINED> ... </>
It does not however treat illegal XML (with mismatching tags, for example) as a valid XML, and throws an exception.
REXML::Document.new(bad_xml)
# REXML::ParseException: #<REXML::ParseException: Missing end tag for 'done' (got "tasks")
It is missing an end-tag to <done> - so it is not valid.
In my profile.html.erb file I get a syntax error anytime I try to assign a class or id to erb. Below is an example:
<p>"<%= current_user.current_program.name, :id => 'progress' %>" Progress</p>
This gives me the following error:
SyntaxError in Users#profile
Showing /.../app/views/users/profile.html.erb where line #13 raised:
/Users/.../app/views/users/profile.html.erb:13: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
...er.current_program.name, :id => 'progress' );#output_buffer....
... ^
I can't figure out what the syntax error is. I'm totally stumped.
We can reproduce and simplify your problem in a standalone Ruby like so:
require 'erb'
ERB.new("<p><%= name, :a => 'b' %></p>").run
Producing the error:
SyntaxError: (erb):1: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
..."; _erbout.concat(( name, :a => 'b' ).to_s); _erbout.concat ...
... ^
from /Users/phrogz/.../ruby/1.9.1/erb.rb:838:in `eval'
from /Users/phrogz/.../ruby/1.9.1/erb.rb:838:in `result'
from /Users/phrogz/.../ruby/1.9.1/erb.rb:820:in `run'
from (irb):2
from /Users/phrogz/.../bin/irb:16:in `<main>'
Even more simply, taking ERB out of the mix:
a, :b=>'c'
#=> SyntaxError: (irb):3: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
What you have just isn't valid Ruby code. What were you trying to do there? Pass the :id => 'progress' hash as a parameter to the .name method? If so, then drop the comma, and (optionally) include parentheses for clarity:
<p>"<%= current_user.current_program.name( :id=>'progress' ) %>" Progress</p>
And if you're using Ruby 1.9+, you can use the simpler Hash-with-symbol-keys syntax:
<p>"<%= current_user.current_program.name( id:'progress' ) %>" Progress</p>
However, it seems unlikely to me that the name method takes such a hash, so I ask again: what are you really trying to accomplish? What does the name method return, and what HTML output do you want?
Taking a guess, maybe you wanted the text returned by .name to be wrapped in <span id="progress">? If so, you must do so like:
<p>"<span id="progress"><%= current_user.current_program.name%></span>" Progress</p>
Or perhaps using content_tag:
<p><%= content_tag("span", current_user.current_program.name, id:'progress') %> Progress</p>
In Haml this would be:
%p
%span#progress= current_user.current_program.name
Progress
maybe if you remove the comma it will work (is current_user.current_program.name a method that takes a hash as a parameter?)
I want to make a node that has both text content and an attribute with Nokogiri. E.g. I want to produce XML:
<root blah="value">text content</root>
I try to do this with:
Nokogiri::XML::Builder.new do
root(:blah=>"value") "text content"
end
But Ruby complains with:
create-config.rb:8: syntax error, unexpected tSTRING_BEG, expecting keyword_end
root(:blah => "value") "text content"
What am I doing wrong?
I found the solution. I had to use {} and text
Nokogiri::XML::Builder.new do
root(:blah => "value") {
text("text content")
}
end
Not exactly sure what the errors are indicating. I am getting the following syntax errors:
compile error
app/views/students/student_fail.html.haml:33: syntax error, unexpected tIDENTIFIER, expecting ')'
... :student_fail_attribute params[:StudentFailState.true], pa...
^
app/views/students/student_fail.html.haml:33: syntax error, unexpected ')', expecting '='
...method], params[:text_method])
^
app/views/students/student_fail.html.haml:39: syntax error, unexpected kENSURE, expecting kEND
...\n", -2, false);_erbout;ensure;#haml_buffer = #haml_buffer.u...
^
app/views/students/student_fail.html.haml:40: syntax error, unexpected kENSURE, expecting kEND
app/views/students/student_fail.html.haml:42: syntax error, unexpected $end, expecting kEND
Here's the html:
:ruby
fields = if #step == 1
[ select_tag(:id, options_from_collection_for_select(Student.passed, :id, :selector_label, #student.id), :size => 10) ]
elsif #step == 2
form_for #student do |f| f.collection_select( :student_fail_attribute params[:StudentFailState.true], params[:value_method], params[:text_method])
end
#fields << render_sequence_nav(sequence_info, students_path)
fields << render(:partial => "resources_partials/sequence/nav", :locals => sequence_info.merge({:cancel_url => {:controller => :dashboard}}))
= render_form { fields }
Thanks for any response.
I think you are missing a comma between :student_fail_attribute and params[:StudentFailState.true].
You might want to think if params[:StudentFailState.true] is supposed to be there at all, unless it returns the collection you will most likely not get the expected results.