Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions lib/fitgem/body_measurements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def body_measurements_on_date(date)
#
# @since v0.9.0
def body_weight(opts = {})
get determine_body_uri("/user/#{@user_id}/body/log/weight", opts)
get uri_with_date_range("/user/#{@user_id}/body/log/weight", opts)
end

# Retrieve the body weight goal of the current user
Expand All @@ -56,7 +56,7 @@ def body_weight_goal
#
# @since v0.9.0
def body_fat(opts = {})
get determine_body_uri("/user/#{@user_id}/body/log/fat", opts)
get uri_with_date_range("/user/#{@user_id}/body/log/fat", opts)
end

# Retrieve the body fat goal of the current user
Expand Down Expand Up @@ -188,31 +188,5 @@ def delete_body_weight_log(logId)
def delete_body_fat_log(logId)
delete("/user/#{@user_id}/body/log/fat/#{logId}.json")
end

private

# Determine the URI for the body_weight or body_fat method
#
# @params [String] base_uri the base URI for the body weight or body fat method
# @params [Hash] opts body weight/fat options
# @option opts [Date] date The date in the format YYYY-mm-dd.
# @option opts [Date] base-date The end date when period is provided, in the
# format yyyy-MM-dd or today; range start date when a date range is provided.
# @option opts [String] period The date range period. One of 1d, 7d, 30d, 1w, 1m
# @option opts [Date] end-date Range end date when date range is provided.
# Note that period should not be longer than 31 day
#
# @return [String] an URI based on the base URI and provided options
def determine_body_uri(base_uri, opts = {})
if opts[:date]
date = format_date opts[:date]
"#{base_uri}/date/#{date}.json"
elsif opts[:base_date] && (opts[:period] || opts[:end_date])
date_range = construct_date_range_fragment opts
"#{base_uri}/#{date_range}.json"
else
raise Fitgem::InvalidArgumentError, "You didn't supply one of the required options."
end
end
end
end
17 changes: 16 additions & 1 deletion lib/fitgem/heart_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ def heart_rate_on_date(date)
get("/user/#{@user_id}/heart/date/#{format_date(date)}.json")
end

# Get a list of heart rate entries for the specified period
#
# @params [Hash] opts heart rate options
# @option opts [Date] base_date The end date when period is provided, in the
# format yyyy-MM-dd or today; range start date when a date range is provided.
# @option opts [String] period The date range period. One of 1d, 7d, 30d, 1w, 1m
# @option opts [Date] end_date Range end date when date range is provided.
#
# @return [Hash] A hash containing heart rate entries
def heart_rate(opts = {})
date_range_opts = opts.dup
date_range_opts.delete(:date)
get uri_with_date_range("/user/#{@user_id}/activities/heart", date_range_opts)
end

# ==========================================
# Heart Rate Logging Methods
# ==========================================
Expand All @@ -29,7 +44,7 @@ def heart_rate_on_date(date)
# @return [Hash] Summary of logged information
def log_heart_rate(opts)
unless opts[:tracker] && opts[:heart_rate] && opts[:date]
raise Fitgem::InvalidArgumentError, "Must include :tracker, :heart_rate, and :date in order to lof heart rate data"
raise Fitgem::InvalidArgumentError, "Must include :tracker, :heart_rate, and :date in order to log heart rate data"
end

opts[:heartRate] = opts.delete :heart_rate
Expand Down
23 changes: 23 additions & 0 deletions lib/fitgem/time_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,28 @@ def construct_date_range_fragment(options)
range_str
end

# Determine the URI for the methods accepting a date range (body weight, body fat, heart rate etc.)
#
# @params [String] base_uri the base URI for the method
# @params [Hash] opts date range options
# @option opts [Date] date The date in the format YYYY-mm-dd.
# @option opts [Date] base-date The end date when period is provided, in the
# format yyyy-MM-dd or today; range start date when a date range is provided.
# @option opts [String] period The date range period. One of 1d, 7d, 30d, 1w, 1m
# @option opts [Date] end-date Range end date when date range is provided.

#
# @return [String] an URI based on the base URI and provided options
def uri_with_date_range(base_uri, opts = {})
if opts[:date]
date = format_date opts[:date]
"#{base_uri}/date/#{date}.json"
elsif opts[:base_date] && (opts[:period] || opts[:end_date])
date_range = construct_date_range_fragment opts
"#{base_uri}/#{date_range}.json"
else
raise Fitgem::InvalidArgumentError, "You didn't supply one of the required options."
end
end
end
end
30 changes: 30 additions & 0 deletions spec/fitgem_heart_rate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe Fitgem::Client do
before(:each) do
@client = Fitgem::Client.new({
:consumer_key => '12345',
:consumer_secret => '67890'
})
end

describe '#heart_rate' do
it 'formats the correct URI based on a base date and period' do
expect(@client).to receive(:get).with('/user/-/activities/heart/date/2015-09-27/30d.json')
@client.heart_rate({:base_date => '2015-09-27', :period => '30d'})
end

it 'formats the correct URI based on a base date and end date' do
expect(@client).to receive(:get).with('/user/-/activities/heart/date/2015-12-01/2015-12-28.json')
@client.heart_rate({:base_date => '2015-12-01', :end_date => '2015-12-28'})
end

it 'raises an error when none of the required options are specified' do
expect { @client.heart_rate({}) }.to raise_error(Fitgem::InvalidArgumentError)
end

it 'raises an error when date is specified' do
expect { @client.heart_rate({:date => '2015-04-26'}) }.to raise_error(Fitgem::InvalidArgumentError)
end
end
end