title: Making my step definition prettier
body: <p>I'm writing a step definition that will take in an http request type (get, post, etc), the url of the api, and a table of data to send from gherkin. I implemented it the following way, but its a very imperative style and isn't necessarily that clear to other testers what's going on. It constructs requests from a table like this</p>
  
  <pre><code>| x | y |
  | 1 | 2 |
  | 5 | 7 |
  </code></pre>
  
  <p>such that the requests are sent using JSON and in this case two are sent, with the following JSON:</p>
  
  <pre><code>{
    "x":"1"
    "y":"2"
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
    "x":"5"
    "y":"7"
  }
  </code></pre>
  
  <p>In other words, each row after the first represent a request with specific values of the variables from the first row.</p>
  
  <p>My implementation below, any more readable refactors are welcome. Thanks.</p>
  
  <pre><code>When(/^I submit the following (?:in)?valid data in a "(.*?)" request to "(.*?)"$/) do |request_type, api, table|
    data = table.raw
    for i in 1...data.length #rows
      body = {}
      for j in 0...data[0].length #cols
        body[data[0][j]] = data[i][j]
      end
      @response = HTTParty.__send__ request_type, "/a/url#{api}", { 
          :body =&gt; body.to_json,
          :headers =&gt; { 'Content-Type' =&gt; 'application/json' }
      }
    end
  end
  </code></pre>