Ruby - Unable to append data to existing hash - ruby-on-rails

I'm getting error no implicit conversion of String into Integer (TypeError) when I'm trying to push new_rule to existing hash.
Any idea what the issue is here?
require 'json'
network_sg_existing_rules = network_sg_properties["securityRules"]
puts network_sg_existing_rules
[{"name"=>"Port_8080", "id"=>"/subscriptions/44a91bb8-d388-467e-89e6-123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/networkSecurityGroups/sg_testing_temp_1/securityRules/Port_8080", "etag"=>"W/\"0d87997d-2a2a-4725-9be5-123456\"", "type"=>"Microsoft.Network/networkSecurityGroups/securityRules", "properties"=>{"provisioningState"=>"Succeeded", "protocol"=>"*", "sourcePortRange"=>"*", "destinationPortRange"=>"8080", "sourceAddressPrefix"=>"*", "destinationAddressPrefix"=>"*", "access"=>"Allow", "priority"=>100, "direction"=>"Inbound", "sourcePortRanges"=>[], "destinationPortRanges"=>[], "sourceAddressPrefixes"=>[], "destinationAddressPrefixes"=>[]}}]
# Append new rule.
new_rule = {
:name => "rule_4",
:properties => {
:protocol => "TCP",
:sourceAddressPrefix => "*",
:destinationAddressPrefix => "*",
:access => "Allow",
:destinationPortRange => "22",
:sourcePortRange => "*",
:priority => "301",
:direction => "Inbound"
}
}
network_sg_new_rules = network_sg_existing_rules["securityRules"].push(new_rule)
puts network_sg_new_rules

network_sg_properties is a hash which has a key called securityRules.
you are trying to add it in value of securityRules.
Instead of
network_sg_new_rules = network_sg_existing_rules["securityRules"].push(new_rule)
try
network_sg_properties = network_sg_properties["securityRules"].push(new_rule)
Hence value of network_sg_properties[securityRules] will be
[
[0] {
"name" => "Port_8080",
"id" => "/subscriptions/44a91bb8-d388-467e-89e6-123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/networkSecurityGroups/sg_testing_temp_1/securityRules/Port_8080",
"etag" => "W/\"0d87997d-2a2a-4725-9be5-123456\"",
"type" => "Microsoft.Network/networkSecurityGroups/securityRules",
"properties" => {
"provisioningState" => "Succeeded",
"protocol" => "*",
"sourcePortRange" => "*",
"destinationPortRange" => "8080",
"sourceAddressPrefix" => "*",
"destinationAddressPrefix" => "*",
"access" => "Allow",
"priority" => 100,
"direction" => "Inbound",
"sourcePortRanges" => [],
"destinationPortRanges" => [],
"sourceAddressPrefixes" => [],
"destinationAddressPrefixes" => []
}
},
[1] {
:name => "rule_4",
:properties => {
:protocol => "TCP",
:sourceAddressPrefix => "*",
:destinationAddressPrefix => "*",
:access => "Allow",
:destinationPortRange => "22",
:sourcePortRange => "*",
:priority => "301",
:direction => "Inbound"
}
}

Related

Rails replace one hash by another if keys exists

I don't know what's going on but I've got two hashes:
first_hash
{
'Ids' => ['string_first_hash'],
'Description' =>
{
'Url' => 'some_path',
'EventCallback' => {
'Url' => 'some_path',
'WhiteList' => ['string'],
},
'Steps' => [{
'OrderIndex' => 1,
'Recipients' => [
{
'Email' => 'email#first.hash',
'FirstName' => 'first_hash_name',
'LastName' => 'first_hash_last_name',
'LanguageCode' => 'en',
},
],
}],
},
}
And the second one:
template_hash
{
'Ids' => ['template_stirng'],
'Description' => {
'Name' => 'Template_name',
'Subject' => 'template_subject',
'Finish' => false,
'Steps' => [{
'OrderIndex' => 1,
'Recipients' => [
{
'Email' => 'Placeholder =>',
'FirstName' => '',
'LastName' => '',
'LanguageCode' => '',
'DisableEmail' => false,
'IdentificationMethods' => [],
},
],
'RecipientType' => 'Signer',
}],
},
}
Now I want to add first_hash to the template_hash and override only those key/value which exist in both and leave the rest from second_hash unchanged (should be added in the result). I thought all I want to do is:
test = first_hash.merge(template_hash)
But the result is surprising - nothing change, it prints second hash unchanged:
> test
{"SspFileIds"=>["7fcf6021-386d-4f31-a871-a89afb8fb36e"],
"SendEnvelopeDescription"=>
{"Name"=>"1 Recipient",
"EmailSubject"=>"Please sign the enclosed envelope",
"EmailBody"=>"Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
"DisplayedEmailSender"=>"",
"EnableReminders"=>true,
"FirstReminderDayAmount"=>5,
"RecurrentReminderDayAmount"=>3,
"BeforeExpirationDayAmount"=>3,
"DaysUntilExpire"=>28,
"StatusUpdateCallbackUrl"=>"",
"LockFormFieldsAtEnvelopeFinish"=>false,
"Steps"=>
[{"OrderIndex"=>1,
"Recipients"=>
[{"Email"=>"Placeholder:",
"FirstName"=>"",
"LastName"=>"",
"LanguageCode"=>"",
"DisableEmail"=>false,
"AddAndroidAppLink"=>false,
"AddIosAppLink"=>false,
"AddWindowsAppLink"=>false,
"AllowDelegation"=>true,
"AllowAccessFinishedWorkstep"=>false,
"SkipExternalDataValidation"=>false,
"AuthenticationMethods"=>[],
"IdentificationMethods"=>[]}],
"RecipientType"=>"Signer"}]}}
What's going on, am I misunderstand something? I'm using Rails 7 and Ruby 3.
[EDIT]
expected_result
{
'Ids' => ['string_first_hash'],
'Description' => {
'Name' => 'Template_name',
'Subject' => 'template_data_string',
'Finish' => false,
'Steps' => [{
'OrderIndex' => 1,
'Recipients' => [{
'Email' => 'email#first.hash',
'FirstName' => 'first_hash_name',
'LastName' => 'first_hash_last_name',
'LanguageCode' => 'en',
}],
'RecipientType' => 'Signer',
}],
'Url' => 'some_path',
'EventCallback' => {
'Url' => 'some_path',
'WhiteList' => ['string'],
},
},
}
Have you tried using deep_merge? https://apidock.com/rails/Hash/deep_merge
What you want is
test = first_hash.deep_merge(second_hash)

How to hide id from url in yii2

I'm trying to hide blog id from url.
For understanding better:
I want this URL
mysite/blog/post-slug-<id> // will be like: mysite/blog/post-slug-358
change to
mysite/blog/post-slug
Here my my URL code:
<a href="<?= Url::to(['post/view',
'id' => $model->id, 'slug' => $model->slug ]) ?>">
and this is my config/main:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<slug>-<id>' => 'blog/view',
]
could any one help me to solve it?
Remove -<id> from your url manager:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<slug>' => 'blog/view',
]
]
Then in your BlogController in actionView you can get it with this code:
$slug = Yii::$app->getRequest()->getQueryParam('slug');
I'm not sure that 'class' is nessecary; This i in my project how I use it:
'urlManager' => [
'baseUrl' => '/',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'ajax/<action>' => 'ajax/<action>',
'<first_step>/<second_step>/<third_step>' => 'page/index',
'<first_step>/<second_step>' => 'page/index',
'<first_step>' => 'page/index',
'<first_step:.+/>' => 'page/index', // redirect 301 /
],
],

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.

Mandrill: Please enter an array

I can't understand why I get an Invalid array!
response = Unirest.post "#{##endpoint}/messages/send.json",
headers:{ "Accept" => "application/json" },
parameters:{
:key => ##apikey,
:message => {
:text => "example text",
:subject => "bla bla",
:from_email => "info#site.com",
:from_name => "site.com",
to: [
{
:email => "myemail#gmail.com",
:name => "myname",
:type => "to"
}
]
}
}
I get this error
{"status":"error","code":-2,"name":"ValidationError","message":"Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}"}
Shouldn't the to array be
[to]=>
array(
[0]=>array(
[email] => "myemail#gmail.com",
[name] => "myname",
[type] => "to"
)
)

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