diff --git a/README.md b/README.md index fa20c2b..7cfd4fb 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 @@ -129,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 + +Adjust timeout 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 ad35c7e..28069a1 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 = {}) @@ -80,7 +86,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 @@ -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?