wiscale 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,50 @@
2
2
 
3
3
  Ruby Wrapper for Withings Wifi Scale API
4
4
 
5
+ == INSTALL:
6
+ sudo gem install wiscale
7
+
8
+ == SYNOPSIS:
9
+
10
+ Setup:
11
+ require 'wiscale'
12
+ client = WiScale.new(:userid => 123, :publickey => 'asdlkfja')
13
+
14
+ Get all measurements without any filters:
15
+ measurements = client.get_meas
16
+
17
+ Get only the last measurement:
18
+ measurements = client.get_meas(:limit => 1)
19
+
20
+ Get the last measurement as weight in lbs and %fat:
21
+ measurements[0].each do |rec|
22
+ type = rec['type'].to_i
23
+ value = (rec['value'].to_i * (10 ** rec['unit'])).to_f
24
+
25
+ if type == 6
26
+ puts "Fat %: " + value.round(2)
27
+ elsif type == 1
28
+ puts "Weight " + (value * 2.20462262).round(2) + " lb"
29
+ end
30
+ end
31
+
32
+ Subscribe a URL to push notification:
33
+ client.notify_subscribe('http://www.mytestingurl.com', 'this is a test')
34
+
35
+ Get the notification you just created:
36
+ client.notify_get('http://www.mytestingurl.com')
37
+
38
+ Revoke the notification:
39
+ client.notify_revoke('http://www.mytestingurl.com')
40
+
41
+
42
+ Get all user information:
43
+ client.get_by_userid
44
+
45
+ Get a list of users for a logon:
46
+ WiScale.get_users_list('myemail@address.com', 'mywithingspassword')
47
+
48
+
5
49
  == Note on Patches/Pull Requests
6
50
 
7
51
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,4 +1,6 @@
1
1
  # Change to a userid/publickey for running test suite
2
2
 
3
- userid : '60'
4
- publickey : 'fdb811'
3
+ userid : 'userid_of_user'
4
+ publickey : 'publickey_of_user'
5
+ secret : 'secret_key_from_scale'
6
+ mac : 'mac_address_of_scale'
@@ -112,6 +112,65 @@ class WiScale
112
112
  end
113
113
  end
114
114
 
115
+ def scale_once()
116
+ ret_val = JSON.parse(HTTParty.get(scale_url + '/once', :query => {:action => 'get'}))
117
+ return ret_val['body']['once']
118
+ end
119
+
120
+ def compute_scale_hash(mac, secret)
121
+ once = scale_once
122
+ hash = mac + ':' + secret + ':' + once
123
+
124
+ Digest::MD5::hexdigest(hash)
125
+ end
126
+
127
+ def session_start(email, secret, mac)
128
+ hash = compute_scale_hash(mac, secret)
129
+
130
+ ret_val = JSON.parse(HTTParty.get(scale_url + '/session', :query => {:action => 'new', :auth => mac, :duration => '30', :hash => hash}))
131
+
132
+ if ret_val['status'] == 0
133
+ ret_val['body']['sessionid']
134
+ else
135
+ ret_val['status']
136
+ end
137
+
138
+ end
139
+
140
+ def session_delete(sessionid)
141
+ ret_val = JSON.parse(HTTParty.get(scale_url + '/session', :query => {:action => 'delete', :sessionid => sessionid}))
142
+ ret_val['status']
143
+ end
144
+
145
+ # Create a new measurements record for the user
146
+ # * email address of account owner
147
+ # * secret password of scale masquerading as
148
+ # * mac address of scale masquerading as
149
+ # * timestamp (epoch time) of measurement recording
150
+ # * weight value (in kg) of measurement
151
+ # * percent body fat of measurement
152
+ def meas_create(email, secret, mac, timestamp, weight, bodyfat)
153
+ session = session_start(email, secret, mac)
154
+
155
+ bfmass = (weight*bodyfat*10).to_i
156
+ weight = weight * 1000
157
+
158
+ meas_string = "{\"measures\":[{\"value\":'#{weight}',\"type\":1,\"unit\":-3},{\"value\":'#{bfmass}',\"type\":8,\"unit\":-3}]}"
159
+
160
+ ret_val = JSON.parse(HTTParty.get(scale_url + '/measure', :query => {
161
+ :action => 'store',
162
+ :sessionid => session,
163
+ :userid => userid,
164
+ :macaddress => mac,
165
+ :meastime => timestamp,
166
+ :devtype => '1',
167
+ :attribstatus => '0',
168
+ :measures => meas_string
169
+ }))
170
+
171
+ ret_val
172
+ end
173
+
115
174
  def compute_hash(email, passwd)
116
175
  once = get_once
117
176
  hash = email + ':' + Digest::MD5::hexdigest(passwd) + ':' + once
@@ -123,6 +182,10 @@ class WiScale
123
182
  @api_url || @api_url = 'http://wbsapi.withings.net'
124
183
  end
125
184
 
185
+ def scale_url
186
+ @scale_url || @scale_url = 'http://scalews.withings.net/cgi-bin'
187
+ end
188
+
126
189
  def userid
127
190
  @userid
128
191
  end
@@ -64,4 +64,26 @@ class TestWiscaleRuby < Test::Unit::TestCase
64
64
  end
65
65
  end
66
66
 
67
+ context "sessions" do
68
+ should "start and end session" do
69
+ config = YAML.load_file("credentials.yml")
70
+ client = WiScale.new()
71
+
72
+ sessionid = client.session_start('jon@digital-drip.com', config['secret'], config['mac'])
73
+ assert_equal 0, client.session_delete(sessionid)
74
+
75
+ end
76
+ end
77
+
78
+ context "create measurement" do
79
+ should "create a new measurement" do
80
+ config = YAML.load_file("credentials.yml")
81
+ client = WiScale.new(:userid => config['userid'], :publickey => config['publickey'])
82
+
83
+ ret_val = client.meas_create(config['email'], config['secret'], config['mac'], Time.now.to_i, 100, 50)
84
+ p 'got return val of: ' + ret_val.inspect
85
+ end
86
+ end
87
+
67
88
  end
89
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jon Gaudette