# Ruby script file with dynamic param

**URL:** <https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130>\
**Category:** Logstash\
**Created:** [May 24, 2018, 11:34am UTC](https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130 "2018-05-24T11:34:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonviously](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonviously/32/44730_2.png) [@jonviously](https://discuss.elastic.co/u/jonviously)\
**Post date:** [May 24, 2018, 11:34am UTC](https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130/1 "2018-05-24T11:34:58Z")

</div>

hi everyone !

in order to factoring my logstash configuration i wanna replace many similar ruby inline code by a ruby script with dynamic param.

what i got before my ruby code :  
**[  
'key1 =\> 'value1',  
'key2 =\> 'value2'  
]**

what i need after :

**?key1=value1&key2=value2**

this inline ruby work fine :

```
# construct the cleaned params string for canonical params
# there isn't functions in logstash to implode array so we used "Ruby" code
if [og_url_param_decode_array] {
    ruby {
        code => "
            param_string='?'
            event.get('[og_url_param_decode_array]').each { |k, v|
                param_string+=k+'='+v+'&'
            }
            event.set('og_url_param_decode', param_string.chomp('&'))
        "
    }
}

```

but when i'm try to do the same thing in a ruby script i can't get input data from the event if it's an array but this work normaly if i try to get a string

see the script bellow

```
def register(params)
  @input_var_name = params["var_name"]
end

def filter(event)
  param_string='?'
  event.get(@input_var_name).each { |k, v|
      param_string+=k+'='+v+'&'
  }
  event.set('test', param_string)

  return [event]
end

```

this is how the script is called

```
ruby {
   path => "/etc/logstash/ruby/create_url_param.rb"
   script_params => { "var_name" => canonical_param_decode_array }
}

```

if canonical\_param\_decode\_array is an array my logstash pipeline is not working

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 24, 2018, 1:20pm UTC](https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130/2 "2018-05-24T13:20:18Z")

</div>

> [@jonviously](#):
>
> if canonical\_param\_decode\_array is an array my logstash pipeline is not working

What error message do you get when this fails? Is your canonical\_param\_decode\_array an array of hashes?

You left the chomp out of the function 🙂

---

<div class="post-metadata">

**Author:** ![jonviously](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonviously/32/44730_2.png) [@jonviously](https://discuss.elastic.co/u/jonviously)\
**Post date:** [May 24, 2018, 2:17pm UTC](https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130/3 "2018-05-24T14:17:31Z")

</div>

there is no error message, when my script is running nothing come in my elastic with the same comportement as a drop filter pluging

thanks for the chomp 🙂

---

<div class="post-metadata">

**Author:** ![jonviously](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonviously/32/44730_2.png) [@jonviously](https://discuss.elastic.co/u/jonviously)\
**Post date:** [May 24, 2018, 3:47pm UTC](https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130/4 "2018-05-24T15:47:39Z")

</div>

I found something, it was a mapping problem. i was trying to insert an array into a string field.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 21, 2018, 3:47pm UTC](https://discuss.elastic.co/t/ruby-script-file-with-dynamic-param/133130/5 "2018-06-21T15:47:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
