Class: WiScale
- Inherits:
-
Object
- Object
- WiScale
- Defined in:
- lib/wiscale.rb
Overview
See API documentation for more information on return values www.withings.com/en/api/bodyscale
OpenStruct used when returning the ‘body’ information from api Individual sub-measurements are returned as array of hashes
Instance Method Summary collapse
- #api_url ⇒ Object
- #compute_hash(email, passwd) ⇒ Object
- #compute_scale_hash(mac, secret) ⇒ Object
- #get_by_userid ⇒ Object
- #get_meas(*params) ⇒ Object
- #get_once ⇒ Object
- #get_status ⇒ Object
- #get_users_list(email, passwd) ⇒ Object
-
#initialize(*params) ⇒ WiScale
constructor
A new instance of WiScale.
-
#meas_create(email, secret, mac, timestamp, weight, bodyfat) ⇒ Object
Create a new measurements record for the user * email address of account owner * secret password of scale masquerading as * mac address of scale masquerading as * timestamp (epoch time) of measurement recording * weight value (in kg) of measurement * percent body fat of measurement.
- #notify_get(callbackurl) ⇒ Object
- #notify_revoke(callbackurl) ⇒ Object
- #notify_subscribe(callbackurl, comment) ⇒ Object
- #publickey ⇒ Object
- #scale_once ⇒ Object
- #scale_url ⇒ Object
- #session_delete(sessionid) ⇒ Object
- #session_start(email, secret, mac) ⇒ Object
- #user_update(ispublic) ⇒ Object
- #userid ⇒ Object
Constructor Details
#initialize(*params) ⇒ WiScale
Returns a new instance of WiScale.
14 15 16 17 |
# File 'lib/wiscale.rb', line 14 def initialize(*params) @publickey = params[0][:publickey] if params.length > 0 @userid = params[0][:userid] if params.length > 0 end |
Instance Method Details
#api_url ⇒ Object
181 182 183 |
# File 'lib/wiscale.rb', line 181 def api_url @api_url || @api_url = 'http://wbsapi.withings.net' end |
#compute_hash(email, passwd) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/wiscale.rb', line 174 def compute_hash(email, passwd) once = get_once hash = email + ':' + Digest::MD5::hexdigest(passwd) + ':' + once Digest::MD5::hexdigest(hash) end |
#compute_scale_hash(mac, secret) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/wiscale.rb', line 120 def compute_scale_hash(mac, secret) once = scale_once hash = mac + ':' + secret + ':' + once Digest::MD5::hexdigest(hash) end |
#get_by_userid ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wiscale.rb', line 47 def get_by_userid ret_val = JSON.parse(HTTParty.get(api_url + '/user', :query => { :action => 'getbyuserid', :userid => userid, :publickey => publickey})) if ret_val['status'] == 0 ret_val['body']['users'].collect { |user| OpenStruct.new(user) } else ret_val['status'] end end |
#get_meas(*params) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wiscale.rb', line 29 def get_meas(*params) params = Array.new unless params params[0] = Hash.new unless params[0] params[0][:action] = 'getmeas' params[0][:userid] = userid params[0][:publickey] = publickey ret_val = JSON.parse(HTTParty.get(api_url + '/measure', :query => params[0])) if ret_val['status'] == 0 measures = ret_val['body']['measuregrps'].collect { |meas| OpenStruct.new(meas) } OpenStruct.new({:updatetime => ret_val['body']['updatetime'], :measures => measures}) else ret_val['status'] end end |
#get_once ⇒ Object
24 25 26 27 |
# File 'lib/wiscale.rb', line 24 def get_once() ret_val = JSON.parse(HTTParty.get(api_url + '/once', :query => {:action => 'get'})) return ret_val['body']['once'] end |
#get_status ⇒ Object
19 20 21 22 |
# File 'lib/wiscale.rb', line 19 def get_status() ret_val = JSON.parse(HTTParty.get(api_url + '/once', :query => {:action => 'probe'})) return ret_val['status'] end |
#get_users_list(email, passwd) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wiscale.rb', line 60 def get_users_list(email, passwd) hash = compute_hash(email, passwd) ret_val = JSON.parse(HTTParty.get(api_url + '/account', :query => {:action => 'getuserslist', :email => email, :hash => hash})) if ret_val['status'] == 0 ret_val['body']['users'].collect { |user| OpenStruct.new(user) } else ret_val['status'] end end |
#meas_create(email, secret, mac, timestamp, weight, bodyfat) ⇒ Object
Create a new measurements record for the user
-
email address of account owner
-
secret password of scale masquerading as
-
mac address of scale masquerading as
-
timestamp (epoch time) of measurement recording
-
weight value (in kg) of measurement
-
percent body fat of measurement
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/wiscale.rb', line 152 def meas_create(email, secret, mac, , weight, bodyfat) session = session_start(email, secret, mac) bfmass = (weight*bodyfat*10).to_i weight = weight * 1000 meas_string = "{\"measures\":[{\"value\":'#{weight}',\"type\":1,\"unit\":-3},{\"value\":'#{bfmass}',\"type\":8,\"unit\":-3}]}" ret_val = JSON.parse(HTTParty.get(scale_url + '/measure', :query => { :action => 'store', :sessionid => session, :userid => userid, :macaddress => mac, :meastime => , :devtype => '1', :attribstatus => '0', :measures => meas_string })) ret_val end |
#notify_get(callbackurl) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/wiscale.rb', line 100 def notify_get(callbackurl) ret_val = JSON.parse(HTTParty.get(api_url + '/notify', :query => { :action => 'get', :userid => userid, :publickey => publickey, :callbackurl => URI.encode(callbackurl) })) if ret_val['status'] == 0 OpenStruct.new(ret_val['body']) else ret_val['status'] end end |
#notify_revoke(callbackurl) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wiscale.rb', line 89 def notify_revoke(callbackurl) ret_val = JSON.parse(HTTParty.get(api_url + '/notify', :query => { :action => 'revoke', :userid => userid, :publickey => publickey, :callbackurl => URI.encode(callbackurl) })) ret_val['status'] end |
#notify_subscribe(callbackurl, comment) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/wiscale.rb', line 77 def notify_subscribe(callbackurl, comment) ret_val = JSON.parse(HTTParty.get(api_url + '/notify', :query => { :action => 'subscribe', :userid => userid, :publickey => publickey, :callbackurl => URI.encode(callbackurl), :comment => comment })) ret_val['status'] end |
#publickey ⇒ Object
193 194 195 |
# File 'lib/wiscale.rb', line 193 def publickey @publickey end |
#scale_once ⇒ Object
115 116 117 118 |
# File 'lib/wiscale.rb', line 115 def scale_once() ret_val = JSON.parse(HTTParty.get(scale_url + '/once', :query => {:action => 'get'})) return ret_val['body']['once'] end |
#scale_url ⇒ Object
185 186 187 |
# File 'lib/wiscale.rb', line 185 def scale_url @scale_url || @scale_url = 'http://scalews.withings.net/cgi-bin' end |
#session_delete(sessionid) ⇒ Object
140 141 142 143 |
# File 'lib/wiscale.rb', line 140 def session_delete(sessionid) ret_val = JSON.parse(HTTParty.get(scale_url + '/session', :query => {:action => 'delete', :sessionid => sessionid})) ret_val['status'] end |
#session_start(email, secret, mac) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/wiscale.rb', line 127 def session_start(email, secret, mac) hash = compute_scale_hash(mac, secret) ret_val = JSON.parse(HTTParty.get(scale_url + '/session', :query => {:action => 'new', :auth => mac, :duration => '30', :hash => hash})) if ret_val['status'] == 0 ret_val['body']['sessionid'] else ret_val['status'] end end |
#user_update(ispublic) ⇒ Object
71 72 73 74 75 |
# File 'lib/wiscale.rb', line 71 def user_update(ispublic) ret_val = JSON.parse(HTTParty.get(api_url + '/user', :query => {:action => 'update', :userid => userid, :publickey => publickey, :ispublic => ispublic})) ret_val['status'] end |
#userid ⇒ Object
189 190 191 |
# File 'lib/wiscale.rb', line 189 def userid @userid end |