Search code examples
ruby-on-railsbinding.pry

How to take payload of an API as an argument in ruby function


I have done rails c on my console of rubymine.

I want to execute a function in rails console.

the function is

funtion_one(Api_payload , some_integer_Value)

API_payload is : -

{
"data" : [
    {
        "from" : "today",
          "to" : "next_day"
    }
],
  "some_ids" : [
    9808
]
}

so basically how do i write it in rails console ..?

pry(main)>  function_one({ "date_ranges" : [ { "from" : "today", "to" : "next_day" } ], "some_ids" : [ 9808 ] } , 25)

after writing above command , error comes , which is SyntaxError: unexpected tIDENTIFIER, expecting end-of-input


Solution

  • You need to parse the incoming payload, since it isn't ruby hash.

    # Notice the ' and beginning and end, it needs to be a string
    
    payload = '{
      "data" : [
          {
            "from" : "today",
            "to" : "next_day"
          }
        ],
       "some_ids" : [9808]
      }'
    
    usable_payload = JSON.parse(payload)
    
    # then call your method
    method_one(usable_payload, 25)