From 9ef68739219fabd20cd8cc3293337640525f1429 Mon Sep 17 00:00:00 2001 From: Ryan Tinker Date: Wed, 23 Oct 2013 18:03:56 -0700 Subject: [PATCH 1/5] Added ability to fetch a subset of activities Can now use activities(indexStart:0,indexEnd:5) --- lib/nike/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nike/client.rb b/lib/nike/client.rb index ad35c7e..43d5519 100644 --- a/lib/nike/client.rb +++ b/lib/nike/client.rb @@ -80,7 +80,7 @@ def fetch_activity_data(id) def fetch_user_data(opts) type = (opts[:type] || :run).to_sym cache(type) do - wrap get_authorized(ACTIVITIES_URLS[type], query: { indexStart: 0, indexEnd: 999999}) + wrap get_authorized(ACTIVITIES_URLS[type], query: { indexStart: (opts[:indexStart] || 0), indexEnd: (opts[:indexEnd] || 999999) }) end end From c99d1ac3d2db123314afdb8e5d13be9baaae61ff Mon Sep 17 00:00:00 2001 From: Ryan Tinker Date: Wed, 23 Oct 2013 18:06:20 -0700 Subject: [PATCH 2/5] Updated readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fa20c2b..9de8852 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ A summary of all activities by type (type is :run by default) $ c.activities(type: :hr) # get all heart rate activities + $ c.activities({indexStart:0, indexEnd:10}) # get first 10 runs + Full activity data (Slow if you have alot of data, use c.activity to fetch a detailed data set for a single activity) $ c.detailed_activities # get detailed data for all runs From 0c56696cfa62a6f42dd3ffb98687b2e4001ed316 Mon Sep 17 00:00:00 2001 From: Ryan Tinker Date: Thu, 24 Oct 2013 15:19:53 -0700 Subject: [PATCH 3/5] Added a timeout on calls to Nike Added a 15 second default timeout on the get and post calls to Nike. This will raise a Timeout::Error if a call takes longer than the timeout specified. --- README.md | 13 +++++++++++++ lib/nike/client.rb | 26 ++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9de8852..57400ba 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,19 @@ will have the side-effect of refreshing the cache. $ c.activities! $ c.activity!(12345) + + +## Timeout + +A 15-second on all calls to Nike is set by default. If you find that this is too short, or wish to change it, use the following: + +Adjust timeout during client initialization + + $ c = Nike::Client.new('your_email', 'your_password', timeout_seconds: #) # => Number of seconds to set the timeout + +Toggle caching after client initialization + + $ c.timeout = # # => Number of seconds to set the timeout ## Contributing diff --git a/lib/nike/client.rb b/lib/nike/client.rb index 43d5519..4ce4ce1 100644 --- a/lib/nike/client.rb +++ b/lib/nike/client.rb @@ -34,6 +34,7 @@ class Nike::Client def initialize(email, password, opts = {}) @email, @password, @user_id = email, password, nil @caching, @cache = opts[:caching] || true, {} + @timeout_seconds = opts[:timeout_seconds] || 15 end def activity(id) @@ -41,7 +42,12 @@ def activity(id) end def activities(opts = {}) - fetch_user_data(opts).activities.map { |a| a.activity } + fetched_activities = fetch_user_data(opts).activities + if fetched_activities.nil? + return {} + else + return fetched_activities.map { |a| a.activity } + end end def detailed_activities(opts = {}) @@ -89,19 +95,27 @@ def fetch_user_data(opts) def get_authorized(url, opts = {}) login_if_unauthenticated raise "Authentication failed!" unless logged_in? - self.class.get(personify_url(url), opts).to_hash + + timeout(@timeout_seconds) do + self.class.get(personify_url(url), opts).to_hash + end + end def login_if_unauthenticated return if logged_in? - response = self.class.login(@email, @password) + + response = self.class.login(@email, @password) @user_id = response['serviceResponse']['body']['User']['screenName'] + end def self.login(email, password) - response = post(LOGIN_URL, query: { email: email, password: password }) - self.default_cookies.add_cookies(response.headers['set-cookie']) - response + timeout(@timeout_seconds) do + response = post(LOGIN_URL, query: { email: email, password: password }) + self.default_cookies.add_cookies(response.headers['set-cookie']) + response + end end def logged_in? From 5ec9cfea19011ad1d8b9a26561f8170c88dc0485 Mon Sep 17 00:00:00 2001 From: Ryan Tinker Date: Thu, 24 Oct 2013 15:29:39 -0700 Subject: [PATCH 4/5] When calling activities, return an empty array if a user doesn't have any activities --- lib/nike/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nike/client.rb b/lib/nike/client.rb index 4ce4ce1..28069a1 100644 --- a/lib/nike/client.rb +++ b/lib/nike/client.rb @@ -44,7 +44,7 @@ def activity(id) def activities(opts = {}) fetched_activities = fetch_user_data(opts).activities if fetched_activities.nil? - return {} + return [] else return fetched_activities.map { |a| a.activity } end From 1ae033217c5adf68a18004ccefbf6cea615d359d Mon Sep 17 00:00:00 2001 From: Ryan Tinker Date: Thu, 24 Oct 2013 15:39:04 -0700 Subject: [PATCH 5/5] Adjusted readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 57400ba..7cfd4fb 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Adjust timeout during client initialization $ c = Nike::Client.new('your_email', 'your_password', timeout_seconds: #) # => Number of seconds to set the timeout -Toggle caching after client initialization +Adjust timeout after client initialization $ c.timeout = # # => Number of seconds to set the timeout