Skip to content

Commit

Permalink
rework params parsing to allow for proper usage of opts hash as a m…
Browse files Browse the repository at this point in the history
…ethod argument in `#get`

This fixes a bug where the options hash would cause Ruby's automatic hash expanding for
method arguments to throw an `unknown keyword` error for methods relying on `#get`
  • Loading branch information
zokioki committed Jun 17, 2018
1 parent c5928bc commit 7396c39
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.8.3
-----
- Fix bug regarding optional `params` parsing for GET requests.

0.8.2
-----
- Fix `#activity_logs_list` to allow passing in expected URL params.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Zoran Pesic
Copyright (c) 2018 Zoran Pesic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 6 additions & 5 deletions lib/fitbit_api/activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ def all_activities(opts={})
# * +:limit+ - the max of the number of entries returned (max: 20)

def activity_logs_list(opts={})
params = {}
opts[:params] = {}
param_defaults = { before_date: Date.today, after_date: nil, sort: 'desc', limit: 20, offset: 0 }

# merge defaults without overwriting specified values, then split params from options
opts.merge!(param_defaults) { |_key, val, _default| val }
param_defaults.keys.each { |i| params[i] = opts.delete(i) }
# move param values from top-level opts into :params sub-hash
param_defaults.each do |key, default_val|
opts[:params][key] = opts.delete(key) || default_val
end

get("user/#{user_id}/activities/list.json", opts, params: params)
get("user/#{user_id}/activities/list.json", opts)
end

# Returns the details of a specific activity in the Fitbit activities database in the format requested.
Expand Down
3 changes: 2 additions & 1 deletion lib/fitbit_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def request_headers
}
end

def get(path, opts={}, params: {})
def get(path, opts={})
params = opts.delete(:params) || {}
response = token.get(("#{@api_version}/" + path), params: deep_keys_to_camel_case!(params), headers: request_headers).response
object = MultiJson.load(response.body) unless response.status == 204
process_keys!(object, opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/fitbit_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module FitbitAPI
VERSION = '0.8.2'
VERSION = '0.8.3'
REPO_URL = 'https://github.com/zokioki/fitbit_api'
end

0 comments on commit 7396c39

Please sign in to comment.