Martin
Martin

Reputation: 11336

Is there a way to refactor this code?

is there any way to refactor this code?

def my_method
  response = HTTParty.post 'http://www.url.com', { 
    body: {
      param1: "value1",
      param2: "value2"
      }
    }
  response.message.include?("OK") ? true : false
end

I'd like to check if the response message includes okay but without needing to define the response as a variable. Is there any way I can retrieve the current method reply from the previous step in the method?

Upvotes: 1

Views: 90

Answers (2)

Caillou
Caillou

Reputation: 1500

def my_method
  HTTParty.post('http://www.url.com', { 
    body: {
      param1: "value1",
      param2: "value2"
      }
    }).message.include?("OK")
end

Upvotes: 1

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

HTTParty.post('http://www.url.com', { 
  body: {
    param1: "value1",
    param2: "value2"
  }
}).message.include?("OK")

Please note, that ? true : false is also redundant.

Upvotes: 9

Related Questions