Ruby Curb gem - Ubiquiti mPower HTTP API
<p>I am attempting to replicate <a href="http://community.ubnt.com/t5/mFi/mPower-mFi-Switch-and-mFi-In-Wall-Outlet-HTTP-API/m-p/1076449#U1076449" rel="nofollow">this</a> HTTP API for Ubiquiti's mPower units using the <a href="https://github.com/taf2/curb" rel="nofollow">Curb gem</a> and store the output from this into a CSV file.</p>

<p><strong>Using cURL:</strong></p>

<pre><code>curl -X POST -d "username=user&amp;password=pass" -b "AIROS_SESSIONID=01234567890123456789012345678901" 192.168.1.75/login.cgi

curl -b "AIROS_SESSIONID=01234567890123456789012345678901" 192.168.1.75/sensors/1/power
</code></pre>

<p>Returns:</p>

<pre><code>{"sensors":[{"power":2.740736961}],"status":"success"}
</code></pre>

<p><br/>
<strong>My attempt using Curb:</strong></p>

<pre><code>require 'curb'    

cookie = rand(36**32).to_s(36)

curl = Curl::Easy.new('http://192.168.1.75/login.cgi')

curl.http_post(
  Curl::PostField.content('username', 'user'),
  Curl::PostField.content('password', 'pass'))

curl = Curl::Easy.new('http://192.168.1.75/sensors/1/power')
curl.cookies = cookie
curl.perform
</code></pre>

<p>Adding <code>puts curl.perform</code> to the end of the script prints <code>true</code>.</p>

<p>Using cURL, I can usually pull the data with a delay ranging from 1-3 seconds from the time I execute <code>curl -b...</code> from the command line before the response shows up. How can I achieve the same results using Curb and save <strong>only</strong> the power data to a local variable? Is there a better tool to accomplish this task besides Curb?</p>