How can I extract/parse from this SOAP envelope using Ruby? - ruby

I am using a gem which uses soap/wsdlDriver.
When I post I get back a SOAP response and am unable to easily parse it.
This is the response I get back:
#<SOAP::Mapping::Object:0x159e95faf098 {}id="27b907f8-da51-f611-ab02-4c5f88a8ec8
8" {}error=#<SOAP::Mapping::Object:0x159e95fae33c {}number="0" {}name="No Error"
{}description="No Error">>
I need to get the entire value in the id="xxxx"
This is what on get on heroku (note: it works locally). This comes from testing various variations of response.id (where response.inspect is what created the output above)
f"
{}error=#>
response[id]
/disk1/home/slugs/220752_47a08bb_10e7/mnt/app/controllers/sugarcrm_controller.rb
:77: warning: Object#id will be
deprecated; use Object#object_id nil
response.id:
/disk1/home/slugs/220752_47a08bb_10e7/mnt/app/controllers/sugarcrm_controller.rb
:79: warning: Object#id will be
deprecated; use Object#object_id
23891500658740
/disk1/home/slugs/220752_47a08bb_10e7/mnt/app/controllers/sugarcrm_controller.rb
:80: warning: Object#id will be
deprecated; use Object#object_id this
is the contact_id: 23891500658740
events:

Ok, I'm 95% sure that is the output of SOAP::Mapping::Object#inspect and not the actual response. And from that class it looks you use the [] method to pull out attributes.
So if I am reading that right, then it looks like you might want:
response_id = response_object['id']
Though each attribute being prefaced with {} seems pretty odd. So if that is actually part of the attribute name, you may need:
response_id = response_object['{}id']
But that seems pretty strange, and may indicate that the SOAP library you are using is not parsing the response properly.
Disclaimer: I've never used this lib before, and posted this from just perusing the docs... This may or may not be very accurate in the ways using this class is described.

I don't know how to do this with ruby, but may be like this code in javascript.
var t = "<SOAP message........>";
var id = t.replace(/.*id=\"(.*?)\".*/, "$1");
Regex details:
.*id=\"(.*?)\".*
.* = anything, 0 or N times.
(.*?) = anything, 0 or N times, stopping at first match.
$1 = first group, the content inside ().
So, everthing will be replaced by id content.

Related

Kamailio 4.4 seturi Only Accepts Explicit Strings?

I've been working at implementing a simple serial forking described in the TM module's documentation (the Q values are stored as a priority weight in a mysql table) where my proxy is querying a database to determine to what domain to forward to.
I've verified through extensive use of xlog that a variable I'm using to build the new URI to use with seturi is getting everything correctly. I use an append_branch call in a subsequent while loop iterating over my sql query results, which doesn't have any problems with taking a very similarly formatted parameter. However, when I go to restart Kamailio it simply gripes at me that a string is expected. The line it corresponds to from console is just the seturi call. I've tried casting as a string, but that doesn't seem to be part of 4.4 (or my syntax is wrong).
I've thought about building the URI strings and storing into avp, but I suspect I'd have the same problem.
For reference, this is what I'm doing:
$var(basedest) = "sip:" + $var(number) + "#" + $(dbr(destination=>[0,0]))+ ":" + $var(port);
seturi($var(basedest));
And what it's outputting when trying to load the config:
<core> [cfg.y:3368]: yyerror_at(): parse error in config file //etc/kamailio/kamailio.cfg, line 570, column 9-22: syntax error
<core> [cfg.y:3371]: yyerror_at(): parse error in config file //etc/kamailio/kamailio.cfg, line 570, column 23: bad argument, string expected
Naturally, when I put $var(basedest) in double quotes, it's literally interpreted as a string. Single quotes behave similarly. Is there something I can do to work around this? When I feed it an explicit hardcoded string, it's happy as a can be and the routing works fine. When I try to do something very simple like the above, it gets upset. If possible, I'd like to avoid updating as I initially grabbed Kamailio from the yum repo.
Thanks in advance - this has been bugging me a good while.
Apparently, not a new problem. I ended up finding out what I can do to work around it.
For reference, seturi and $ru pseudo variable refer to the same thing. So basically you'd just do:
$var(mynewru) = "sip:user#domain:5060";
$ru = $var(mynewru);
This would achieve the same thing I was originally attempting to do before based on the TM module's documentation. For serial forking, issuing some number of append_branch calls is fine.

Watir Check Text exists

I get it not the Watir in conjunction with rspec find my text.
The following code leads to this error.
Code:browser.text.include?("Coverberechnung").should == true
Error1: expected: true got: false (using ==)
Error2: Using should from rspec-expectations' old :should syntax
without explicitly enabling the syntax is deprecated. Use the new
:expect syntax or explicitly enable :should instead. Called from
Maybe I can have a help
URL for the Site: enter link description here
You're looking for an initially-capitalized string (i.e. Coverberechnung), but that string is all-capitalized on the test site (i.e. COVERBERECHNUNG).
Try:
browser.text.include?("COVERBERECHNUNG").should == true
or (using expect syntax)
expect(browser.text.include?("COVERBERECHNUNG")).to be true
I prefer to use
expect(browser.text).to include('coverberechnung')
If I wanted to be indifferent about case I would do something like this:
browser.text.upcase.include?("COVERBERECHNUNG").should == true
or
browser.text.downcase.include?("coverberechnung").should == true
this way you can avoid text comparison that may have varying cases.
also for you last problem #3:
use
expect(browser.text.downcase.include?("coverberechnung")).to be true
they deprecated that version some time ago. so you can give this one a try with no issue.
NOTE: only one caveat is that this will ignore case. As described above.
Or you can just do the following:
fail unless #browser.text.include? 'COVERBERECHNUNG'
Or if you want to target that exact string, you could do the following instead:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present
This code will raise an exception after 30 seconds (thus, failing your test in the process) if it can't find a header element with the text: 'COVERBERECHNUNG'. You can also override the waiting or polling process by doing the following:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present(10)
That code will check that h1 element within 10 seconds.

Ruby ActiveRecord: DEPRECATION WARNING: Relation#find_in_batches with finder options is deprecated

I'm implementing Model.find_each as mentioned here:
http://edgeguides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches
but I'm getting this error message:
Ruby ActiveRecord: DEPRECATION WARNING: Relation#find_in_batches with finder options is deprecated. Please build a scope and then call find_in_batches on it instead.
for this code:
Person.find_each(start: start_index, limit: limit) do |person|
I'm pretty much following the code given in the documentation so I'm a little puzzled. Is this code correct and, if not, what's the fix?
Change limit: limit for batch_size: limit
EDIT
Since you need a limit and your batch is based on id, i think you can do something like:
Person.where("id < ?", (start_index + limit)).find_each(start: start_index) do |person|
I don't get exactly the issue but I think you can just write this:
Person.offset(start_index).limit(limit).find_each(batch_size: NUMBER) do |person|
find_each is just a replacement for each, it's the last thing you should do on a query and you should use it only to choose how many results will be loaded in memory (batch_size).
For offsets/limits you should use the usual query API.
Important: Notice that find_each has nothing to do with find, find_each replaces each, which should be used to loop over records, not to find/filter/what else.
Also, is clearly written in the guide:
The find_each method accepts most of the options allowed by the
regular find method, except for :order and :limit, which are reserved
for internal use by find_each.
So you can't use :limit. In any case I think there is something wrong in the guide: you should use find_each for looping, not for searching.
Notice also that :start is used to restart at some point an interrupted job (it's not directly and id like 2000, it's the 2000 element in a batch of 5000 for example), much like the docs states:
By default, records are fetched in ascending order of the primary key,
which must be an integer. The :start option allows you to configure
the first ID of the sequence whenever the lowest ID is not the one you
need. This would be useful, for example, if you wanted to resume an
interrupted batch process, provided you saved the last processed ID as
a checkpoint.
You should not use it to replace offset and limit methods from ActiveRecord

Get full path in Sinatra route including everything after question mark

I have the following path:
http://192.168.56.10:4567/browse/foo/bar?x=100&y=200
I want absolutely everything that comes after "http://192.168.56.10:4567/browse/" in a string.
Using a splat doesn't work (only catches "foo/bar"):
get '/browse/*' do
Neither does the regular expression (also only catches "foo/bar"):
get %r{/browse/(.*)} do
The x and y params are all accessible in the params hash, but doing a .map on the ones I want seems unreasonable and un-ruby-like (also, this is just an example.. my params are actually very dynamic and numerous). Is there a better way to do this?
More info: my path looks this way because it is communicating with an API and I use the route to determine the API call I will make. I need the string to look this way.
If you are willing to ignore hash tag in path param this should work(BTW browser would ignore anything after hash in URL)
updated answer
get "/browse/*" do
p "#{request.path}?#{request.query_string}".split("browse/")[1]
end
Or even simpler
request.fullpath.split("browse/")[1]
get "/browse/*" do
a = "#{params[:splat]}?#{request.env['rack.request.query_string']}"
"Got #{a}"
end

Issues with Sinatra and Heroku

So I've created and published a Sinatra app to Heroku without any issues. I've even tested it locally with rackup to make sure it functions fine. There are a series of API calls to various places after a zip code is consumed from the URL, but Heroku just wants to tell me there is an server error.
I've added an error page that tries to give me more description, however, it tells me it can't perform a `count' for #, which I assume means hash. Here's the code that I think it's trying to execute...
if weather_doc.root.elements["weather"].children.count > 1
curr_temp = weather_doc.root.elements["weather/current_conditions/temp_f"].attributes["data"]
else
raise error(404, "Not A Valid Zip Code!")
end
If anyone wants to bang on it, it can be reached at, http://quiet-journey-14.heroku.com/ , but there's not much to be had.
Hash doesn't have a count method. It has a length method. If # really does refer to a hash object, then the problem is that you're calling a method that doesn't exist.
That # doesn't refer to Hash, it's the first character of #<Array:0x2b2080a3e028>. The part between the < and > is not shown in browsers (hiding the tags themselves), but visible with View Source.
Your real problem is not related to Ruby though, but to your navigation in the HTML or XML document (via DOM). Your statement
weather_doc.root.elements["weather"].children.count > 1
navigates the HTML/XML document, selecting the 'weather' elements, and (tries to) count the children. The result of the children call does not have a method count. Use length instead.
BTW, are you sure that the document contains a tag <weather>? Because that's what your're trying to select.
If you want to see what's behind #, try
raise probably_hash.class.to_s

Resources