Hash manipulation in Ruby - ruby-on-rails

Quick ruby question regarding the manipulation of a Hash in Ruby.
I actually have the following hash:
[2] project(#<V1::UsersController>) » error.info
=> {
:id => "914a24888-5e71-4d12-b9b0-10e2d98f516b",
:game => "vampotron",
:data => {
"private" => {
"name" => "Jean",
"logins" => 2300,
"foo" => "bar"
}
},
:revision => 1
}
I want the hash to become:
[2] project(#<V1::UsersController>) » error.info
=> {
:id => "914a24888-5e71-4d12-b9b0-10e2d98f516b",
:game => "vampotron",
:data => {
"name" => "Jean",
"logins" => 2300,
"foo" => "bar"
},
:revision => 1
}
I would like to remove the 'private' key by keeping the existing k,v pairs in my 'data' hash.
Thanks for your help,
M

The easiest way
hash[:data] = hash[:data]['private']

Related

ARRAY ACCESS AND INSERTION RECORD DATABASE

I would need to access the array to retrieve the information and insert it into the database (Code, Customer, Phone1, Phone2). Someone can help me?
{
:recordset => {
:row => [
[0] {
:property => [
[0] {
:name => "Code",
:value => "C0001"
},
[1] {
:name => "Customer",
:value => "ROSSI MARIO"
},
[2] {
:name => "Phone1",
:value => "1234567890"
}
]
},
[1] {
:property => [
[0] {
:name => "Code",
:value => "C0002"
},
[1] {
:name => "Customer",
:value => "VERDE VINCENT"
},
[2] {
:name => "Phone1",
:value => "9876543210"
},
[3] {
:name => "Phone2",
:value => "2468101214"
}
]
}
]
},
:#xmlns => "http://localhost/test"
}
p.s. The Phone2 value during the SOAP call is only displayed if it is present in an archive
Thank you
Your data doesn't look like a valid ruby structure. Once you are able to convert it to be more ruby-like, you can make use of each_with_object to generate a well formatted set of attributes and their values from your data:
data
=> {:recordset=>{
:row=>[{
:property=>[
{:name=>"Code", :value=>"C0001"},
{:name=>"Customer", :value=>"ROSSI MARIO"},
{:name=>"Phone1", :value=>"1234567890"}
]
}, {
:property=>[
{:name=>"Code", :value=>"C0002"},
{:name=>"Customer", :value=>"VERDE VINCENT"},
{:name=>"Phone1", :value=>"9876543210"},
{:name=>"Phone2", :value=>"2468101214"}
]
}]
},
:#xmlns=>"http://localhost/test"}
data.keys
=> [:recordset, :#xmlns]
data[:recordset][:row].count
=> 2 # There are 2 set of attribute-value pairs
result = data[:recordset][:row].each_with_object([]) do |hash, out|
out << hash[:property].each_with_object({}) do |h, o|
o[h[:name]] = h[:value]
end
end
=> [{"Code"=>"C0001", "Customer"=>"ROSSI MARIO", "Phone1"=>"1234567890"},
{"Code"=>"C0002", "Customer"=>"VERDE VINCENT", "Phone1"=>"9876543210", "Phone2"=>"2468101214"}]
Now, you can iterate over result and create respective records in database.

Why am I getting singleton cant be dumped err?

When I try to add a variable value into session, I am getting singleton cant be dumped err.
Here is the value in the varibale
[
[0] {
:id => "574ecb43a7a5bb44c000443b",
:_id => "574ecb43a7a5bb44c000443b",
:active => true,
:capabilities => {
:network_connections => [
[0] {
:type => "ethernet-wan",
:name => "wan"
},
[1] {
:type => "ethernet-lan",
:name => "lan"
},
[2] {
:type => "wifi",
:name => "wlan"
},
[3] {
:type => "cellular",
:name => "wwan"
}
]
},
:commander_ids => [],
:created_at => "2016-05-11T15:46:12+00:00",
:deleted_at => nil,
:firmware_upgradable => true,
:ingestor_ids => [],
:last_known_translator_port => nil,
:long_description => "this is a liong desc",
:manufacturer => "test_sushant",
:model => "sushant_test",
:name => "zxc",
:parent_gateway_data_source_type_id => nil,
:rule_ids => [],
:software => "qwe",
:translator => "edge",
:type => "asd",
:updated_at => "2016-05-11T15:46:12+00:00",
:user_id => "572adee5a7a5bb320b000852"
},
The variable is an array of objects. I do not know why this is causing an err.

Ruby on Rails Adobe EchoSign SOAP Request

I am trying to use Ruby on Rails to create a SOAP request to the EchoSign API but I am getting an error saying one of the fields is blank:
{http://dto14.api.echosign}RecipientRole: cannot accept ''
This is the code I am using:
require 'soap/wsdlDriver'
require 'base64'
filename = 'quote'
recipient = 'martin#domain.co.uk'
quote_id = params[:id]
$S = SOAP::WSDLDriverFactory.new("https://secure.echosign.com/services/EchoSignDocumentService16?wsdl").create_rpc_driver
r = $S.sendDocument(
:apiKey => 'XXXXXXXXXXX',
:senderInfo => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:documentCreationInfo => {
:fileInfos => [{
:file => RAILS_ROOT + '/pdfs/' + quote_id + '.pdf',
:fileName => filename,
}],
:recipients =>
[{ :RecipientInfo => [{
:email => 'martin#domain.co.uk',
:fax => SOAP::SOAPNil.new,
:role => 'SIGNER'
}]
}],
:message => "This is neat.",
:name => "Test from SOAP-Lite: " + filename,
:reminderFrequency => "WEEKLY_UNTIL_SIGNED",
:signatureFlow => "SENDER_SIGNATURE_NOT_REQUIRED",
:signatureType => "ESIGN",
:callbackInfo => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:ccs => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:externalId => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:securityOptions => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
}
)
There must obviously be something wrong with the way I am doing the
:recipients =>
[{ :RecipientInfo => [{
:email => 'martin#domain.co.uk',
:fax => SOAP::SOAPNil.new,
:role => 'SIGNER'
}]
}],
but I am finding it extremely difficult to find what just from the documentation, I think it will be something silly but can't spot it.
update
after looking more closely at the example request I tried the following but it made no difference:
require 'soap/wsdlDriver'
require 'base64'
filename = 'quote'
quote_id = params[:id]
$S = SOAP::WSDLDriverFactory.new("https://secure.echosign.com/services/EchoSignDocumentService16?wsdl").create_rpc_driver
r = $S.sendDocument(
:apiKey => 'XXXXXXXXXXX',
:senderInfo => SOAP::SOAPNil.new,
:documentCreationInfo => {
:fileInfos => {
:FileInfo => {
:file => RAILS_ROOT + '/pdfs/' + quote_id + '.pdf',
:fileName => filename
}
},
:recipients => {
:RecipientInfo => {
:email => 'martin#domain.co.uk',
:fax => SOAP::SOAPNil.new,
:role => 'SIGNER'
}
},
:mergeFieldInfo => SOAP::SOAPNil.new,
:tos => SOAP::SOAPNil.new,
:message => "This is neat.",
:name => "Test from SOAP-Lite: " + filename,
:reminderFrequency => "WEEKLY_UNTIL_SIGNED",
:signatureFlow => "SENDER_SIGNATURE_NOT_REQUIRED",
:signatureType => "ESIGN",
:callbackInfo => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:ccs => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:externalId => SOAP::SOAPNil.new, # we need to explicitly set elements to nil
:securityOptions => SOAP::SOAPNil.new # we need to explicitly set elements to nil
}
)
Can you please try to pass the value SIGNER with double quotations like "SIGNER". Just like you have for "ESIGN" and some other parameters.
Also, if sending for ESIGN, Fax should not need to be nil . Try to not defining :Fax at all.
You should change the email address and the role to have double quotes instead of single quotes.
:RecipientInfo => {
:email => "martin#domain.co.uk",
:fax => SOAP::SOAPNil.new,
:role => "SIGNER" change
Same as you have done for
:reminderFrequency => "WEEKLY_UNTIL_SIGNED",
:signatureFlow => "SENDER_SIGNATURE_NOT_REQUIRED",

Ruby on Rails condition on a hash

Can anyone help me with a bit of code:), I have the fallowing hash
selector = {
:fields => ['Id'],
:predicates => [
{:field => 'grpId', :operator => 'EQUALS', :values => [params_id]},
{:field => 'CrType', :operator => 'EQUALS', :values => ['KEYWORD']}
]
}
And I would like to create a condition for this line:
{:field => 'grpId', :operator => 'EQUALS', :values => [params_id]},
to be performed only if the params_id is not empty.
Thank you
selector = {
:fields => ['Id'],
:predicates => [
params_id ? {:field => 'grpId', :operator => 'EQUALS', :values => [params_id]} : nil,
{:field => 'CrType', :operator => 'EQUALS', :values => ['KEYWORD']}
].compact
}

Active resource param serialization issue

I'm trying to pass the following ruby hash into an active resource(3.0.9) find(:from) call.
my_hash = {
:p => {:s => 100, :e => 2},
:k => "blah",
:f => [
{
:fl => :bt,
:tp => :trm,
:vl => "A::B"
},
{
:fl => :jcni,
:tp => :trm,
:vl => [133, 134]
},
{
:mnfl => :bmns,
:mxfl => :bmxs,
:tp => :rfstv,
:vl => 1e5
},
{
:fl => :bpo,
:tp => :rftv,
:op => :eta,
:vl => 1.months.ago.strftime("%Y-%m-%d")
}
]
}
Resource.find_by_x_and_y(:all, :from => :blah, params: my_hash)
On the server side action, when I print the params hash, its all messed up. ( last 3 hashes in the array mapped to :f )
{
"f" => [
{
"fl" => "bt",
"tp" => "trm",
"vl" => "A::B"
},
{
"fl" => "jcni",
"tp" => "trm",
"vl" => [
"133",
"134"
],
"mnfl" => "bmns",
"mxfl" => "bmxs"
},
{
"tp" => "rfstv",
"vl" => "100000.0",
"fl" => "bpo",
"op" => "eta"
},
{
"tp" => "rftv",
"vl" => "2013-01-25"
}
],
"k" => "blah",
"p" => {
"e" => "2",
"s" => "100"
},
"action" => "blah",
"controller" => "x/Y",
"format" => "json"
}
my_hash.to_query gives me
f[][fl]=bt&f[][tp]=trm&f[][vl]=A%3A%3AB&f[][fl]=jcni&f[][tp]=trm&f[][vl][]=133&f[][vl][]=134&f[][mnfl]=bmns&f[][mxfl]=bmxs&f[][tp]=rfstv&f[][vl]=100000.0&f[][fl]=bpo&f[][op]=eta&f[][tp]=rftv&f[][vl]=2013-01-25&k=blah&p[e]=2&p[s]=100
which doesn't have indices, hence the mixup.
Is there a name for this type of serialization using "[]" ? Is this guaranteed to serialize/deserialize arbitrarily nested hashes/arrays/primitives faithfully ? How do I make active resource behave sanely ?

Resources