Skip to content

Commit

Permalink
Simplify filtering of body measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
paulosman committed Jan 9, 2016
1 parent 6dccf19 commit 6d6988c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
11 changes: 4 additions & 7 deletions lib/withings-sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,11 @@ def body_measurements(user_id, options = {})
# @return [Array<WithingsSDK::Measure::Weight>]
def weight(user_id, options = {})
groups = body_measurements(user_id, options)
weights = []
groups.each do |group|
group.measures.each do |measure|
next if !measure.is_a? WithingsSDK::Measure::Weight
weights << WithingsSDK::Measure::Weight.new(measure.attrs.merge('weighed_at' => group.date))
groups.map do |group|
group.measures.select { |m| m.is_a? WithingsSDK::Measure::Weight }.map do |m|
WithingsSDK::Measure::Weight.new(m.attrs.merge('weighed_at' => group.date))
end
end
weights
end.flatten
end

# Get details about a user's sleep
Expand Down
22 changes: 22 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@
end
end

describe '#weight' do
let (:user_id) { 123 }
let (:opts) { Hash['startdate', '2012-01-01', 'enddate', '2013-01-01'] }
let (:results) { configured_client.weight(user_id, opts) }

before do
stub_request(:get, /.*wbsapi.*/).
with(query: hash_including({action: 'getmeas'})).
to_return(body: fixture('body_measurements.json'))
end

it 'should return an array of weight measures' do
expect(results).to be_an Array
expect(results.first).to be_an WithingsSDK::Measure::Weight
expect(results.last).to be_an WithingsSDK::Measure::Weight
end

it 'should return 2 results' do
expect(results.length).to eq(2)
end
end

describe '#sleep_series' do
let (:user_id) { 123 }
let (:opts) { Hash['startdate', '2012-01-01', 'enddate', '2013-01-01'] }
Expand Down
33 changes: 25 additions & 8 deletions spec/fixtures/body_measurements.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@
"body": {
"measuregrps": [
{
"grpid": 123,
"grpid": 123,
"date": 2147483647,
"measures": [
{
"type": 11,
"unit": 0,
"type": 11,
"unit": 0,
"value": 70
},
},
{
"type": 1,
"unit": -3,
"type": 1,
"unit": -3,
"value": 87839
}
]
},
{
"grpid": 432,
"date": 21749343434,
"measures": [
{
"type": 2,
"unit": 0,
"value": 70
},
{
"type": 1,
"unit": -3,
"value": 87820
}
]
}
],
],
"updatetime": 123
},
},
"status": 0
}

0 comments on commit 6d6988c

Please sign in to comment.