Ruby Command not return expected value - ruby

In my redhat 5 RoR, i have a command calling a jar to pull xml from another system.
irb:
2.1.5 :005 > %x(java -jar ./GENTESTResult.jar -action=status -user="tester" -password="tester" -job_id="12324")
<?xml version="1.0"?><cliout action="status" request_id="1446456310693_54" >
<action_status>ERROR</action_status>
<action_status_detail>Given job not found.</action_status_detail><job id="12324"/>
</cliout>
=> ""
2.1.5 :006 > %x(java -jar ./GENTESTResult.jar -action=status -user="tester" -password="tester" -job_id="5192782")
=> "<?xml version=\"1.0\"?><cliout action=\"status\" request_id=\"1446456368168_11\" >\n<action_status>SUCCESS</action_status><action_status_detail>Job has been processed successfully.</action_status_detail><job id=\"5192782\" status=\"Failed\" >\n...(stop paste)
the first job id "12324" is not exist. the second job id 5192782 is a valid id.
I don't have question on the job 5192782.
But for job "12324", my ruby code
#lines = %x(java -jar ./GENTESTResult.jar -action=status -user="tester" -password="tester" -job_id="12324")
gave me empty string. but i really need the xml that from the result key part which is
<?xml version="1.0"?><cliout action="status" request_id="1446456310693_54" >
<action_status>ERROR</action_status>
<action_status_detail>Given job not found.</action_status_detail><job id="12324"/>
</cliout>
How can i get the string? Thank you in advance.

You are not catching the output because it's being printed on stderr.
http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html is a comprehensive blog post about the subject, you might want to look at popen3 and popen2e in particular

Related

Where is the ruby function 'powershell' defined?

I am using the msutter DSC module for puppet. While reading through the source code, I come across code like this (in dsc_configuration_provider.rb):
def create
Puppet.debug "\n" + ps_script_content('set')
output = powershell(ps_script_content('set'))
Puppet.debug output
end
What file defines the powershell function or method? Is it a ruby builtin? A puppet builtin? Inherited from a class? I know that it is being used to send text to powershell as a command and gathering results, but I need to see the source code to understand how to improve its error logging for my purposes, because certain powershell errors are being swallowed and no warnings are being printed to the Puppet log.
These lines in file dsc_provider_helpers.rb may be relevant:
provider.commands :powershell =>
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end
Surely this defines where the Powershell executable is located, but gives no indication how it is called and how its return value is derived. Are stdout and stderr combined? Am I given the text output or just the error code? etc.
This is core Puppet logic. When a provider has a command, like
commands :powershell => some binary
That is hooked up as a function powershell(*args).
You can see it with other providers like Chocolatey:
commands :chocolatey => chocolatey_command
def self.chocolatey_command
if Puppet::Util::Platform.windows?
# must determine how to get to params in ruby
#default_location = $chocolatey::params::install_location || ENV['ALLUSERSPROFILE'] + '\chocolatey'
chocopath = ENV['ChocolateyInstall'] ||
('C:\Chocolatey' if File.directory?('C:\Chocolatey')) ||
('C:\ProgramData\chocolatey' if File.directory?('C:\ProgramData\chocolatey')) ||
"#{ENV['ALLUSERSPROFILE']}\chocolatey"
chocopath += '\bin\choco.exe'
else
chocopath = 'choco.exe'
end
chocopath
end
Then other locations can just call chocolatey like a function with args:
chocolatey(*args)

Create a file descriptor in ruby

I am writing a script will perform various tasks with DSV or positional files. These tasks varies and are like creating an DB table for the file, or creating a shell script for parsing it.
As I have idealized my script would receive a "descriptor" as input to perform its tasks. It then would parse this descriptor and perform its tasks accordingly.
I came up with some ideas on how to specify the descriptor file, but didn't really manage to get something robust - probably due my inexperience in ruby.
It seems though, the best way to parse the descriptor would be using ruby language itself and then somehow catch parsing exceptions to turn into something more relevant to the context.
Example:
The file I will be reading looks like (myfile.dsv):
jhon,12343535,27/04/1984
dave,53245265,30/03/1977
...
Descriptor file myfile.des contains:
FILE_TYPE = "DSV"
DSV_SEPARATOR = ","
FIELDS = [
name => [:pos => 0, :type => "string"],
phone => [:pos => 1, :type => "number"],
birthdate => [:pos => 2, :type => "date", :mask = "dd/mm/yyyy"]
]
And the usage should be:
ruby script.rb myfile.des --task GenerateTable
So the program script.rb should load and parse the descriptor myfile.des and perform whatever tasks accordingly.
Any ideas on how to perform this?
Use YAML
Instead of rolling your own, use YAML from the standard library.
Sample YAML File
Name your file something like descriptor.yml, and fill it with:
---
:file_type: DSV
:dsv_separator: ","
:fields:
:name:
:pos: 0
:type: string
:phone:
:pos: 1
:type: number
:birthdate:
:pos: 2
:type: date
:mask: dd/mm/yyyy
Loading YAML
You can read your configuration back in with:
require 'yaml'
settings = YAML.load_file 'descriptor.yml'
This will return a settings Hash like:
{:file_type=>"DSV",
:dsv_separator=>",",
:fields=>
{:name=>{:pos=>0, :type=>"string"},
:phone=>{:pos=>1, :type=>"number"},
:birthdate=>{:pos=>2, :type=>"date", :mask=>"dd/mm/yyyy"}}}
which you can then access as needed to configure your application.

Search command output for string minitest

I'm trying to create a minitest for chef (minitest::spec) and I'm a little lost at how to accomplish what I want in ruby.
What I want to do is have the code run the 'date' command and then check if the output contains 'UTC'.
I have this so far, but I don't know how to check the output for 'true':
it "uses the correct timezone" do
timezone_check = shell_out("date")
timezone_check.to_s.include?('UTC')
end
I tried to use .must_output, but I don't know how to incorporate it. Is this even the best way to accomplish what I want?
Any input is appreciated!
Thanks.
EDIT: I have now tried this:
it "uses the correct timezone" do
date_input = `date`
proc { puts date_input.to_s }.must_output /UTC/
end
but it results in this:
Failure:
test_0002_uses_the_correct_timezone(recipe::base::default) [/var/chef/minitest/base/default_test.rb:18]:
In stdout.
--- expected
+++ actual
## -1 +1,2 ##
-/UTC/
+"Fri Apr 19 17:50:27 UTC 2013
+"
Testing shell_out requires you to test against stdout
it "uses the correct timezone" do
timezone_check = shell_out("date")
timezone_check.stdout.must_match /UTC/
end
For more examples check out Cookbook Integration Testing
Wrap it in a proc and try using must_output. The test would probably look like:
it "uses the correct timezone" do
proc { timezone_check = shell_out("date") }.should_output /UTC/
end
Not entirely sure from the documentation that the should_output method will accept a pattern, but if you can write the test such that you know precisely the entire output expected, then you can simply test against the full expected string. E.g.
it "uses the correct timezone" do
proc { timezone_check = shell_out("date") }.should_output("Fri Apr 19 12:33:13 CDT 2013")
end

Handle json in ruby

In ruby how to convert the follwing into valid json
"{\"transferType\"=>\"D\", \"accountNumber\"=>\"123\", \"employeeId\"=>\"12\", \"districtNumber\"=>\"15\", \"orderedBy\"=>\"vbcb\", \"department\"=>\"vghghj\", \"po\"=>\"23434\", \"Phone\"=>\"4542433435\", \"Instructions\"=>\"vbvcnvbnb\"}"
The class of above is string. Because Without proper Json conversion i am problem like
Javascript replace for equal symbol
First of all you should make this in to proper JSON format. So replace the => in to :
and then try JSON.load or JSON.parse will work successfully.
2.2.3 :015 > data
=> "{\"transferType\"=>\"D\", \"accountNumber\"=>\"123\", \"employeeId\"=>\"12\", \"districtNumber\"=>\"15\", \"orderedBy\"=>\"vbcb\", \"department\"=>\"vghghj\", \"po\"=>\"23434\", \"Phone\"=>\"4542433435\", \"Instructions\"=>\"vbvcnvbnb\"}"
2.2.3 :016 > JSON.parse(data.gsub("=>", ":"))
=> {"transferType"=>"D", "accountNumber"=>"123", "employeeId"=>"12", "districtNumber"=>"15", "orderedBy"=>"vbcb", "department"=>"vghghj", "po"=>"23434", "Phone"=>"4542433435", "Instructions"=>"vbvcnvbnb"}
from the result you can do anything what do you want :)

How to change the prompt

I am trying to configure the prompt characters in ripl, an alternative to interactive ruby (irb). In irb, it is done using IRB.conf[:DEFAULT], but it does not seem to work with ripl. I am also having difficulty finding an instruction for it. Please guide to a link for an explanation or give a brief explanation.
Configuring a dynamic prompt in ~/.riplrc:
# Shows current directory
Ripl.config[:prompt] = lambda { Dir.pwd + '> ' }
# Print current line number
Ripl.config[:prompt] = lambda { "ripl(#{Ripl.shell.line})> " }
# Simple string prommpt
Ripl.config[:prompt] = '>>> '
Changing the prompt in the shell:
>> Ripl.shell.prompt = lambda { Dir.pwd + '> ' }
ripl loads your ~/.irbrc file, which
typically contains some irb specific
options (e.g. IRB.conf[:PROMPT]). To
avoid errors, you can install
ripl-irb, which catches calls to the
IRB constant and prints messages to
convert irb configuration to ripl
equivalents.
http://rbjl.net/44-ripl-why-should-you-use-an-irb-alternative

Resources