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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions lib/nike/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ 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)
fetch_activity_data(id.to_s).activity
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 = {})
Expand Down Expand Up @@ -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

Expand All @@ -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?
Expand Down